-->
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.  [ 4 posts ] 
Author Message
 Post subject: Mapping a concrete class whose superclass contains id
PostPosted: Wed Mar 12, 2008 1:35 am 
Newbie

Joined: Tue Mar 11, 2008 10:12 pm
Posts: 2
I have been working on a set of domain objects that inherit from an abstract class that needs to have knowledge of the identifier's get and set methods. For example:

Code:
ConcreteClass extends AbstractClass {
  int id;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

AbstractClass {
abstract public int getId();
abstract public void setId(int id);

public function() {
   int i = getId();
     // uses i to do some calculations.
  }
}

<hibernate-mapping package="...">
  <class name="ConcreteClass" table="....">
     <id name="id" column="id">
        <generator class="native"/>
   </id>
  </class>
</hibernate-mapping>


The issue is that I do not want to map the inheritance tree to the database. I want each concrete class to be mapped individually as a domain object (eg. No sharing of primary keys). Hibernate throws the following error when I try a direct mapping:

Initial SessionFactory creation failed.org.hibernate.MappingException: identifier mapping has wrong number of columns:

Is this possible to do this without using inheritance mapping strategies? I would think that Hibernate would allow me to since the get and set functions are defined as abstract.

Creating a helper object is not an option in this case. The super class needs to know about the getId and setId functions based on some contraints of the framework I am using.

Any help would be greatly appreciated. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 2:48 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I don't know the xml equivalent but with annotations it's @MappedSuperclass


http://www.hibernate.org/hib_docs/annot ... le/#d0e898

Or you could try the inheritance strategy Table per concrete class. This way you shouldn't get a table for the abstract superclass.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 12, 2008 5:06 pm 
Newbie

Joined: Tue Mar 11, 2008 10:12 pm
Posts: 2
Thanks for your reply. I gave MappedSuperClass a try, but it doesn't seem to behave the way I was expecting. Since my superclass has the @Id annotation, it is being mapped to the database similiar to the Table per concrete class inheritance strategy, which is what I don't want.

The problem with Table per concrete class mapping is that the primary key is unique to all the tables that inherit from the abstract class. This is too broad of a scope for me. Instead, I would like each table to have it's own set of id's without worry of conflict from the other tables.

Maybe I am doing something wrong, but it doesn't seem like the MappedSuperClass annotation is really embedding the member variables as it says in the specification. I would expect all functionality to be copied from superclass to the concrete class and for the primary key to not be shared between all tables. Maybe this is a bug with the MappedSuperClass annotation?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 5:25 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Yes you are doing something wrong. When used correctly you get completely independent sets of IDs for the subclasses.

Make sure you DONOT add any Inheritance or Entity annotation to the superclass, which has the the MappedSuperClass Annotation.

If it still doesn't work, post some sourcecode

Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.