Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unidirectional one-to-many problem

 
Juzar Roopawalla
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i am using hibernate 3. I have two classes Opportunity & Project. The mapping files are :
<hibernate-mapping>
<class name="com.msdw.pbefficiency.servicestracker.Opportunity" table="OPPORTUNITY">
<id name="id" column="OPPID">
<generator class="identity"/>
</id>
<property name="name" column="NAME" not-null="true"/>
<set name="projectList" cascade="save-update" >
<key column="OPPID" />
<one-to-many class="com.msdw.pbefficiency.servicestracker.Project" />
</set>
</class>
</hibernate-mapping>

and for Project it is :

<hibernate-mapping>
<class name="com.msdw.pbefficiency.servicestracker.Project" table="PROJECT">
<id name="id" column="PID">
<generator class="identity"/>
</id>
<property name="name" column="NAME" not-null="true"/>

</class>
</hibernate-mapping>

The table PROJECT has an OPPID field which is a foreign key to OPPID in OPPORTUNITY table.

jsp has :
Opportunity o = new Opportunity();
o.setName("Opp1");
Set list = new HashSet();
o.setProjectList(list);
Project p = new Project();
p.setName("P1");
list.add(p);
session.saveOpp(o);

On the method call session.save(o) I get the following error:

7 Error org/hibernate/util/JDBCExceptionReporter The column OPPID in table PROJECT does not allow null values.

This means that the OPPID field in PROJECT is not being automatically populated by the OPPID which was generated for the OPPORTUNITY table. Isn't this what is expected from hibernate automatically?

Please let me know if I am missing something here. All examples which I have seen show bi-directional i.e. with inverse=true, so all the help I can get is definitely appreciated!!
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to this section of the Hibernate reference, you have to
specify a not-null="true" in the <key> element.
 
Juzar Roopawalla
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for your reply. the issue is resolved. somehow i overlooked that attribute...something tells me there will be many more queries to come in the very near future
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic