dnc253
dnc253

Reputation: 40357

Any way to get fields from entity without actually joining into that table?

I'm not sure this is possible, but knowing just the very basics of JPA, I want to ask if it is possible. Basically I have an entity (We'll call it MyEntity) with a bunch of fields on it. We now want a 2nd entity that has all the same fields as MyEntity plus some of it's own. The use case for this is archiving these entities. We want to store all the archived entities in a separate table than MyEntity so that we don't have to qualify all the queries with archived=false. The JPA annotations for MyEntity look something like this:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE")
public abstract class MyEntity
{
    ....

There are multiple classes that extend this abstact class, each with @DiscriminatorValue annotations

For my archived entity (MyArchivedEntity) I want something along the lines of this:

@Entity
public class MyArchivedEntity
{
    private MyEntity entity;
    private String archiveSpecificField;
    ....

The problem with this of course is that it will want to join into the MyEntity table and get a specifc MyEntity record for populate the entity field. Is there some kind of annotation or something I can do to just get the same fields/columns from that entity (MyEntity) into this entity (MyArchivedEntity)?

Like I said in the beginning, I'm not sure if this is possible, but I hope I've explained well enough the end goal of what I'm trying to achieve, so that there could be some way to achieve it. If it makes any difference, I'm using PostgreSQL with EclipseLink.

Upvotes: 0

Views: 223

Answers (2)

James
James

Reputation: 18389

You may wish to look into EclipseLink's history support. It can automatically maintain a historical archive table.

See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/History

Another option would be to map the same classes in another persistence unit using an orm.xml to the archive tables.

Upvotes: 0

ben75
ben75

Reputation: 28736

What you can do is using @MappedSuperclass on a AbstractParentEntity becoming the super class of both MyEntity and MyArchiveEntity. So you will have something like the following:

@MappedSuperclass
public abstract class AbstractParentEntity {
    public String someField;
    ...
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE")
public abstract class MyEntity extends AbstractParentEntity 
{
   //here you don't have any field (they all move to  AbstractParentEntity
   // (or, at least all the fields that need to be archivied are now declared in parent class)
   ....
}

@Entity
public class MyArchivedEntity extends AbstractParentEntity  
{
    private String archiveSpecificField;
    ....

More about MappedSuperclass here:

Mapped superclass inheritance allows inheritance to be used in the object model, when it does not exist in the data model. It is similar to table per class inheritance, but does not allow querying, persisting, or relationships to the superclass. Its' main purpose is to allow mappings information to be inherited by its' subclasses. The subclasses are responsible for defining the table, id and other information, and can modify any of the inherited mappings. A common usage of a mapped superclass is to define a common PersistentObject for your application to define common behavoir and mappings such as the id and version. A mapped superclass normally should be an abstract class. A mapped superclass is not an Entity but is instead defined though the @MappedSuperclass annotation or the <mapped-superclass> element.

Upvotes: 1

Related Questions