Hi Guys,
I am a newbie, so I apologise for this question. I have two tables defined customer and billingaddress. The billingaddress has a customerId which relates to customer.id. I want the customer class to have a reference to its billingaddress which is just one object. I am struggling to get the mapping defined correctly.
Hibernate version:
3.0.5
Mapping documents:
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="src">
<class name="Customer" table="customer">
<id name="id" column="id" type="integer" unsaved-value="null">
<generator class="native"/>
</id>
<property name="firstname" type="string" length="45"/>
<property name="lastname" type="string" length="45"/>
<property name="gender" type="string" length="1"/>
<many-to-one name="billingAddress" class="src.BillingAddress" column="id"/>
</class>
</hibernate-mapping>
BillingAddress.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="src">
<class name="BillingAddress" table="billingaddress">
<id name="id" column="id" type="integer" unsaved-value="null">
<generator class="native"/>
</id>
<property name="line1" type="string" length="45"/>
<property name="line2" type="string" length="45"/>
<property name="line3" type="string" length="45"/>
<property name="line4" type="string" length="45"/>
<property name="line5" type="string" length="45"/>
<property name="customerId" type="integer"/>
<set name="customer" cascade="all" inverse="true" lazy="true">
<key column="id"/>
<one-to-many class="src.Customer"/>
</set>
</class>
<query name="src.getBillingAddressByCustomerId">
<![CDATA[
from src.BillingAddress as billingAddress
where billingAddress.customerId = ?
]]>
</query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
17:57:34,809 INFO Environment:464 - Hibernate 3.0.5
17:57:34,839 INFO Environment:477 - hibernate.properties not found
17:57:34,859 INFO Environment:510 - using CGLIB reflection optimizer
17:57:34,859 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
17:57:35,239 INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
17:57:35,249 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
17:57:36,291 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... on-3.0.dtd in classpath under org/hibernate/
17:57:36,301 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... on-3.0.dtd in classpath
17:57:36,481 DEBUG Configuration:1067 - dialect=org.hibernate.dialect.MySQLDialect
17:57:36,481 DEBUG Configuration:1067 - connection.driver_class=com.mysql.jdbc.Driver
17:57:36,481 DEBUG Configuration:1067 - connection.url=jdbc:mysql://localhost/practicum
17:57:36,481 DEBUG Configuration:1067 - connection.username=practicum
17:57:36,491 DEBUG Configuration:1067 - connection.password=practicum
17:57:36,491 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@d3c65d [Attribute: name resource value "src/Customer.hbm.xml"]
17:57:36,491 INFO Configuration:444 - Mapping resource: src/Customer.hbm.xml
17:57:36,511 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
17:57:36,521 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
17:57:36,952 INFO HbmBinder:260 - Mapping class: src.Customer -> customer
17:57:36,982 DEBUG HbmBinder:1099 - Mapped property: id -> id
17:57:37,032 DEBUG HbmBinder:1099 - Mapped property: firstname -> firstname
17:57:37,042 DEBUG HbmBinder:1099 - Mapped property: lastname -> lastname
17:57:37,042 DEBUG HbmBinder:1099 - Mapped property: gender -> gender
17:57:37,342 DEBUG HbmBinder:1099 - Mapped property: billingAddress -> id
17:57:37,342 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@19a37a [Attribute: name resource value "src/BillingAddress.hbm.xml"]
17:57:37,342 INFO Configuration:444 - Mapping resource: src/BillingAddress.hbm.xml
17:57:37,362 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
17:57:37,362 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
17:57:37,653 INFO HbmBinder:260 - Mapping class: src.BillingAddress -> billingaddress
17:57:37,663 DEBUG HbmBinder:1099 - Mapped property: id -> id
17:57:37,663 DEBUG HbmBinder:1099 - Mapped property: line1 -> line1
17:57:37,673 DEBUG HbmBinder:1099 - Mapped property: line2 -> line2
17:57:37,673 DEBUG HbmBinder:1099 - Mapped property: line3 -> line3
17:57:37,673 DEBUG HbmBinder:1099 - Mapped property: line4 -> line4
17:57:37,683 DEBUG HbmBinder:1099 - Mapped property: line5 -> line5
17:57:37,683 DEBUG HbmBinder:1099 - Mapped property: customerId -> customerId
17:57:37,713 DEBUG HbmBinder:1099 - Mapped property: customer
17:57:37,733 DEBUG HbmBinder:2233 - Named query: src.getBillingAddressByCustomerId ->
from src.BillingAddress as billingAddress
where billingAddress.customerId = ?
17:57:37,733 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@1dcc2a3 [Attribute: name resource value "src/ContactDetail.hbm.xml"]
17:57:37,743 INFO Configuration:444 - Mapping resource: src/ContactDetail.hbm.xml
17:57:37,743 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
17:57:37,763 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
17:57:37,873 INFO HbmBinder:260 - Mapping class: src.ContactDetail -> contactdetails
17:57:37,883 DEBUG HbmBinder:1099 - Mapped property: id -> id
17:57:37,883 DEBUG HbmBinder:1099 - Mapped property: homeNumber -> homeNumber
17:57:37,883 DEBUG HbmBinder:1099 - Mapped property: workNumber -> workNumber
17:57:37,883 DEBUG HbmBinder:1099 - Mapped property: mobileNumber -> mobileNumber
17:57:37,893 DEBUG HbmBinder:1099 - Mapped property: faxNumber -> faxNumber
17:57:37,893 DEBUG HbmBinder:1099 - Mapped property: emailAddress -> emailAddress
17:57:37,903 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@943dc4 [Attribute: name resource value "src/Order.hbm.xml"]
17:57:37,903 INFO Configuration:444 - Mapping resource: src/Order.hbm.xml
17:57:37,903 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
17:57:37,923 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
17:57:38,033 INFO HbmBinder:260 - Mapping class: src.Order -> order
17:57:38,033 DEBUG HbmBinder:1099 - Mapped property: id -> id
17:57:38,033 DEBUG HbmBinder:1099 - Mapped property: orderNumber -> orderNumber
17:57:38,043 DEBUG HbmBinder:1099 - Mapped property: datePlaced -> datePlaced
17:57:38,063 DEBUG HbmBinder:1099 - Mapped property: shippedDate -> shippedDate
17:57:38,063 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@6db724 [Attribute: name resource value "src/OrderItem.hbm.xml"]
17:57:38,063 INFO Configuration:444 - Mapping resource: src/OrderItem.hbm.xml
17:57:38,083 DEBUG DTDEntityResolver:42 - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
17:57:38,093 DEBUG DTDEntityResolver:53 - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
17:57:38,143 INFO HbmBinder:260 - Mapping class: src.OrderItem -> orderitem
17:57:38,153 DEBUG HbmBinder:1099 - Mapped property: id -> id
17:57:38,153 DEBUG HbmBinder:1099 - Mapped property: product -> product
17:57:38,163 DEBUG HbmBinder:1099 - Mapped property: price -> price
17:57:38,163 INFO Configuration:1222 - Configured SessionFactory: null
17:57:38,163 DEBUG Configuration:1223 - properties: {hibernate.connection.password=practicum, java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=C:\opt\java\jdk1.5.0_04\jre\bin, java.vm.version=1.5.0_04-b05, hibernate.connection.username=practicum, 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.os.patch.level=Service Pack 4, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\usr\M.Sc\Practicum\Development\example\hibernate, java.runtime.version=1.5.0_04-b05, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\opt\java\jdk1.5.0_04\jre\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows 2000, sun.jnu.encoding=Cp1252, java.library.path=C:\opt\java\jdk1.5.0_04\bin;.;C:\WINNT\system32;C:\WINNT;C:\opt\Perl\bin\;C:\opt\oracle\product\oracle902\bin;C:\opt\java\jdk1.5.0_04\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\opt\perforce;C:\opt\ant\1.6.5\bin\;C:\opt\java\Sun\AppServer\bin;C:\opt\java\jad1.5.8, java.specification.name=Java Platform API Specification, java.class.version=49.0, sun.management.compiler=HotSpot Client Compiler, os.version=5.0, connection.password=practicum, user.home=C:\Documents and Settings\Administrator, user.timezone=Europe/London, connection.username=practicum, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.5, hibernate.connection.driver_class=com.mysql.jdbc.Driver, user.name=Administrator, java.class.path=C:\usr\M.Sc\Practicum\Development\example\hibernate;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\antlr-2.7.5H3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-antlr-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-junit-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-launcher-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ant-swing-1.6.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\asm.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\asm-attrs.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\c3p0-0.8.5.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\cglib-2.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\cleanimports.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\commons-collections-2.1.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\commons-logging-1.0.4.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\concurrent-1.3.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\connector.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\dom4j-1.6.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\ehcache-1.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jaas.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jacc-1_0-fr.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jaxen-1.1-beta-4.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-cache.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-common.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-jmx.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jboss-system.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jdbc2_0-stdext.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jgroups-2.2.7.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\jta.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\junit-3.8.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\log4j-1.2.9.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\oscache-2.1.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\proxool-0.8.3.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\swarmcache-1.0rc2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\versioncheck.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\xerces-2.6.2.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\lib\xml-apis.jar;C:\opt\tomcat\5.5.9\common\lib\servlet-api.jar;C:\opt\tomcat\5.5.9\common\lib\mcj317.jar;C:\usr\M.Sc\Practicum\Development\hibernate\hibernate-3.0\hibernate3.jar;/c:/opt/java/eclipse/3.1/eclipse/plugins/org.eclipse.jdt.junit_3.1.0/junitsupport.jar;/c:/opt/java/eclipse/3.1/eclipse/plugins/org.eclipse.jdt.junit.runtime_3.1.0/junitruntime.jar, java.vm.specification.version=1.0, java.home=C:\opt\java\jdk1.5.0_04\jre, sun.arch.data.model=32, hibernate.connection.url=jdbc:mysql://localhost/practicum, hibernate.dialect=org.hibernate.dialect.MySQLDialect, 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.5.0_04, java.ext.dirs=C:\opt\java\jdk1.5.0_04\jre\lib\ext, sun.boot.class.path=C:\opt\java\jdk1.5.0_04\jre\lib\rt.jar;C:\opt\java\jdk1.5.0_04\jre\lib\i18n.jar;C:\opt\java\jdk1.5.0_04\jre\lib\sunrsasign.jar;C:\opt\java\jdk1.5.0_04\jre\lib\jsse.jar;C:\opt\java\jdk1.5.0_04\jre\lib\jce.jar;C:\opt\java\jdk1.5.0_04\jre\lib\charsets.jar;C:\opt\java\jdk1.5.0_04\jre\classes, java.vendor=Sun Microsystems Inc., connection.driver_class=com.mysql.jdbc.Driver, file.separator=\, 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:mysql://localhost/practicum, dialect=org.hibernate.dialect.MySQLDialect, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
17:57:38,173 DEBUG Configuration:998 - Preparing to build session factory with filters : {}
17:57:38,364 INFO Configuration:875 - processing extends queue
17:57:38,364 INFO Configuration:879 - processing collection mappings
17:57:38,364 DEBUG HbmBinder:2466 - Second pass for collection: src.BillingAddress.customer
17:57:38,364 INFO HbmBinder:2041 - Mapping collection: src.BillingAddress.customer -> customer
17:57:38,374 DEBUG HbmBinder:2482 - Mapped collection key: id, one-to-many: src.Customer
17:57:38,374 INFO Configuration:888 - processing association property references
17:57:38,374 INFO Configuration:917 - processing foreign key constraints
17:57:38,384 DEBUG Configuration:964 - resolving reference to class: src.BillingAddress
org.hibernate.MappingException: Repeated column in mapping for entity: src.Customer column: id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:504)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:526)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:544)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:335)
at org.hibernate.mapping.RootClass.validate(RootClass.java:188)
at org.hibernate.cfg.Configuration.validate(Configuration.java:839)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1000)
at src.HibernateUtil.createSession(HibernateUtil.java:27)
at src.HibernateUtil.getSession(HibernateUtil.java:41)
at src.HibernateUtil.persist(HibernateUtil.java:64)
at tests.CustomerTest.testCreateCustomer(CustomerTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using:
MySQL 4.1
Rory