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

问题描述 投票:0回答:1

有我的课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,port,ip),因此我想将它们保留在两个不同的表中。例如:在被调用方和调用方表中。我该怎么做?

java hibernate jpa spring-data-jpa persistence
1个回答
0
投票
您可以使用两个实体。只需从@Entity中删除Party注释,然后用MappedSuperclass对其进行注释。然后,您可以创建两个实体:
© www.soinside.com 2019 - 2024. All rights reserved.