嗨,为了我的工作,我试图正确处理一个用于保持其他表之间一致性的表。
架构师决定在Oracle Database 12c上提供此解决方案:
Relational architecture
由于数据量大,每个实体上都有一个由分区列控制的分区。实体ID由每种实体类型生成。
我尝试使用JPA实体继承来简化数据库中的插入。这段代码是我发现的最佳解决方案:
@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{
...
}
问题是,由于JPA自动进行内部联接,因此我期望select命令的性能非常差:
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=?
我担心这种关系的表现对吗?有没有更好的解决方案或任何方法可以避免在select命令中加入该连接?
**编辑**:克里斯给了我一个主意。如果IdTable实体像这样将所有相关实体连接在一起怎么办?
@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插入Entity1之前,并且在请求Entity1时没有直接链接到IdTable。
现在,是否可以使用Entity1序列在IdTable中填充Entity_ID?
最佳答案
第二次尝试使用Entity1PK(可以删除注释),请尝试:
@Entity
@Table(name="ID_TABLE")
@IdClass(Entity1PK.class)
public class IdTable implements Serializable {
private static final long serialVersionUID = 1L;
@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;
..
}
@Entity
@Table(name = "ENTITY1")
@IdClass(Entity1PK.class)
public class Entity1 {
@Id
@Column(name="Partition_col1")
protected short partitionCol1;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
@Column(name="Entity_ID1")
protected long entityID1;
@Id
@Column(name="Version_ID1")
protected short versionID1;
@Id
@Column(name="Entity_Type1")
protected String entityType1;
})
将要使用排序的任何形式的排序注释放在任何字段上。