-->
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.  [ 3 posts ] 
Author Message
 Post subject: Begginer problem with automaticly generating an id
PostPosted: Tue Apr 19, 2005 5:40 am 
Newbie

Joined: Mon Apr 18, 2005 7:23 am
Posts: 3
Location: Cologne, Germany
The problem:
I am trying to save an object.
I don't really know know how the generators work.
As far as I understood there's no need for me to set the id.
The generator will take care of that.
I tried it with other generators (sequence,hilo,...) but it won't work.
The sql-code
Code:
insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)

tells me there is a 'null' value to be inserted. ok i can understand that this will not work since pk values must be != null. But even when I initialise the property id with a value e.g. new Long(0) it will always insert a 'null'.
What about the other values they all are mapped with a question mark. Why does the code not show the real values I set before?

I tried to find sth. in the docs and forum about how id are generated and used. but I could not find anything that talks about null values, other initial values in detail.

Thx in advance...
The bean
The pojo later in the code...
Code:
public class ContactEntry {

    public final static int UNKNOWN_SEX = 0;
   
    // the primary key
    private Long id;

    private String organisation = null;
    private String firstName = null;
    private String lastName = null;
    private String middleName = null;
    private int gender = UNKNOWN_SEX;

//...simple getter and setter
}


Hibernate version:
3.0rc1

Mapping documents:

Code:
<hibernate-mapping>

<class name="de.greyshine.telab.model.ContactEntry" table="contact_entrys">
                <id name="id" column="id" type="long">
                    <generator class="identity"/>
                </id>
                <property name="organisation" type="string"/>
                <property name="lastName" type="string"/>
                <property name="firstName" type="string"/>
                <property name="middleName" type="string"/>
                <property name="gender" type="integer"/>
        </class>
   
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

pojo is the object to be stored

Code:
Session session = getSession(  );

Transaction tx = session.beginTransaction(  );

DbModulException dme = null;

try {

            session.save( pojo );
            tx.commit(  );

            // Log.log( "STORED: " + pojo );
} catch ( HibernateException e ) {

            tx.rollback(  );

            // Log.log( "Storing failed: " + e.getMessage(  ) );
            dme = new DbModulException( "Add failed: " + pojo, e );
}


Full stack trace of any exception that occurs:

Quote:
org.hibernate.exception.GenericJDBCException: could not insert: [de.greyshine.telab.model.ContactEntry]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1747)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:238)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
at de.greyshine.telab.database.HibernateModul.add(HibernateModul.java:177)
... 26 more
Caused by: java.sql.SQLException: Try to insert null into a non-nullable column: column: ID table: CONTACT_ENTRYS in statement [insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)]
at org.hsqldb.jdbc.jdbcUtil.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1737)
... 40 more


Name and version of the database you are using:

hsqldb 1.7

The generated SQL (show_sql=true):

insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)

Debug level Hibernate log excerpt:
11:00:35,209 WARN JDBCExceptionReporter:57 - SQL Error: -10, SQLState: 23000
11:00:35,219 ERROR JDBCExceptionReporter:58 - Try to insert null into a non-nullable column: column: ID table: CONTACT_ENTRYS in statement [insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)]
11:00:35,410 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:file:resources/db/telabdb


Top
 Profile  
 
 Post subject: Begginer problem with automaticly generating an id
PostPosted: Tue Apr 19, 2005 6:23 am 
Beginner
Beginner

Joined: Thu Sep 02, 2004 4:54 am
Posts: 24
Hi,
I don't know hsql very well but I will try to help you.

First, do you try to change the "unsaved-value" attribute of the <id> element in your hibernate configuration file to "any" for instance. It seems like it is set to "null".

Or the problem comes from your database parameters. With the identity generator, the db has to generate the id.
Does your table generates Id for new values?

regards
ju


Top
 Profile  
 
 Post subject: Re: Begginer problem with automaticly generating an id
PostPosted: Tue Apr 19, 2005 9:40 am 
Newbie

Joined: Mon Apr 18, 2005 7:23 am
Posts: 3
Location: Cologne, Germany
Yes thanx a lot !-)

It don't know about the "unsaved-value" attribute of the <id> stuff.
but the hint with the declaration at the table building sql seemed to make sense.

I change the column line:
Quote:
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,

it works.

I guess I would not have figured the problem alone. So thanx again...
Dirk


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.