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