File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Many-To-Many problem in Hibernate 3.0 beta 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Many-To-Many problem in Hibernate 3.0 beta 1" Watch "Many-To-Many problem in Hibernate 3.0 beta 1" New topic
Author

Many-To-Many problem in Hibernate 3.0 beta 1

girish bajaj
Greenhorn

Joined: Jan 28, 2005
Posts: 2
Problem: I have a bi directional many to many relationship with two tables that i am trying to insert new data into via hibernate 3.0 beta 1. The table schema along with the association table schema is below:

Table8
-------
PK:table8pk int
table8desc char(10)

Table9
------
PK:table9pk int
table9desc char(10)

Table10 (association table)
-------
FK:table8pk
FK:table9pk


Here are the mapping files:

Table8
------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="com.tietronix.hibernateTest.dal.Table8"
table="Table8"
>

<id
name="table8pk"
type="java.lang.Integer"
column="table8pk"
unsaved-value="0"
>
<generator class="identity" />
</id>

<property
name="table8desc"
type="java.lang.String"
column="table8desc"
length="10"
/>

<!-- Associations -->

<!-- bi-directional one-to-many association to Table10 -->
<set
name="table9s"
lazy="true"
table="Table10"
cascade="all"
>
<key>
<column name="table8pk" />
</key>
<many-to-many
column="table9pk"
class="com.tietronix.hibernateTest.dal.Table9"
/>
</set>

</class>
</hibernate-mapping>





Table9
--------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="com.tietronix.hibernateTest.dal.Table9"
table="Table9"
>

<id
name="table9pk"
type="java.lang.Integer"
column="table9pk"
unsaved-value="0"
>
<generator class="identity" />
</id>

<property
name="table9desc"
type="java.lang.String"
column="table9desc"
length="10"
/>

<!-- Associations -->

<!-- bi-directional one-to-many association to Table10 -->
<set
name="table8s"
lazy="true"
table="Table10"
inverse="true"
cascade="all"
>
<key>
<column name="table9pk" />
</key>
<many-to-many
column="table8pk"
class="com.tietronix.hibernateTest.dal.Table8"
/>
</set>

</class>
</hibernate-mapping>



Heres the code Im trying to execute:
-------------------------------------


org.hibernate.Session session2 = HibernateUtil.currentSession();

Transaction tx = session2.beginTransaction();

Set myTable8Sets = new HashSet();
Set myTable9Sets = new HashSet();

Table8 tbl8_1 = new Table8();
tbl8_1.setTable8desc("CALENDAR");
myTable8Sets.add(tbl8_1);


Table9 tbl9_1 = new Table9();
tbl9_1.setTable9desc("MONTHS");
myTable9Sets.add(tbl8_1);


tbl8_1.setTable9s(myTable9Sets);
tbl9_1.setTable8s(myTable8Sets);

session2.save(tbl9_1);

session2.flush();
tx.commit();

-----------------------
Heres the ERROR message IM getting!! Can someone help me with this problem? Thanks!
-------------------------

22:03:58,244 WARN [JDBCExceptionReporter] SQL Error: 515, SQLState: 23000
22:03:58,244 ERROR [JDBCExceptionReporter] Cannot insert the value NULL into column 'table9pk', table 'hibernateTest.dbo.Table9'; column does not allow nulls. INSERT fails.
22:03:58,275 ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.hibernate.exception.ConstraintViolationException: could not insert: [com.tietronix.hibernateTest.dal.Table9]


Appreciate anyones help. esp Davids Hey, I also tried session2.save(tbl8_1), but it didnt work.

Thanks,
Girish
girish bajaj
Greenhorn

Joined: Jan 28, 2005
Posts: 2
thanks for reading david, but i figured zee problem out. i had stupidly forgotten to create identities for my tables... so pse forgive my ignorance.

for the other ppl who have read this - i thank you loads as well.

thanks,
girish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Many-To-Many problem in Hibernate 3.0 beta 1