3 Replies Latest reply on Jan 23, 2006 5:46 AM by martinganserer

    Newly created identity not propagated to dependent columns i

      Hello:

      It seems like a simple problem, but I can't get it to work. I have two entites, related by a OneToMany relationship:

      @Entity
      public class WidgetHolder {
      
       @Id (generate=GeneratorType.IDENTITY)
       private long id;
      
      
       @OneToMany(mappedBy="myWidgetHolder", cascade=CascadeType.ALL)
       private List<Widget> myWidgets = new ArrayList<Widgets>();
      
      
       public void addWidget(Widget aWidget) {
       myWidgets.add(aWidget);
       }
      }
      
      
      @Entity
      public class Widget {
      
       ...
      
       @ManyToOne(optional=false)
       @JoinColumn(name="WIDGET_HOLDER_ID", nullable=false, updatable=false)
       private WidgetHolder myWidgetHolder;
      
      
       public Widget(WidgetHolder aHolder) {
       myWidgetHolder = aHolder;
       }
      
      
       ...
      
      }
      


      When I do the following in a SLSB:

      public class StatelessBean {
      
       @PersistenceContext (unitName="widgetContext")
       protected EntityManager em;
      
       public void createWidgetHolder() {
      
       WidgetHolder widgetHolder = new WidgetHolder();
      
       List<Widget> widgets = new ArrayList<Widget>();
       widgetHolder.addWidget(new Widget(widgetHolder))
       widgetHolder.addWidget(new Widget(widgetHolder));
      
       ...
      
       em.persist(widgetHolder);
       }
      }
      


      I get the following error:

      java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=VELOCITY1/39, BranchQual=, localId=39] status=STATUS_NO_TRANSACTION; - nested throwable: (org.hibernate.PropertyValueException: not-null property references a null or transient value: Widget.myWidgetHolder)

      When I take out the "optional=false" and "nullable=false" on the ManyToOne relationship, the INSERT statement for Widget tries to insert a NULL in the WIDGET_HOLDER_ID column. (I use MSSQL 2000 as the datastore).

      Is there some sort of annotation I need to explicitly tell Hibernate to get the newly-created ID from WidgetStore and use it in the INSERT of Widget? Any help would be appreciated - thanks!!



        • 1. Re: Newly created identity not propagated to dependent colum
          mwoelke

          I think your error is, that all your annotation are connected to the fields.
          if you try changing the way the container is accessing your annotations (@Entity(access=...)) it sould be working. Of course you could put your annotations to the properties as well.

          @Id(generate=GeneratorType.AUTO)
          public int getId(){
          return id;
          }
          


          hope i could help you.

          regards, milan wölke

          • 2. Re: Newly created identity not propagated to dependent colum

            Thanks for the reply. I left out (access=AccessType.FIELD) in the @Entity annotation - I do use that.

            I'll try and explain further - it seems that JBoss/Hibernate are generating the correct INSERT statements for the Entities, but it is not propagating the newly created Identity value for the WidgetHolder row to the Widget rows. Using the trace, I see it is properly formatting the inserts, and even adding a "select scope_identity()" to the end of the insert statement to have SQL Server return the newly created ID - however, Hibernate is not using that value in the subsequent INSERT statement for the Widgets. Very frustrating.

            • 3. Re: Newly created identity not propagated to dependent colum
              martinganserer

              Hello,

              I think it won't work as long as you try to perist a whole list.
              Try this:

              for(Widget widget: widgetHolder)
              {
               em.persist(widget);
              }