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]