-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using a backup table for historization
PostPosted: Mon May 10, 2004 11:13 am 
Newbie

Joined: Thu May 06, 2004 11:10 am
Posts: 3
Location: Eitorf-Germany
Hi,

have two tables one is called Mitgliedr and one is MitgliedHistory that has an additional
version counter. When I try to save a member instance I'ld like to have a copy with a version counter in the mitglied_history table. I always receive an exception saying:

HibernateException: identifier of an instance of de.guug.mv.data.MitgliedHistory altered from de.guug.mv.data.MitgliedHistory@1d686c1 to de.guug.mv.data.MitgliedHistory@1d686c1

My code is:
public void storeMitglied(Mitglied mitglied) throws Exception {
MitgliedHistory history = new MitgliedHistory();
this.copyIMitgliedForm(mitglied, history);
this.copyMitgliedUnconverted(mitglied, history);
session.saveOrUpdate(mitglied);
history.setNr(mitglied.getNr());
session.save(history);
}

The mapping of the MitgliedHistory Class is:
<class
name="de.guug.mv.data.MitgliedHistory"
table="mitglieder_history"
dynamic-update="false"
dynamic-insert="false"
>

<composite-id>
<key-property name="nr" column="nr" />
<key-property name="version" column="version"/>
</composite-id>

<property
name="alias"
type="java.lang.String"
update="true"
insert="true"
column="alias"
/>

<property
name="aliasValid"
type="boolean"
update="true"
insert="true"
column="alias_valid"
/>

<property
name="bank"
type="java.lang.String"
update="true"
insert="true"
column="bank"
/>

<property
name="betrag"
type="java.lang.Integer"
update="true"
insert="true"
column="betrag"
/>

<property
name="bezahlinfo"
type="java.lang.String"
update="true"
insert="true"
column="bezahlinfo"
/>

<property
name="blz"
type="java.lang.String"
update="true"
insert="true"
column="blz"
/>

<property
name="buchungshinweis"
type="java.lang.String"
update="true"
insert="true"
column="buchungshinweis"
/>

<property
name="dr"
type="boolean"
update="true"
insert="true"
column="dr"
/>

<property
name="duName"
type="java.lang.String"
update="true"
insert="true"
column="du_name"
/>

<property
name="eintritt"
type="java.sql.Timestamp"
update="true"
insert="true"
column="eintritt"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="email"
/>

<property
name="emailValid"
type="boolean"
update="true"
insert="true"
column="email_valid"
/>

<property
name="ermaessigungsgrund"
type="java.lang.String"
update="true"
insert="true"
column="ermaessigungsgrund"
/>

<property
name="fax"
type="java.lang.String"
update="true"
insert="true"
column="fax"
/>

<property
name="firma"
type="java.lang.String"
update="true"
insert="true"
column="firma"
/>
</class>


I' currently using version 2.1.1

Actually I'm a little bit confused by this exception message, because it seems that the class id is the same. I already played around with the mapping, I'ld also like to use the
<version> tag for my version field. But then I cannot use the version field also as part
of the compound-id.
Does my exceptions have something to do with the override hashCode function, that is right now just super.hashCode(); ?

Any ideas?

Another issue is that I'ld would like to inherit the history class from the normal class in order no to copy code. But I'ld also like to use the Xdoclet tags. But that causes trouble,
because the parent class has a different primary key.

thanks in advance
Michael


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 11:18 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Use a database view and map your Mitgliedr class to that view. The view has all the columns of the Mitgliedr table and the "highest" version column of the MitgliedrHistory table. Then, create an INSTEAD OF trigger on that view where you INSERT and UPDATE yourself, splitting the modification into an update of the base table and a copy to the history table.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Database views
PostPosted: Tue May 11, 2004 3:59 am 
Newbie

Joined: Thu May 06, 2004 11:10 am
Posts: 3
Location: Eitorf-Germany
Hi Christian,

the use of a database view is of cause an option to solve my problem. But I still have the task to map the history table, because I'ld like to view the changes on some entity.
But I'll try that. Probably the trigger keeps away the insert and readonly access to the history table won't be a problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 11, 2004 4:00 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The INSTEAD OF trigger will handle all INSERT and UPDATE on that view, this is transparent to Hibernate (and other clients of your database) and the best choice.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Triggers
PostPosted: Tue May 11, 2004 5:46 am 
Newbie

Joined: Thu May 06, 2004 11:10 am
Posts: 3
Location: Eitorf-Germany
Hi!

I don't like the TRIGGERS solution. The database postgresql that I'm currently use doesn't
support SQL triggers as far as I understand. So I have to add a new Language like 'C' to my Java project and that enhances the complexity. The second point is that I want to have access to the mitglied and the mitglied_history therefore I have to map both anyway.
The trigger/view solution is nice when I'm going to make the backup hidden.
So I'll investigate the mapping a little bit more.

cheers

Michael


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