Hi, for my job I'm trying to handle correctly a table used to keep consistency between other tables.
Architects decided to provide this solution on an Oracle Database 12c:
Due to the amount of data, there is a partitionning controled by the partition colomn on every entity. The entity id is generated by each entity type.
I tryed to use the JPA entity inheritance to simplify insertion in database. This code is the best solution that I've found:
@Entity
@Table(name="ID_TABLE")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="Entity_Type", discriminatorType=DiscriminatorType.STRING)
public abstract class IdTable implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected IdTablePK id;
}
@Embeddable
public class IdTablePK implements Serializable {
@Column(name="Partition_col")
protected short partitionCol;
@Column(name="Entity_ID")
protected long entityID;
@Column(name="Version_ID")
protected short versionID;
@Column(name="Entity_Type")
protected String entityType;
}
@Entity
@DiscriminatorValue("01")
@Table(name = "ENTITY1")
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="Partition_col1", referencedColumnName="Partition_col"),
@PrimaryKeyJoinColumn(name="Entity_ID1", referencedColumnName="Entity_ID"),
@PrimaryKeyJoinColumn(name="Version_ID1", referencedColumnName="Version_ID"),
@PrimaryKeyJoinColumn(name="Entity_Type1", referencedColumnName="Entity_Type")
})
public class Entity1 extends IdTable{
...
}
The problem is that I'm expecting very poor performance on select command because of the inner join automatically made by JPA:
select
entity1.Partition_col1
entity1.Entity_ID1
entity1.Version_ID1
entity1.Entity_Type1
entity1.Data1
from
entity1
inner join
table_ID
on entity1.Partition_col=table_ID.Partition_col
entity1.Entity_ID=table_ID.Entity_ID
entity1.Version_ID=table_ID.Version_ID
entity1.Entity_Type=table_ID.Entity_Type
where
entity1.Partition_col1=?
and entity1.Entity_ID1=?
and entity1.Version_ID1=?
and entity1.Entity_Type1=?
Am I right to worry about performance with this kind of relation ? Is there any better solution or any way to avoid that join on select command?
**EDIT** : Chris gave me an idea. What if the IdTable entity have all related entity joined like this ?
@Entity
@Table(name="ID_TABLE")
public class IdTable implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected IdTablePK id;
@OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST, optional = true)
@JoinColumns({
@JoinColumn(name="Partition_col1", referencedColumnName="Partition_col"),
@JoinColumn(name="Entity_ID1", referencedColumnName="Entity_ID"),
@JoinColumn(name="Version_ID1", referencedColumnName="Version_ID"),
@JoinColumn(name="Entity_Type1", referencedColumnName="Entity_Type")
})
private Entity1 e1;
...
}
@Embeddable
public class IdTablePK implements Serializable {
@Column(name="Partition_col")
protected short partitionCol;
@Column(name="Entity_ID")
protected long entityID;
@Column(name="Version_ID")
protected short versionID;
@Column(name="Entity_Type")
protected String entityType;
}
@Entity
@Table(name = "ENTITY1")
public class Entity1 implements Serializable{
@EmbeddedId
protected Entity1PK id;
...
}
@Embeddable
public class Entity1PK implements Serializable {
@Column(name="Partition_col1")
protected short partitionCol1;
@Column(name="Entity_ID1")
protected long entityID1;
@Column(name="Version_ID1")
protected short versionID1;
@Column(name="Entity_Type1")
protected String entityType1;
}
IdTable is inserted before Entity1 and there is no direct link to IdTable when requesting Entity1.
Now, is it possible to use Entity1 sequence to fill Entity_ID in IdTable ?