-->
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: Table name is variable
PostPosted: Tue Aug 05, 2008 8:31 am 
Newbie

Joined: Tue Aug 05, 2008 4:31 am
Posts: 3
Hello, I have this scenary:

I'm developing an application for a wear store, they have the articles divided in sesasons, so there is a table for the articles of summer of 2008, winter 2008 and so on.

There are one table for each season and years, i.e. S08_ARTICLES, W08_ARTICLES, S09_ARTICLES...

When the user login in the application selects wich sseason want to use, so all the seasons must be available, and can be two users using the application and each one with one diferent season.

I like to use it with Hibernate, I have a class Article mapped to S08_ARTICLES, but my DAO use handcoded SQL for set the table name.

Is it possible to map this tables to the same class and that the DAO use the correct table depending on the season selected by the user? the season is stored in session, so I could pass it to the DAO methods.

Many thanks in advance and sorry for my poor english.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 05, 2008 10:01 am 
Newbie

Joined: Thu Mar 27, 2008 11:45 am
Posts: 10
Location: Russia
Hope next chapter will help you.
http://www.hibernate.org/hib_docs/refer ... tance.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 4:18 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes, it is possible to map the same class to multiple tables. There is an example in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... entityname

In your case you would map something like this:

Code:
<class name="Article" table="S08_Articles"
        entity-name="S08Article">
...
</class>

<class name="Article" table="W08_Articles"
        entity-name="W08Article">
...
</class>

and so on...



In your code you need to use the entity-name:s instead of the class name when loading articles.

Eg.

session.get("S08Article", id)

instead of

session.get(Article.class, id)


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Aug 11, 2008 3:11 am 
Newbie

Joined: Tue Aug 05, 2008 4:31 am
Posts: 3
Thanks nordborg. That's just what I was looking for.


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.