-->
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.  [ 1 post ] 
Author Message
 Post subject: SQL error, cannot insert id from IdentityGenerator
PostPosted: Thu Dec 02, 2004 8:15 pm 
Newbie

Joined: Thu Dec 02, 2004 7:24 pm
Posts: 3
Thanks in advance to anyone who can point me in the right direction.

I'm trying to use a custom UserType, LongIdType, as an id. (In case you're wondering, the type internally holds onto a long, which needs to be stored, and a class name, which does not.) So I wrote an IdentifierGenerator, LongIdGenerator (see code below), which simply delegates to the IdentityGenerator, which in turn always returns an empty string from generate.

When I try to save a new Gift object (id is null), I get the SQLException
shown below.

I've tried several different things that might provide hints:
1. I've mapped the id column as a long, used native generation and changed the type of Gift.id to primitive long, and everything works fine.

2. I've tried two differnent DDL statements for the id column, but the resulting behavior is the same (i.e. same SQLException).
Code:
   id BIGINT NOT NULL AUTO_INCREMENT

and
Code:
   id BIGINT NOT NULL


Two questions:
1. Why the exception?

2. If I'm not totally doing something weird, why do I need to write my
own identifier generator? Why can't I just use generator class="identity"?

Thanks again,
Rich

Hibernate version: 2.1.6

Mapping documents:
Code:
<class name="Gift" table="gift">
   <id name="id"
      type="org.mpr.common.hibernate.LongIdType"
      access="field">
      <generator class="org.mpr.common.hibernate.LongIdGenerator"/>
   </id>
   <property name="name" not-null="true"/>
</class>

Code between sessionFactory.openSession() and session.close():
Trying to create/insert a new Gift instance...
Code:
        Gift gift = new Gift();
        gift.setName("A real nice gift thingy");
        Session session = null;
        try {
            session = HibernateUtil.currentSession();
            if (session.isOpen()) {
                session.save(i);
                ...


My id generator gets called, and delegates to IdentityGenerator...
Code:
public class LongIdGenerator implements IdentifierGenerator {
    private IdentifierGenerator delegate;
   
    public LongIdGenerator() throws MappingException{
        delegate = IdentifierGeneratorFactory.create(
                                  "native",
                                  new LongType(),
                                  null,
                                  new MySQLDialect());
    }

    public Serializable generate(SessionImplementor session,
            Object object)
            throws SQLException, HibernateException {
        return delegate.generate(session, object); // always returns an empty string
    }
}



Full stack trace of any exception that occurs:
net.sf.hibernate.JDBCException: could not insert: [org.mpr.pledgetally.model.Gift]
Caused by: java.sql.SQLException: SQL String can not be NULL
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:110)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1401)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1336)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1436)
at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:370)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:415)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at net.sf.hibernate.util.GetGeneratedKeysHelper.prepareStatement(GetGeneratedKeysHelper.java:39)
at net.sf.hibernate.impl.BatcherImpl.getPreparedStatement(BatcherImpl.java:246)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:61)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:523)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:556)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:925)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:850)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:768)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:731)

... a bunch of stack frames that get me through Tomcat and Struts...

Name and version of the database you are using:
MySQL server version: 4.0.20a-nt

The generated SQL (show_sql=true):
show_sql is true, but no SQL generated[/code]


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

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.