tdimmig
tdimmig

Reputation: 730

How to persist entity to two different tables using Spring+Hibernate

I imagine this pertains to Hibernate only (I'm just now beginning to use these two frameworks). I have an application that tracks sessions for users. While a session is active, the Session entity is stored in a table for active sessions. When the user goes offline and the session ends, the session is moved to a secondary historical table.

How do I achieve this with Hibernate? Right now I have a Session.hbm.xml file that maps a Session object to the active sessions table. Can I map it to a secondary table and somehow specify to which table I want it to persist when I call saveOrUpdate?

My reputation currently won't allow me to answer my own question this quickly. I don't want anyone to waste their time on this though, since I found an answer, so I'm posting it here as an edit.

I can do this by making use of the entity-name attribute in a mapping file. I created a second mapping, identical to Session.hbm.xml, called HistoricalSession.hbm.xml. In this new mapping file I reference the same Session class, but add:

entity-name="HistoricalSession"

Then I map the object to my second (historical) table just like normal. Calling save() or saveOrUpdate() defaults to using the classname as the entity-name, and saves in my primary table as before. Now, when I want to save a session to the historical table I use the Hibernate API overrides that allow you to specify an entity-name:

saveOrUpdate("HistoricalSession",session);

This accomplishes exactly what I want without need to create another Java class for historical sessions

Upvotes: 3

Views: 2562

Answers (3)

tdimmig
tdimmig

Reputation: 730

I can do this by making use of the entity-name attribute in a mapping file. I created a second mapping, identical to Session.hbm.xml, called HistoricalSession.hbm.xml. In this new mapping file I reference the same Session class, but add:

entity-name="HistoricalSession"

Then I map the object to my second (historical) table just like normal. Calling save() or saveOrUpdate() defaults to using the classname as the entity-name, and saves in my primary table as before. Now, when I want to save a session to the historical table I use the Hibernate API overrides that allow you to specify an entity-name:

saveOrUpdate("HistoricalSession",session);

This accomplishes exactly what I want without need to create another Java class for historical sessions

Upvotes: 5

baba.kabira
baba.kabira

Reputation: 3171

Your need sounds like more of an audit like. Check project Hibernate Envers it might help solve your case in a better way.

Upvotes: 0

ams
ams

Reputation: 62632

A couple of way to do this could be:

  • Use a database trigger when the session gets expired the trrigger will move the row to the historical table.
  • You can create a HistoricalSession extends Session and then do a second mapping for HistoricalSession and write the code to delete from Session and insert into historical session.

Upvotes: 1

Related Questions