What i want to do :
Product -> Parts ( many to many association with a join table ).
Join table -> Product_Parts;
My Product_Parts class :
Code:
public class ProductParts implements Serializable{
public static class Id implements Serializable{
private Long productId;
private Long partId;
public Id(){
// nothing here
}
// constructor - this sets up Product and parts foreign key relationship
public Id(Long productId, Long partId){
this.partId = partId;
this.productId = productId;
}
}
private Id id = new Id();
private Product product;
private Part part;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Part getPart() {
return part;
}
public void setPart(Part part) {
this.part = part;
}
// default constructor
public ProductParts(){
// nothing here
}
// constructor for this class, sets up the relationship ( foreign key relationship that is )
public ProductParts(Product product, Part part){
this.part = part;
this.product = product;
// set the composite key like this
id.partId = part.getId();
id.productId = product.getId();
}
then i run the following test :
Code:
public class ClassicProductToPartsTestWithCompositeId {
static{
BasicConfigurator.configure();
}
public static void main(String[] args) {
Set<ProductParts> productParts = new HashSet<ProductParts>();
Session session = new Configuration().configure().buildSessionFactory().openSession();
session.beginTransaction();
Product product0 = new Product();
product0.setId(108L);
product0.setName("Shiva");
Part part1 = new Part();
part1.setId(11L);
part1.setName("AUM");
ProductParts newLink = new ProductParts(product0,part1);
productParts.add(newLink);
product0.setProductParts(productParts);
part1.setProductParts(productParts);
session.save(product0);
session.getTransaction().commit();
session.clear();
}
My Product_Part Mapping is as follows :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="beans">
<class name="ProductParts" table="Product_Parts">
<composite-id class="ProductParts$Id">
<key-property name="product_id" column="product_id" type="long" />
<key-property name="part_id" column="part_id" type="long" />
</composite-id>
<many-to-one name="product" class="Product" not-null="true" insert="false" update="false" />
<many-to-one name="part" class="Part" not-null="true" insert="false" update="false" />
</class>
</hibernate-mapping>
I get this exception :
Code:
69 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: beans.ProductParts -> Product_Parts
370 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: product_id -> product_id
370 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: part_id -> part_id
371 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: product -> product
371 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: part -> part
371 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
371 [main] DEBUG org.hibernate.cfg.Configuration - properties: {hibernate.connection.********, java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files (x86)\Java\jre1.6.0_02\bin, java.vm.version=1.6.0_02-b05, hibernate.connection.username=system, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=, java.vm.specification.name=Java Virtual Machine Specification, user.dir=Z:\DEFAULT_WORKSPACE\HibernateRefresher_Advanced_Associations, java.runtime.version=1.6.0_02-b05, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, hbm2ddl.auto=create, java.endorsed.dirs=C:\Program Files (x86)\Java\jre1.6.0_02\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\Users\rudra\AppData\Local\Temp\, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows Vista, sun.jnu.encoding=Cp1252, java.library.path=C:\Program Files (x86)\Java\jre1.6.0_02\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Java\jdk1.6.0_02\bin\..\jre\bin\client;C:\Program Files (x86)\Java\jdk1.6.0_02\bin\..\jre\bin;C:\Program Files (x86)\Java\jdk1.6.0_02\bin;c:\ant\bin;z:\maven\bin;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\jboss-4.2.0.GA\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\hp\bin\Python;c:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;c:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;c:\Program Files (x86)\Common Files\Roxio Shared\9.0\DLLShared\;;;C:\Program Files (x86)\Subversion\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\CVSNT\, hibernate.myeclipse.connection.profile=OracleDriver, java.specification.name=Java Platform API Specification, java.class.version=50.0, sun.management.compiler=HotSpot Client Compiler, os.version=6.0, connection.password=shiva010, user.home=C:\Users\rudra, user.timezone=, connection.username=system, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.6, hibernate.format_sql=true, myeclipse.connection.profile=OracleDriver, hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, user.name=rudra, java.class.path=Z:\DEFAULT_WORKSPACE\HibernateRefresher_Advanced_Associations\bin;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\antlr-2.7.6rc1.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\asm.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\asm-attrs.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\cglib-2.1.3.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\commons-collections-2.1.1.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\commons-logging-1.0.4.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\dom4j-1.6.1.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\ehcache-1.1.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\hibernate3.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\jaas.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\jaxen-1.1-beta-7.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\jdbc2_0-stdext.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\jta.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\log4j-1.2.11.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\xerces-2.6.2.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\xml-apis.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\c3p0-0.9.0.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\concurrent-1.3.2.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\connector.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\optional\jboss-cache.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\optional\jboss-common.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\optional\jboss-jmx.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\optional\jboss-system.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\jacc-1_0-fr.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.1\lib\jgroups-2.2.8.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\oscache-2.1.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\proxool-0.8.3.jar;C:\MyEclipse 6.0\eclipse\plugins\com.genuitec.org.hibernate.eclipse_6.0.1.zmyeclipse601200710\myeclipse-data\3.0\lib\swarmcache-1.0rc2.jar;Z:\DEFAULT_WORKSPACE\HibernateRefresher_Advanced_Associations\lib\ojdbc14.jar;Z:\DEFAULT_WORKSPACE\HibernateRefresher_Advanced_Associations\lib\ojdbc14_g.jar, hibernate.show_sql=true, java.vm.specification.version=1.0, java.home=C:\Program Files (x86)\Java\jre1.6.0_02, sun.arch.data.model=32, hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:XE, hibernate.dialect=org.hibernate.dialect.Oracle9Dialect, user.language=en, java.specification.vendor=Sun Microsystems Inc., awt.toolkit=sun.awt.windows.WToolkit, hibernate.cglib.use_reflection_optimizer=true, java.vm.info=mixed mode, java.version=1.6.0_02, java.ext.dirs=C:\Program Files (x86)\Java\jre1.6.0_02\lib\ext;C:\Windows\Sun\Java\lib\ext, sun.boot.class.path=C:\Program Files (x86)\Java\jre1.6.0_02\lib\resources.jar;C:\Program Files (x86)\Java\jre1.6.0_02\lib\rt.jar;C:\Program Files (x86)\Java\jre1.6.0_02\lib\sunrsasign.jar;C:\Program Files (x86)\Java\jre1.6.0_02\lib\jsse.jar;C:\Program Files (x86)\Java\jre1.6.0_02\lib\jce.jar;C:\Program Files (x86)\Java\jre1.6.0_02\lib\charsets.jar;C:\Program Files (x86)\Java\jre1.6.0_02\classes, java.vendor=Sun Microsystems Inc., connection.driver_class=oracle.jdbc.driver.OracleDriver, file.separator=\, hibernate.hbm2ddl.auto=create, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, connection.url=jdbc:oracle:thin:@localhost:1521:XE, dialect=org.hibernate.dialect.Oracle9Dialect, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
371 [main] DEBUG org.hibernate.cfg.Configuration - Preparing to build session factory with filters : {}
371 [main] DEBUG org.hibernate.cfg.Configuration - processing extends queue
372 [main] DEBUG org.hibernate.cfg.Configuration - processing collection mappings
372 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Second pass for collection: beans.Product.parts
372 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: beans.Product.parts -> Product_Parts
373 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Mapped collection key: id, one-to-many: beans.ProductParts
373 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Second pass for collection: beans.Part.products
373 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Mapped collection key: id, element: elt
373 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Second pass for collection: beans.Cat.kittens
373 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: beans.Cat.kittens -> CAT
373 [main] DEBUG org.hibernate.cfg.CollectionSecondPass - Mapped collection key: ID, one-to-many: beans.Cat
373 [main] DEBUG org.hibernate.cfg.Configuration - processing native query and ResultSetMapping mappings
373 [main] DEBUG org.hibernate.cfg.Configuration - processing association property references
373 [main] DEBUG org.hibernate.cfg.Configuration - processing foreign key constraints
373 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: beans.Part
374 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: beans.ProductParts
Exception in thread "main" org.hibernate.MappingException: Foreign key (FK42C3C750BF009549:PRODUCT_PARTS [elt])) must have same number of columns as the referenced primary key (Product_Parts [product_id,part_id])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1145)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1052)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)
at beans.test.ClassicProductToPartsTestWithCompositeId.main(ClassicProductToPartsTestWithCompositeId.java:41)
Cant figure out whats the problem ... ?
Regards
Vyas, Anirudh[/code]