Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Persisting a class into two tables

Fri Aug 06, 2010 9:40 am

Hi to all,

I hope to be in the right forum/section. If not sorry...

I'm new to JPA and I have the following problem:

I would like to build historization this way:

I have a class Identity and I would like to have two tables associated with it; Identity(ID) and Identity_History(IDHist).
In the first I will only have the actual "state" while in the second I will store all the (previous)states. So, for the first table will be inserts and updates and for the second only inserts(for every insert and update in ID).

In ID I only have the attributes, while in IDHist the attributes + the version numbers.

Is this with JPA mapping and annotations possible? I mean to have only a class(Identity) and two associated tables with "distributed" inserts/updates?

For a better understandin (maybe :) ) see the attachment.


At the moment my classes look like this:

Code:
...
@Entity
@Table(name="identity")
@SecondaryTable(name = "identity_history", pkJoinColumns = @PrimaryKeyJoinColumn(name = "dbId"))
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Identity
{
   // Identity and Version Fields
   @Id
   @GeneratedValue
   @XmlTransient
   private int dbId;
   @XmlElement(name = "identity-id")
   @Transient
   private String xmlId;
   @Version
   @XmlTransient
   private int version;

   // Data Fields
   @Embedded
   @AttributeOverrides({
       @AttributeOverride(name="attribute", column=@Column(name="username_attribute")),
       @AttributeOverride(name="versionNumber",column=@Column(name="username_versionNumber"))
   })
   private VersionedAttribute<String> username;
   
   @Embedded
   @AttributeOverrides({
       @AttributeOverride(name="attribute", column=@Column(name="password_attribute")),
       @AttributeOverride(name="versionNumber",column=@Column(name="password_versionNumber"))
   })
   private VersionedAttribute<String> password;
   
   @Embedded
   @AttributeOverrides({
       @AttributeOverride(name="attribute", column=@Column(name="isRevoked_attribute")),
       @AttributeOverride(name="versionNumber",column=@Column(name="isRevoked_versionNumber"))
   })
   private VersionedAttribute<Boolean> isRevoked;
   
   @Embedded
   @AttributeOverrides({
       @AttributeOverride(name="attribute", column=@Column(name="revocationReason_attribute")),
       @AttributeOverride(name="versionNumber",column=@Column(name="revocationReason_versionNumber"))
   })
   private VersionedAttribute<String> revocationReason;
...
}



@Embeddable
public class VersionedAttribute<T>
{
   private String versionNumber;
   private T attribute;
   
   public VersionedAttribute()
   {
      
   }

   public VersionedAttribute(String versionNumber, T attribute)
   {
      super();
      this.versionNumber = versionNumber;
      this.attribute = attribute;
   }
...
}


Thanx in advance for anx help

Francesco


Attachments
JPA-Mapping.jpg

Re: Persisting a class into two tables

Sat Aug 07, 2010 12:33 am

Hi brother , i think your answer is here :
jpa/one-to-many-relation-jpa-example-t6091.html

Post a reply
  Related Posts  to : Persisting a class into two tables
 Cascade.All and persisting related entities directly     -  
 Add two tables     -  
 Generating Tables from XML by php     -  
 Optimize All Tables In A MySQL Database     -  
 Database Cashing tables results model     -  
 Define class helper class to check the method existance     -  
 java abstract class,concrete class and interface     -  
 relationship between the Canvas class and the Graphics class     -  
 Define class inside another class C++     -  
 inner class that is a member of an outer class?     -  

Topic Tags

Java JPA