-->
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.  [ 5 posts ] 
Author Message
 Post subject: Nulls in Insert (Hibernate 2.1.1, JBoss 3.2.1, HSQLDB)
PostPosted: Sun Jan 18, 2004 5:20 am 
Newbie

Joined: Fri Jan 16, 2004 2:37 am
Posts: 5
Location: Columbus, Ohio
Hi, I'm getting a "java.sql.SQLException: Try to insert null into a non-nullable column in statement" in my very simple application. I have a POJO with an Integer PK that looks like:

Code:
/**
* @hibernate.class
*     table="conferences"
*/
public class Conference implements Serializable {
...   
   /**
    * @hibernate.id
    *     column="PK_ID"
    *     generator-class="native"     
    */      
   public Integer getId() {
      return id;
   }
...
}


From a SessionBean I'm trying to save an object of the type shown above

Code:
/**
    * @ejb.interface-method
    * @ejb.transaction
    *      type="NotSupported"
    *
    * @return
   */   
   public boolean saveConference(Conference conference) {
      boolean result = true;
       
        System.out.println("Saving " + conference);
       
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.save(conference);
        } catch (HibernateException hex) {
            result = false;
            throw new EJBException("Could not retrieve list of conferences", hex);
        } finally {
            if (session != null) {           
                try {
                    session.close();
                } catch (HibernateException e) {
                    // do nothing, just log it
                }
            }
        }       
       
      return result;
   }



It doesn't matter if I set the value of the id field or not, I always get the exception. From the println added to the code above I can see that the value is in the object right before the call to session.save

The server output and stack trace is shown below.

Any help would be greatly appreaciated.


04:06:03,320 INFO [STDOUT] Saving [Conference] id = null, name = Apress OSC, description = Apress Open Source Conference, st
art date = Sun Jan 18 04:06:03 EST 2004, end date = Sun Jan 18 04:06:03 EST 2004, tracks = 3
04:06:03,330 INFO [STDOUT] Hibernate: insert into conferences (name, description, startDate, endDate, abstractSubmissionStar
tDate, abstractSubmissionEndDate, PK_ID) values (?, ?, ?, ?, ?, ?, null)
04:06:03,340 WARN [JDBCExceptionReporter] SQL Error: -10, SQLState: 23000
04:06:03,340 ERROR [JDBCExceptionReporter] Try to insert null into a non-nullable column in statement [insert into conference
s (name, description, startDate, endDate, abstractSubmissionStartDate, abstractSubmissionEndDate, PK_ID) values ('Apress OSC'
, 'Apress Open Source Conference', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18
04:06:03.29', null)]
04:06:03,340 WARN [JDBCExceptionReporter] SQL Error: -10, SQLState: 23000
04:06:03,340 ERROR [JDBCExceptionReporter] Try to insert null into a non-nullable column in statement [insert into conference
s (name, description, startDate, endDate, abstractSubmissionStartDate, abstractSubmissionEndDate, PK_ID) values ('Apress OSC'
, 'Apress Open Source Conference', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18
04:06:03.29', null)]
04:06:03,340 ERROR [JDBCExceptionReporter] could not insert: [com.ejdoab.pojos.Conference]
java.sql.SQLException: Try to insert null into a non-nullable column in statement [insert into conferences (name, description
, startDate, endDate, abstractSubmissionStartDate, abstractSubmissionEndDate, PK_ID) values ('Apress OSC', 'Apress Open Sourc
e Conference', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', null)]

at org.hsqldb.Trace.getError(Unknown Source)
at org.hsqldb.Result.<init>(Unknown Source)
at org.hsqldb.jdbcConnection.executeHSQL(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 org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:308)
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 com.ejdoab.beans.ConferenceHibernateFacadeBean.saveConference(ConferenceHibernateFacadeBean.java:127)
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 org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:629)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:216)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:104)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:117)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:322)
at org.jboss.ejb.Container.invoke(Container.java:674)
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 org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:359)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:534)
04:06:03,340 ERROR [LogInterceptor] EJBException, causedBy:
net.sf.hibernate.JDBCException: could not insert: [com.ejdoab.pojos.Conference]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:541)
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 com.ejdoab.beans.ConferenceHibernateFacadeBean.saveConference(ConferenceHibernateFacadeBean.java:127)
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 org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:629)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:216)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:104)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:117)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:322)
at org.jboss.ejb.Container.invoke(Container.java:674)
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 org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:359)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: Try to insert null into a non-nullable column in statement [insert into conferences (name,
description, startDate, endDate, abstractSubmissionStartDate, abstractSubmissionEndDate, PK_ID) values ('Apress OSC', 'Apress
Open Source Conference', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.29', '2004-01-18 04:06:03.
29', null)]
at org.hsqldb.Trace.getError(Unknown Source)
at org.hsqldb.Result.<init>(Unknown Source)
at org.hsqldb.jdbcConnection.executeHSQL(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 org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:308)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:502)
... 39 more


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2004 5:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Have you declared the primary key column as IDENTITY when creating the table?


Top
 Profile  
 
 Post subject: null in insert
PostPosted: Sun Jan 18, 2004 6:29 am 
Newbie

Joined: Fri Jan 16, 2004 2:37 am
Posts: 5
Location: Columbus, Ohio
gloeglm wrote:
Have you declared the primary key column as IDENTITY when creating the table?


No, but I just read about the IDENTITY columns and the HSQLDB bug, so I switched from "native" to "increment":

Code:
   /**
    * @hibernate.id
    *     column="PK_ID"
    *     generator-class="increment"     
    */      
   public Integer getId() {
      return id;
   }


Now, I don't get any exceptions but when I check the database there is no data in there. Here's a snippet of the client code:

Code:
    Conference newConf =
        new Conference(   "My Conf", "My Conf Description", new Date(), new Date(), new Date(),   new Date());
newConf.addTrack(new Track("J2SE","Java 2 Standard Edition","Learn how to build powerful Java desktop applications"));
...
boolean result = conf.saveConference(newConf);


and here's what I see in the console (I have the showSql param on but I don't see any insert statements now!!). Do I need to explicitely demarcate the transactions, even though I'm in a managed environment?

Code:
05:13:14,697 INFO  [Configuration] Mapping resource: com/ejdoab/pojos/Track.hbm.xml
05:13:14,807 INFO  [Binder] Mapping class: com.ejdoab.pojos.Track -> tracks
05:13:14,997 INFO  [Configuration] Mapping resource: com/ejdoab/pojos/Conference.hbm.xml
05:13:15,067 INFO  [Binder] Mapping class: com.ejdoab.pojos.Conference -> conferences
05:13:15,087 INFO  [Configuration] processing one-to-many association mappings
05:13:15,087 INFO  [Binder] Mapping collection: com.ejdoab.pojos.Conference.tracks -> tracks
05:13:15,087 INFO  [Configuration] processing one-to-one association property references
05:13:15,087 INFO  [Configuration] processing foreign key constraints
05:13:15,127 INFO  [Dialect] Using dialect: net.sf.hibernate.dialect.HSQLDialect
05:13:15,127 INFO  [SettingsFactory] Maximim outer join fetch depth: 1
05:13:15,127 INFO  [SettingsFactory] Use outer join fetching: true
05:13:15,137 INFO  [NamingHelper] JNDI InitialContext properties:{}
05:13:15,137 INFO  [DatasourceConnectionProvider] Using datasource: java:/tcmsDS
05:13:15,137 INFO  [TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
05:13:15,148 INFO  [NamingHelper] JNDI InitialContext properties:{}
05:13:15,148 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBo
ssTransactionManagerLookup
05:13:15,148 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
05:13:15,148 INFO  [NamingHelper] JNDI InitialContext properties:{}
05:13:15,148 INFO  [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBo
ssTransactionManagerLookup
05:13:15,158 INFO  [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
05:13:15,168 INFO  [SettingsFactory] Use scrollable result sets: true
05:13:15,168 INFO  [SettingsFactory] echoing all SQL to stdout
05:13:15,168 INFO  [SettingsFactory] Query language substitutions: {no='N', true=1, yes='Y', false=0}
05:13:15,168 INFO  [SettingsFactory] cache provider: net.sf.hibernate.cache.HashtableCacheProvider
05:13:15,178 INFO  [Configuration] instantiating and configuring caches
05:13:15,348 INFO  [SessionFactoryImpl] building session factory
05:13:15,748 INFO  [SessionFactoryObjectFactory] Factory name: java:/hibernate/HibernateFactory
05:13:15,748 INFO  [NamingHelper] JNDI InitialContext properties:{}
05:13:15,748 INFO  [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/hibernate/HibernateFactory
05:13:15,748 WARN  [SessionFactoryObjectFactory] InitialContext did not implement EventContext
05:13:15,748 INFO  [NamingHelper] JNDI InitialContext properties:{}
05:13:15,869 INFO  [STDOUT] Hibernate: select conferences.PK_ID as PK_ID, conferences.name as name, conferences.description a
s descript3_, conferences.startDate as startDate, conferences.endDate as endDate, conferences.abstractSubmissionStartDate as
abstract6_, conferences.abstractSubmissionEndDate as abstract7_ from conferences conferences
05:13:15,939 INFO  [STDOUT] Hibernate: select this.PK_ID as PK_ID0_, this.name as name0_, this.description as descript3_0_, t
his.startDate as startDate0_, this.endDate as endDate0_, this.abstractSubmissionStartDate as abstract6_0_, this.abstractSubmi
ssionEndDate as abstract7_0_ from conferences this where (this.description=?)
[b]05:13:15,999 INFO  [STDOUT] Saving [Conference] id = null, name = Apress OSC, description = Apress Open Source Conference, st
art date = Sun Jan 18 05:13:15 EST 2004, end date = Sun Jan 18 05:13:15 EST 2004, tracks = 3
05:13:16,009 INFO  [STDOUT] **** setID was called with value 1****[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2004 6:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Call session.flush() at the end


Top
 Profile  
 
 Post subject: Got it!
PostPosted: Sun Jan 18, 2004 1:59 pm 
Newbie

Joined: Fri Jan 16, 2004 2:37 am
Posts: 5
Location: Columbus, Ohio
gloeglm wrote:
Call session.flush() at the end


Doh! Thanks, that did it!


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