gpt4 book ai didi

java - 在 JPA 中处理 ID 表的最佳解决方案是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:29 24 4
gpt4 key购买 nike

嗨,对于我的工作,我正在尝试正确处理用于保持其他表之间一致性的表。
架构师决定在 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=?

我担心这种关系的性能是否正确?有没有更好的解决方案或任何方法来避免在选择命令上加入?

**编辑**:克里斯给了我一个想法。如果 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;
})

将任何形式的排序注释放在您想要使用排序的任何字段上。

关于java - 在 JPA 中处理 ID 表的最佳解决方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33013045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com