-->
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.  [ 7 posts ] 
Author Message
 Post subject: Inheritance: Storing only the base class
PostPosted: Wed Apr 14, 2004 3:51 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
Hi:

I have a base class called Person

Code:
public class Person
{
   private String ID;
   private String attr1;
   private String attr2;

   public String getID()
   {
      return ID;
   }

   public void setID(String ID)
   {
      this.ID = ID;
   }

   public String getAttr2()
   {
      return attr2;
   }

   public void setAttr2(String attr2)
   {
      this.attr2 = attr2;
   }

   public String getAttr1()
   {
      return attr1;
   }

   public void setAttr1(String attr1)
   {
      this.attr1 = attr1;
   }
}


It has a sub class called Manager

Code:
public class Manager extends Person
{
   private String attr3;

   public String getAttr3()
   {
      return attr3;
   }

  public void setAttr3(String attr3)
  {
     this.attr3 = attr3;
  }
}


And my hibernate.xml is as follows:

<hibernate-mapping>
<class name="Person" table="Person">
<id name="ID" type="string">
<column name="ID" length="32"/>
<generator class="uuid.hex"/>
</id>
<property name="attr1" column="attr1" type="string"/>
<property name="attr2" column="attr2" type="string"/>
</class>
</hibernate-mapping>

I do not want to create a table for the subclass but want to store the subclass' (Manager's) data (only attr1 and attr2) in the superclass' (Person's) table.

So, I tried to run the following code,

Code:
public static void main(String[] args)
{
    .
    .
    Session session = sessionFactory.openSession();

   Manager manager  = new Manager();
   session.save(manager);
   session.flush();
   session.connection().commit();
   session.close();
}


But I get the following exception:

net.sf.hibernate.MappingException: No persister for: Manager
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:344)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2574)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2581)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:725)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)

Is what I am trying even feasible? If yes, please enlighten me.

Thank you,
Om


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 4:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You don't have to map it to a seperate table, but you would need to map the Manager class.

Have a look at the docs regarding mapping inheritence hierarchies...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 4:13 am 
Newbie

Joined: Wed Jun 30, 2004 4:00 am
Posts: 4
Om,

I am having the same problem like you and do not understand,
how I have to map the subclass.
Did you find a solution for this problem?


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 12:22 pm 
Newbie

Joined: Fri Jul 02, 2004 12:13 pm
Posts: 2
You will need to add a discriminator and a subclass mapping to your person mapping, as follows:

<hibernate-mapping>
<class name="Person" table="Person" discriminator-value="Manager">
<id name="ID" type="string">
<column name="ID" length="32"/>
<generator class="uuid.hex"/>
</id>
<discriminator/>
<property name="attr1" column="attr1" type="string"/>
<property name="attr2" column="attr2" type="string"/>

<subclass name="Manager" discriminator-value="Manager">
<property name="attr3" column="attr3" type="string"/>
</subclass>


</class>
</hibernate-mapping>

You will also need to add a column called "class" to the Person table in your database. This is all described in the inheritance mapping section of the docs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 5:41 pm 
Newbie

Joined: Wed Jun 30, 2004 4:00 am
Posts: 4
My subclass have no further fields which have to be stored in the database, only the fields of the base class are relevant for the persister.
Since there is an option for implicit or explicit polymorphism when loading objects from the database,
I think it would make sense to declare a subclass without a dricriminator when explicit polymorphism is used.

Or do I misunderstand something?


Top
 Profile  
 
 Post subject: Similar Problem
PostPosted: Wed Sep 22, 2004 1:45 pm 
Newbie

Joined: Wed Jul 28, 2004 3:20 pm
Posts: 16
Hi,

Rather than start a new post on this already busy board I found this one that is very similar to my problem. We have a situation where we have a concrete class which is mapped in hibernate as Category. We're writing an application which will be extended by end users in creative ways. We would like the users to be able to subclass Category to add convenience methods (but not new persistent data). I'll call this subclass MyCategory.

Since the code containing the superclass is locked down and immutable for the end users, it's not really feasable to modify the hibernate mappings.

I see two problems here, 1 of which I have solved.

1) On retrieval of the object from the database, hibernate creates the object of the type given in the mapping file. Ok, I have an interceptor which overrides instantiate() to load a configured subclass instead and set the id. This works fine.

2) As discussed in this thread, hibernate needs to know about the persisteer to use and it doesn't know anything about MyCategory, only Category so it chokes saying
-- net.sf.hibernate.MappingException: No persister for: com.stormhq.util.hibernate.MyCategory

I have a couple of possible solutions that seem like they might work, but I'd rather not spend days trying to figure this out if it's already been solved.

1) Can one of the interceptor methods or a custom ClassPersister be used to provide the name of the super class to use as a persister?

2) Can a class be mapped as a subclass of a known hibernate object -- outside of the mapping of the superclass??? (This would be acceptable to us)

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject: Re: Similar Problem
PostPosted: Mon Oct 17, 2005 2:39 pm 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
anyone has found a solution to this? i need it as well. basically the "Category" class is known to hiberenate. but the sub-class "MyCategory" doesn't contain any new persistent data. how can we make the load/save/find operations return MyCategory, not Category?

many thanks.

rmangi wrote:
Hi,

Rather than start a new post on this already busy board I found this one that is very similar to my problem. We have a situation where we have a concrete class which is mapped in hibernate as Category. We're writing an application which will be extended by end users in creative ways. We would like the users to be able to subclass Category to add convenience methods (but not new persistent data). I'll call this subclass MyCategory.

Since the code containing the superclass is locked down and immutable for the end users, it's not really feasable to modify the hibernate mappings.

I see two problems here, 1 of which I have solved.

1) On retrieval of the object from the database, hibernate creates the object of the type given in the mapping file. Ok, I have an interceptor which overrides instantiate() to load a configured subclass instead and set the id. This works fine.

2) As discussed in this thread, hibernate needs to know about the persisteer to use and it doesn't know anything about MyCategory, only Category so it chokes saying
-- net.sf.hibernate.MappingException: No persister for: com.stormhq.util.hibernate.MyCategory

I have a couple of possible solutions that seem like they might work, but I'd rather not spend days trying to figure this out if it's already been solved.

1) Can one of the interceptor methods or a custom ClassPersister be used to provide the name of the super class to use as a persister?

2) Can a class be mapped as a subclass of a known hibernate object -- outside of the mapping of the superclass??? (This would be acceptable to us)

Any help would be greatly appreciated.


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