-->
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 one class to 2 different tables
PostPosted: Wed Aug 08, 2007 12:25 pm 
Newbie

Joined: Wed Aug 08, 2007 12:06 pm
Posts: 3
Hi there!

After some time trying to find out a solution for my problem, I have decided to write a post asking for some help. I'll try to explain what I'm trying to do. We are using Hibernate 3.2 and mysql 5.0

I have two different tables, both having exactly the same information.

Code:
CREATE TABLE A(
...
);

CREATE TABLE A_TEMP(
---
);


The A table has all the information but every week, some rows are changed. The process to update the information takes some time but the information has to be ready as soon as possible in table A. So we use A_TEMP to calculate the new data in advance. We have used entity-name to try to use the same java class and be able to differentiate both tables:

Code:
<class name="A" entity-name="A"  table="A">
...
</class>

<class name="A" entity-name="A_TEMP"  table="A_TEMP">
...
</class>


Then, we try to read from table A and insert into table A_TEMP with the following code:

Code:
List<A> list = (List<A>)session.createQuery(hql).list();
for (A a : list)
{
  //calculate A
  session.save("A_TEMP", a);
}


But we haven't been able to update the objects in A_TEMP table ....
Any help would be appreciate!

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 4:11 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You need to detach the objects from the session before saving them to the new table. Use session.evict(a):
Code:
List<A> list = (List<A>)session.createQuery(hql).list();
for (A a : list)
{
  session.evict(a);

  //calculate A
  session.save("A_TEMP", a);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 10:32 am 
Newbie

Joined: Wed Aug 08, 2007 12:06 pm
Posts: 3
This works!

But now, I would like to be able to query ONLY in one of the tables. However, it seems Hibernate executes the query in both tables. I have tried to use SQLQuery:

Code:
session.createSQLQuery(sql).addEntity("A_TEMP", A.class).list();


However, it keeps returning results from both tables.

Is there any way to specify which table to use?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 2:37 pm 
Newbie

Joined: Wed Aug 08, 2007 12:06 pm
Posts: 3
Find a solution in this post:

http://forum.hibernate.org/viewtopic.ph ... entityname


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.