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

    Newly created identity not propagated to dependent columns i

    Robert Benkovitz Newbie

      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!!