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



View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3918877#3918877

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3918877


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to