-->
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.  [ 3 posts ] 
Author Message
 Post subject: Adding a list of entities where foreign key not known
PostPosted: Thu Jan 13, 2005 5:59 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
I am sure there is an answer to this but I am not even sure how to word the problem to search for it.

The problem is this. I create a Principal object along with a bunch of settings. Obviously the Principal does not have an id until it gets written to the database. But, when I try to send the Principal object, along with its settings to the database there is an error because the settings do not have the Principals ID in them.

As can be seen by the logs, the principals ID and the first settings ID get generated fine. It's when it tries to insert the setting into the database that there is a problem

Is there a way of adding an entity to the database, where the entity gets its ID from the database on addition as well as adding any sets that the entity may include using the generated ID?

Hibernate version:
The version that comes with JBoss 4.0.1.RC2. I think it is version 2.1

Mapping documents:
Two mapping documents. The first is a Principal. The second is the PRincipals settings which are in a set which uses a comparator for ordering.

<hibernate-mapping>

<class name="com.camp.common.config.user.Principal" table="tblPrincipals">

<id name="principalID" type="integer">
<column name="id_principal" sql-type="integer" not-null="true"/>
<generator class="sequence">
<param name="sequence">tblprincipals_id_principal_seq</param>
</generator>
</id>

<property name="username">
<column name="username" sql-type="char(16)" not-null="true" unique="true"/>
</property>

<property name="password">
<column name="password" sql-type="char(32)" not-null="true"/>
</property>

<set name="settings" sort="com.camp.common.config.user.UserConfigSettingComparator" order-by="id_setting asc" lazy="false" cascade="all" inverse="true">
<key column="fk_id_principal"/>
<one-to-many class="com.camp.common.config.user.UserConfigSetting"/>
</set>

</class>

</hibernate-mapping>

====================================================================

<hibernate-mapping>

<class name="com.camp.common.config.user.UserConfigSetting" table="tblUserConfig">

<id name="settingID" type="integer">
<column name="id_setting" sql-type="integer" not-null="true"/>
<generator class="sequence">
<param name="sequence">tbluserconfig_id_setting_seq</param>
</generator>
</id>

<property name="principalID">
<column name="fk_id_principal" sql-type="integer" not-null="true"/>
</property>

<property name="name">
<column name="setting_name" sql-type="char(32)" not-null="true"/>
</property>

<property name="description">
<column name="setting_description" sql-type="char(256)" not-null="false"/>
</property>

<property name="value">
<column name="setting_value" sql-type="char(256)" not-null="true"/>
</property>

<property name="type">
<column name="setting_type" sql-type="char(128)" not-null="true"/>
</property>


</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

//open the session
sess = sf.openSession();

// Create Transaction
t = sess.beginTransaction();

if (_operation.equals(Globals.ADD))
sess.save(principal);

//commit the transaction
t.commit();

sess.close();

Full stack trace of any exception that occurs:

14:02:43,613 INFO [STDOUT] Hibernate: select nextval ('tblprincipals_id_principal_seq')
14:02:43,618 INFO [STDOUT] Hibernate: select nextval ('tbluserconfig_id_setting_seq')
14:02:43,625 INFO [STDOUT] net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: com.camp.common.config.user.UserConfigSetting.principalID
at net.sf.hibernate.impl.SessionImpl.checkNullability(SessionImpl.java:1277)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:928)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:779)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:952)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:779)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at com.camp.server.config.configEJB.modifyConfigs(Unknown Source)
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.invocation.Invocation.performCall(Invocation.java:345)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:214)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.webservice.server.ServiceEndpointInterceptor.invoke(ServiceEndpointInterceptor.java:51)
at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:48)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:105)
at org.jboss.ejb.plugins.AbstractTxInterceptorBMT.invokeNext(AbstractTxInterceptorBMT.java:153)
at org.jboss.ejb.plugins.TxInterceptorBMT.invoke(TxInterceptorBMT.java:62)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:113)

Name and version of the database you are using:
Postgres 7.4

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 12:06 am 
Regular
Regular

Joined: Sat Aug 28, 2004 4:15 pm
Posts: 61
From your UserConfigSettings mapping :

Quote:
<property name="principalID">
<column name="fk_id_principal" sql-type="integer" not-null="true"/>
</property>



This is definitely NOT what you want. It is clear to me that you want a bidrectional relationship between a principal and settings. So, a principal has a one-to-many relation to its settings and a setting has a many-to-one relationship to its principal.

However, as I quoted above you have pricipleID because you were thinking relational when making your UserConfigSettings object. Your object really wants a Principal reference NOT a reference to an ID. So, get principalID out of that class, put a Principal refererence in there instead. Then, make your mapping a many-to-one from UserConfigSettings to Principal. Specify your cascade stuff. Watch out for inverse=true (READ FORUM ABOUT IT).

Hibernate manages persistent IDs for you if you let it.

Your error makes sense. You expected hibernate to fill some property in a class for you. It didnt fill it in so it was null and then you tried to save the null value on a column that you specified not-null=true on.

You really should go through the parent/child section of the reference manual. Read Hibernate in Action.

Anyway, good luck.


joe

_________________
Joe W


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 12:37 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
Thank you so much for the explanation. Dang, its hard trying to get out of the relational way of thinking.

Thanks again.


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