-->
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.  [ 1 post ] 
Author Message
 Post subject: How to insert in databas for a many to many relationship
PostPosted: Wed Dec 22, 2004 3:31 am 
Beginner
Beginner

Joined: Tue Dec 21, 2004 3:18 am
Posts: 32
Hi all,
I don't to know how to insert in the database for a many-to-many relatioship.
I have 3 classes: Customer,Product and Vendor.
The reatioship between Customer and product is many to many.
I have 4 tables in the database: customer, product ,vendor and customerproduct which stores the values of the many to many relatioship between customer and product.

Can anyone tell me how could I insert values of customer id and product id in the table customerproduct.

Thanks in Advance.
Regards,
Sherihan.

Hibernate version:
2.1
Mapping documents:
1-Customer.hbm.xml:
<?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 package="hibernate2package">
<class name = "Customer" table ="customer">
<id name="cid" column ="cid">
<generator class ="native"/>
</id>
<property name="cname" column="name"/>
<property name="caddress" column="address"/>
<property name="ctel" column="tel"/>

<bag name="products" table="customerproduct" lazy="true" cascade="save-update">
<key column= "cid"/>
<many-to-many class="Product" column="pid"/>
</bag>
</class>
</hibernate-mapping>

2-Product.hbm.xml:
<?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 package="hibernate2package">

<class name="Product" table="product">
<id name = "pid" column ="pid">
<generator class="native"/>
</id>
<property name= "pname" column="name"/>
<property name="pdescription" column="description"/>
<!-- <bag name="customers" table ="customerproduct" lazy="true" inverse="true" cascade="save-update">
<key column= "pid"/>
<many-to-many class="Customer" column="cid"/>
</bag> -->
<many-to-one name="vendor" column="vid" class="Vendor"/>
</class>

</hibernate-mapping>

3-Vendor.hbm.xml:
<?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 package="hibernate2package">
<class name="Vendor" table="vendor">
<id name="vid" column="vid">
<generator class="native"/>
</id>
<property name="vname" column="name"/>
<property name="vaddress" column="address"/>
<property name="vtel" column="tel"/>
<bag name="products" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="vid"/>
<one-to-many class="Product"/>
</bag>
</class>
</hibernate-mapping>

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:
mysql server
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.