-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Trying to insert null in an identifier column?
PostPosted: Fri Dec 26, 2003 6:21 am 
Newbie

Joined: Fri Dec 26, 2003 5:47 am
Posts: 4
Hello,

this is my first trial at Hibernate and I'm encountering the following difficulty : when Hibernate tries to save my very simple Customer object to the database, it issues a "null" in the identifier column.

Does anybody sees why?

mapping file:
---------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="jfh.hibernatetest">

<class name="Customer" table="Customer">
<id name="id" column="id" type="long">
<generator class="native"/>
</id>

<property name="firstName" type="string"/>
<property name="lastName" type="string"/>

</class>

</hibernate-mapping>

part of the code trying to insert a customer:
--------------------------------------------------

Customer c=new Customer();
c.setFirstName("Jeff");
c.setLastName("Halleux");

try {
tx = session.beginTransaction();
session.save(c);
tx.commit();



I'm using Hypersonic in standalone mode.


Thanks, JF


Top
 Profile  
 
 Post subject: Re: Trying to insert null in an identifier column?
PostPosted: Fri Dec 26, 2003 9:52 am 
Beginner
Beginner

Joined: Thu Dec 11, 2003 9:54 am
Posts: 25
I think that you need to show "column", like:

Code:
    <property
        name="someName"
        type="java.lang.String"
        column="SOME_NAME"
        length="50"
    />



jeffhalleux wrote:
Hello,

this is my first trial at Hibernate and I'm encountering the following difficulty : when Hibernate tries to save my very simple Customer object to the database, it issues a "null" in the identifier column.

Does anybody sees why?



Top
 Profile  
 
 Post subject: Re: Trying to insert null in an identifier column?
PostPosted: Fri Dec 26, 2003 2:34 pm 
Newbie

Joined: Fri Dec 26, 2003 5:47 am
Posts: 4
Still doesn't work.

This is the exception & generated SQL if it may help:

...
Try to insert null into a non-nullable column in statement [insert into Customer (firstName, lastName, id) values ('Jeff', 'Halleux', null)]
...

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 4:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It should work (at least from my understanding of your mapping and code ;)

because you are saying "native" to an HSQL database the actual id strategy chosen will be IdentityGenerator and thus the table definition should automatically generate and id for the row.....but then hibernate should not send "null" with as argument?!

could you make the smallest possible main method with apropriate mapping and then show us that ?

A stacktrace would also be very helpful.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 4:30 pm 
Newbie

Joined: Fri Dec 26, 2003 5:47 am
Posts: 4
This is my main:
==========

Code:
public static void main(String[] args) throws MappingException, HibernateException {
      
      Configuration cfg = new Configuration();
      cfg.addClass(Customer.class);
      SessionFactory sessions = cfg.buildSessionFactory();
      Session session = sessions.openSession();
      Transaction tx = null;
      
      Customer c=new Customer();
      c.setFirstName("Jeff");
      c.setLastName("Halleux");
      
      try {
         tx = session.beginTransaction();
         session.save(c);
         tx.commit();
      } catch (HibernateException he) {
         if (tx != null)
            tx.rollback();
         throw he;
      } finally {
         session.close();
      }
   }



The customer class:
-----------------------

Quote:
package jfh.hibernatetest;

public class Customer {

private Long id;
private String firstName;
private String lastName;

public Customer() {
}

public String getFirstName() {
return firstName;
}

public Long getId() {
return id;
}

public String getLastName() {
return lastName;
}

public void setFirstName(String string) {
firstName = string;
}

public void setId(Long along) {
id = along;
}

public void setLastName(String string) {
lastName = string;
}

}



...and the relevant part of the log:
---------------------------------------

Quote:
21:24:52,333 DEBUG SQL:223 - insert into Customer (firstName, lastName, id) values (?, ?, null)
21:24:52,333 DEBUG BatcherImpl:227 - preparing statement
21:24:52,343 DEBUG EntityPersister:389 - Dehydrating entity: [jfh.hibernatetest.Customer#<null>]
21:24:52,353 DEBUG JDBCExceptionReporter:36 - SQL Exception
java.sql.SQLException: Try to insert null into a non-nullable column in statement [insert into Customer (firstName, lastName, id) values ('Jeff', 'Halleux', null)]
at org.hsqldb.Trace.getError(Unknown Source)
at org.hsqldb.jdbcResultSet.<init>(Unknown Source)
at org.hsqldb.jdbcConnection.executeStandalone(Unknown Source)
at org.hsqldb.jdbcConnection.execute(Unknown Source)
at org.hsqldb.jdbcStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbcStatement.executeUpdate(Unknown Source)
at org.hsqldb.jdbcPreparedStatement.executeUpdate(Unknown Source)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:502)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:433)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:876)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:817)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:737)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at jfh.hibernatetest.Main.main(Main.java:26)



Note that I'm executing all of this inside Eclipse.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 4:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It work like a charm for me ;)

...but i also let hibernate create the table for me:

21:49:44,656 DEBUG SchemaExport:149 - create table Customer (
id BIGINT NOT NULL IDENTITY,
firstName VARCHAR(255),
lastName VARCHAR(255)
)

And as i wrote before - HSQL's native idgenerator uses IdentityGenerator which require that your tables id column is an "identity" column.

If it is, then you get the following good result.

21:49:44,656 INFO SchemaExport:160 - schema export complete
21:49:44,666 INFO DriverManagerConnectionProvider:137 - cleaning up connection pool: jdbc:hsqldb:.
21:49:44,756 INFO DriverManagerConnectionProvider:137 - cleaning up connection pool: jdbc:hsqldb:.
21:49:44,786 DEBUG SQL:223 - insert into Customer (firstName, lastName, id) values (?, ?, null)
21:49:44,796 DEBUG StringType:46 - binding 'Jeff' to parameter: 1
21:49:44,796 DEBUG StringType:46 - binding 'Halleux' to parameter: 2
21:49:44,806 DEBUG SQL:223 - CALL IDENTITY()

Note, that it is actually sending null as the third parameter (my mistake before to say it should not) ...and it works. So, please check your DDL's for the table.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 5:11 pm 
Newbie

Joined: Fri Dec 26, 2003 5:47 am
Posts: 4
I'll do some more research tomorrow but this is what I get when Hibernate tries to create the table:

Code:
Unsuccessful: create table Customer (id BIGINT NOT NULL IDENTITY, firstName VARCHAR(255), lastName VARCHAR(255))
22:03:17,568 ERROR SchemaExport:155 - Wrong data type: ID in statement [create table Customer (id BIGINT NOT NULL IDENTITY, firstName VARCHAR(255), lastName VARCHAR(255))]


I'm running HSQLDB 1.7.1

Anyway, why should Hibernate try to insert a row with a null in a primary key??? That doesn't make much sense to me.

I guess I need some more reading...

Thanks anyway,

JF


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 5:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
My guess is your are running with a buggy hsql ?

Have you tried use the one distributed with Hibernate ?

If you can't make it work - then alternatively use another id-strategy.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2003 7:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I think there is a bug in HSQL when using BIGINT for identity columns. See this thread: http://forum.hibernate.org/viewtopic.php?t=926427&highlight=hsql+bigint


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.