2 Replies Latest reply on Sep 14, 2007 4:39 AM by Frank Langelage

    Foreign keys with 0 and not NULL

    Frank Langelage Master

      We have a legacy application which is prepared to use NULL values for most datatypes (exception: date and timestamp).
      So a foreign key field of type integer is filled with a zero instead of null in the database.
      Using Entity Beans now with relations we get this:

      Caused by: javax.persistence.PersistenceException: javax.persistence.EntityNotFoundException: Unable to find biz.mbisoftware.fn.ejb.entity.MbiMsgss with id 0
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:527)
       at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:114)
       at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:247)
       at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:86)
       at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
       at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1382)
       ... 8 more
      Caused by: javax.persistence.EntityNotFoundException: Unable to find biz.mbisoftware.fn.ejb.entity.MbiMsgss with id 0
       at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:107)
       at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
       at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:72)
       at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
       at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:64)
       at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
       at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
       at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
       at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
       at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
       at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
       at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
       at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
       at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
       at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
       at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
       at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
       at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:515)
       ... 13 more
      


      Any hints how to deal with this?


        • 1. Re: Foreign keys with 0 and not NULL
          fhh Expert

          Use Integer instead of int as the property type.

          Regards

          Felix

          • 2. Re: Foreign keys with 0 and not NULL
            Frank Langelage Master

            The ID-field of MbiMsgss is of type Integer:

            @Entity
            @Table(name="mbi_msgss")
            public class MbiMsgss implements java.io.Serializable
            {
             @Id
             @GeneratedValue(strategy=GenerationType.IDENTITY)
             @Column(name="msgss_serial")
             private java.lang.Integer msgssSerial;
             ...
            


            The problem is, that our legacy app fills in a zero instead of a null into the database table field which works as a foreign key field.
            So when I access the entity which has a OneToOne relation (uni-directional) to the above, I get that exception.
            @Entity
            @IdClass(value=MbiMsgdi.PK.class)
            @Table(name="mbi_msgdi")
            public class MbiMsgdi implements java.io.Serializable
            {
             @Id
             @Column(name="bp_id")
             private java.lang.Integer bpId;
            
             @Id
             @Column(name="seq_no")
             private java.lang.Integer seqNo;
             ....
            
             @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
             @JoinColumns({
             @JoinColumn(name="msgss_serial",referencedColumnName="msgss_serial",insertable=false,updatable=false)
             })
             private MbiMsgss mbiMsgss;
             ...