-->
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.  [ 10 posts ] 
Author Message
 Post subject: same model persists to Multiple tables - please help (long)
PostPosted: Fri Apr 01, 2005 12:32 pm 
Beginner
Beginner

Joined: Thu Mar 03, 2005 9:19 pm
Posts: 22
Hibernate version:
3.0

I am working on an enterprise web project which requires we store customer data into different tables. These tables have absolutely the same schema. But they need to be named based on userId. For example, customer 1's data stored in table data1, customer 2's data in data2....

Well, to me this looks like a bad design to begin with. Everyone probably will agree using one table and data partitioning would be better. But in reality each table may have huge amount of data (gigas) in our case, and if we use one table it is going to be huge. Also, I think may be performance is better if we keep them in different tables (or does it matter?)

So I guess my question is, how to persist the same class into different tables using hibernate. We are using a login model. So, our clients log into the web front, and based on the userId x , we want to tell Hibernate where to get the stored data (in table datax).

I read some post on the internet asking similar question - but there is no answer. This one is closet I can find http://www.theserverside.com/news/threa ... 9732#85428

In this post, Gavin King said that it can be done by written your own ClassPersister. But I don't see how to do that in my case:

First of all, I think I need to override the getPropertySpaces() function in my own persister because this method actually returns table names for the query. However, because the table name need to be associated with userID, I need a mechanism to pass in a userId so getPropertySpaces can return correct table name (data+userId). But currently only one instance of ClassPersister is associated with a persisitent class in SessionFactory. In other words it is not thread safe.

I really don't know if this can be done or not. Please help and let me know if you have better suggestions.

Gavin, if you are reading this post, I would appreciate if you can let me know if overriding ClassPersisiter will do the trick and how.

Thank you all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 01, 2005 12:54 pm 
Beginner
Beginner

Joined: Thu Mar 03, 2005 9:19 pm
Posts: 22
Ok, in this post
http://forum.hibernate.org/viewtopic.php?t=940644
max suggest to rebuild configuration and sessionfactory. But it still does not answer the question: how to pass in the userId in a threadsafe manner?


Top
 Profile  
 
 Post subject: Entity name
PostPosted: Fri Apr 01, 2005 2:39 pm 
Newbie

Joined: Fri Apr 01, 2005 2:31 pm
Posts: 3
Try using the entity-name attribute. It's not well documented but it's pretty easy. Here's a quick example..

<hibernate-mapping>
<class entity-name="address1" name="Address" table="ADDRESS1">
........
</class>
<class entity-name="address2" name="Address" table="ADDRESS2">
........
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="User" table="ADDRESS1">
<many-to-one entity-name="address1" >
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 02, 2005 5:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why dont you also consider having one configuration/session factory per user ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 1:49 am 
Beginner
Beginner

Joined: Thu Mar 03, 2005 9:19 pm
Posts: 22
kanderson:

That won't help me. Hard coded mapping won't work. I need to dynamically pass in userId.

max:

Thanks for your idea. That might work... Dynamically generate mapping and feed it to a new Sessionfactory object.... the only downsize is performance because I see SessionFactory is heavyweight....

Thanks guys.


Top
 Profile  
 
 Post subject: Re: Entity name
PostPosted: Mon Apr 25, 2005 12:52 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
kanderson wrote:
Try using the entity-name attribute. It's not well documented but it's pretty easy. Here's a quick example..

<hibernate-mapping>
<class entity-name="address1" name="Address" table="ADDRESS1">
........
</class>
<class entity-name="address2" name="Address" table="ADDRESS2">
........
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="User" table="ADDRESS1">
<many-to-one entity-name="address1" >
</class>
</hibernate-mapping>


I'm trying to solve a similar problem, but maybe not so dynamic. The above mapping appears to solve my problem. I have a question about it.

My problem is simpler i think. Example: I've got a class called Address. I then have 2 other classes, say Student and Teacher, each of which contains a one-to-one association to an address. However, i want to store the Address data in 2 separate tables Student_Addresses and Teacher_Addresses. All of this is known in the mapping file.

Will the above mapping handle it?

The actual POJO class, for example, is ADDRESS. Where do the mappings of the attributes in the POJO live (street1, street2, etc). Do they have to be duplicated in both address1 and address2 class mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 11:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
yes.

yes.

:)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 9:56 am 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
steve wrote:
yes.

yes.

:)


Thanks for the reply. I guess I will have to make due.

We have several places where we really would like a separate table for a particular class based on where the collection of objects are housed. This is known up front, in the mapping file.

Mappings the same, everything the same, just want to separate it out for clarity.

Good example would be that we have a class called "Business", that is used for most/all business entities.

However we would like a single table that holds all "Lienholders", rather than store all Businesses in a Business table with a type.

Sounds like I can do it with the above mapping but i have to maintain the mapping for the Business class in 2 places and keep them in synch.

If there is another way of achieving this, please pass it on.

Meantime i'll head in this direction. thanks again for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well now it sounds like you are talking about inheritence which is really different from the initial question. Take a look at <joined-subclass/> as it sounds like what you want.

There you would have a BUSINESS table that held all the common attributes about a business regardless of its concrete type (name, address, etc). Additionally, you would have a LIENHOLDERS table with a shared PK relation to BUSINESS. LIENHOLDERS would hold attributes specific to lienholders (i.e. not shared with a general business).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 12:39 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
steve wrote:
Well now it sounds like you are talking about inheritence which is really different from the initial question. Take a look at <joined-subclass/> as it sounds like what you want.

There you would have a BUSINESS table that held all the common attributes about a business regardless of its concrete type (name, address, etc). Additionally, you would have a LIENHOLDERS table with a shared PK relation to BUSINESS. LIENHOLDERS would hold attributes specific to lienholders (i.e. not shared with a general business).


We do use inheritance with Hibernate in many places.

I'm sorry, I probably misstated my problem. In my scenario a Business can be a Lienholder, Lessor, Insurance Company, etc etc. These 3 entities do not have any additional attributes, they are not subclasses of Business.

Their difference simply lies in where they exist in the domain. A Title object contains a Lessor of type "Business". A Registration object contains an Insurance Company of type "Business". And a Lien contains a LienHolder of type "Business".

They are all a Business instance with no further attributes. However rather than have a "Business" table that contains a mix of all of these types, we want a Lessor table, InsuranceCompany table, and LienHolder table. The mappings and columns are identical, just different tables.
I was hoping that when these relationships were defined in the mapping file that the table to use could, in some way, be overridden. What it gets down to is that a class maps to a table. However there are times we want a class to map to a different table (same mapping/attributes) based on where the foreign/key relationshiop exists.


I didn't want to have to maintain 3 sets of mappings for Business to be able to achieve this - but perhaps i do.

thanks for your patience and help.


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