gpt4 book ai didi

java - 如何将一个实体映射到不同的表

转载 作者:行者123 更新时间:2023-12-02 08:57:35 25 4
gpt4 key购买 nike

这是我的类TableOne.java:

@Table(name = "table_one")
@EntityListeners(AuditingEntityListener.class)
public class TableOne {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", unique = true, nullable = false, updatable = false)
private String id;

@CreatedDate
@Column(name = "created", nullable = false, updatable = false)
private LocalDateTime created;

@LastModifiedDate
@Column(name = "modified", nullable = false)
private LocalDateTime modified;

@Column(name = "status_desc")
private String statusDesc;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(table = "callers")
private Party caller;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(table = "callee")
private Party callee;

...getter/setter
}

还有Part.java:

@Entity
public class Party {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false, updatable = false)
private long id;

@Column(name = "desc")
private String desc;

@Column(name = "ip")
private String ip;

@Column(name = "port")
private int port;
}

TableOne.java 中的以下字段:调用者、被调用者包含相同的字段(id、desc、端口、ip),因此我想将它们保留在两个不同的表中。例如在 calleecaller 表中。我怎样才能做到这一点?

最佳答案

您可以为此使用两个实体。只需从 Party 中删除 @Entity 注释并使用 @MappedSuperclass 对其进行注释即可。然后您可以创建两个实体:

@Entity
@Table(name = "caller")
public class Caller extends Party

@Entity
@Table(name = "callee")
public class Callee extends Party

两者都将具有相同的字段,但将映射到两个不同的表。

关于java - 如何将一个实体映射到不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60399862/

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