Hi,
I'm trying to persist an implementing class and the interface using the table per subclass mapping shown in the documentation.
The example is straighforward, however I'm getting the mapping file muddled I think. Effectively I have two tables, Payment and Visa, Payment should be mapping to the interface and Visa to the implementing class.
Table Visa columns,
visa_id (integer)
name (varchar)
Tbale payment columns,
payment_id (integer)
amount (decimal)
To me this seems to be logically correct, but at the same time feel I'm missing something and my concept cannot be this simple.
I'm using Hibernate 2.x,
this is my mapping file
Payment.hbm.xml
Code:
<?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>
<class name="com.duneorbit.Payment" table="Payment">
<id name="id" type="long" column="payment_id">
<generator class="native"/>
</id>
<property name="amount" column="amount"/>
<joined-subclass name="com.duneorbit.Visa" table="visa">
<key column="payment_id"/>
<property name="name" column="name"/>
<many-to-one name="payment" column="payment_id" class="com.duneorbit.Payment"/>
</joined-subclass>
</class>
</hibernate-mapping>
this my interface and implementing class,
Payment interface
Code:
package com.duneorbit;
public interface Payment {
public void setId(Long id);
public Long getId();
public void setName(String name);
public String getName();
public void setAmount(Long amount);
public Long getAmount();
public void setPayment(Payment payment);
public Payment getPayment();
}
package com.duneorbit;
public class Visa implements Payment{
private Long id;
private String name;
private Long amount;
private Payment payment;
public void setId(Long id){
this.id = id;
}
public Long getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAmount(Long amount){
this.amount = amount;
}
public Long getAmount(){
return amount;
}
public void setPayment(Payment payment){
this.payment = payment;
}
public Payment getPayment(){
return payment;
}
}
in my unit test I'm doing this,
Code:
Payment payment = new Visa();
payment.setId(new Long(1));
payment.setName("name");
payment.setAmount(new Long("10"));
//payment.setPayment(payment);
try{
HibernateUtil.beginTransaction();
HibernateUtil.getSession().saveOrUpdate(payment);
HibernateUtil.commitTransaction();
}catch(Exception e){
System.out.println(e);
}
my stack trace error,
Code:
net.sf.hibernate.MappingException: Repeated column in mapping for class com.duneorbit.Visa should be mapped with insert="false" update="false": payment_id
at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:1015)
at net.sf.hibernate.persister.NormalizedEntityPersister.<init>(NormalizedEntityPersister.java:826)
at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:45)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:805)
at com.duneorbit.util.HibernateUtil.<clinit>(HibernateUtil.java:23)
at test.com.duneorbit.basics.AllHibernateInActionTests.testPersistInterface(AllHibernateInActionTests.java:175)
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:324)
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 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)
Thanks for any help....
I will continue to investigate.....