-->
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.  [ 2 posts ] 
Author Message
 Post subject: help needed - persisting an interface
PostPosted: Wed Nov 16, 2005 5:13 pm 
Newbie

Joined: Thu Jul 07, 2005 6:59 am
Posts: 13
Location: Derry Northern Ireland
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.....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 2:28 pm 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Quote:
<key column="payment_id"/>
<property name="name" column="name"/>
<many-to-one name="payment" column="payment_id" class="com.duneorbit.Payment"/>


The exception appears to indicate that it doesn't like you to declare payment_id both as the key column and as foreign key column for the many-to-one. Your table definitions don't contain that kind of foreign key, so why does your Hibernate mapping? In contrast, you have a visa_id in your database, but not in your mapping.

Apart from those technical points, I don't see the necessity for this complicated model. A class implementing an interface, on the other hand inheriting from that interface... Do you really need this complication, or could you just do with either a class implementing an interface (where you wouldn't have to persist the interface), or a class inheriting from a (potentially abstract) class?


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

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.