-->
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.  [ 6 posts ] 
Author Message
 Post subject: How do I map one class to separate tables ?
PostPosted: Tue Jan 29, 2008 7:16 am 
Newbie

Joined: Thu Jun 14, 2007 6:14 pm
Posts: 15
To improve performance, I have two classes that are the same in every way but used in completely different scenarios, so I would like to split the tables for faster querying.

However I'm not sure how this works using Spring's Hibernate Template. So two questions,

1) Is entity names what I should be looking at to split them,

2) How can I load or save an object to the correct table, since those methods take the Class as an argument?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 9:51 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
If you are using Hibernate Annotations, you would do the following.

Code:

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class TopClass {

   // all shared properties and methods, id

}

@Entity
@Table(name="TABLE_A")
public class SubClassA extends TopClass {
    // empty class body
}


@Entity
@Table(name="TABLE_B")
public class SubClassB extends TopClass {
    // empty class body
}



After that, Hibernate will always know which table to access when you create instances of, or query for, SubClassA or SubClassB. It's that simple!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 11:00 am 
Newbie

Joined: Thu Jun 14, 2007 6:14 pm
Posts: 15
I'm still a little confused because in your example you are persisting subclasses, in my scenario the two classes are exactly the same, just a very reusable object that I would prefer to be saved in different tables.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 11:17 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
I see. Well I've not used this myself but you could look into using @Embeddable entities. As far as I know you can only override column names but you might be able to work out a solution.

My solution could still work though. Both the subclasses are still instances of the super class, and your business logic code can treat them identically by assigning them to a variable of class TopLevel.

Though you say your two classes are the same, they are not really as far as hibernate is concerned as their persistance strategy needs to be different.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 29, 2008 4:05 pm 
Newbie

Joined: Thu Jun 14, 2007 6:14 pm
Posts: 15
I think I found the solution, I can use entity-name to map the same class to a different table, and I can use the method save(String entityName, Object object) to persist the object. Thanks for your suggestions


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 7:31 am 
Newbie

Joined: Mon Feb 04, 2008 7:29 am
Posts: 7
Location: Belarus
fatefree wrote:
I think I found the solution, I can use entity-name to map the same class to a different table, and I can use the method save(String entityName, Object object) to persist the object. Thanks for your suggestions


Hello. You solution with entity-name is clearly discussed in "Java persistence with Hibernate". But does anybody know how to do this using annotations?


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