Spring 使用重复的额外列休眠多对多

Spring 使用重复的额外列休眠多对多,spring,database,spring-boot,hibernate,Spring,Database,Spring Boot,Hibernate,所以我想在多对多的情况下设置一个具有附加属性的表 public class Customer { private int id; // other attributes // constructors // getters / setters } 然后我有一个“访问”类,它链接了前两个 @Entity @EntityListeners(AuditingEntityListener.class) @NoArgsConstructor @AllArgsConstr

所以我想在多对多的情况下设置一个具有附加属性的表

public class Customer {
    private int id;
    // other attributes 
    // constructors
    // getters / setters
}
然后我有一个“访问”类,它链接了前两个

@Entity
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Visit {

    @EmbeddedId
    private VisitId visitId = new VisitId();

    @ManyToOne
    @MapsId("customerId")
    @JoinColumn(name = "id_customer", nullable = false)
    private Customer customer;

    @ManyToOne
    @MapsId("productId")
    @JoinColumn(name = "id_product", nullable = false)
    private Product product;

    private LocalDateTime date = LocalDateTime.now();

    @Embeddable
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public static class VisitId implements Serializable {

        private static final long serialVersionUID = 1L;

        private int customerId;
        private int productId;
    }
}
测试 客户1和产品1 客户2和产品1

并将其添加到数据库中。但是,如果我在日期不相同的情况下再次尝试将Customer 1与Product 1添加到一起,ofc将不起作用,因为主键是CustomerId和ProductId。如何将另一个“private int id”添加到主键中,或将日期转换为主键?

由于复杂id(客户和产品)不是唯一的,您只需拥有一个简单id以及与指定表的关系即可

@Id
private int visitId

@ManyToOne 
@JoinColumn(name = "id_customer", nullable = false)
private Customer customer;

@ManyToOne
@JoinColumn(name = "id_product", nullable = false)
private Product product;
也许再加一个


我更喜欢将日期作为尊重域概念的主键。因此,将日期字段从Visit类移动到VisitId类:

@Entity
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Visit {

    @EmbeddedId
    private VisitId visitId = new VisitId();

    @ManyToOne
    @MapsId("customerId")
    @JoinColumn(name = "id_customer", nullable = false)
    private Customer customer;

    @ManyToOne
    @MapsId("productId")
    @JoinColumn(name = "id_product", nullable = false)
    private Product product;

    @Embeddable
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public static class VisitId implements Serializable {

        private static final long serialVersionUID = 1L;

        private int customerId;
        private int productId;
        private LocalDateTime date = LocalDateTime.now();

    }
}

谢谢你的回答,我试试这个。
@Table(
uniqueConstraints=
    @UniqueConstraint(columnNames={"id_customer", "id_product", date })
)
@Entity
public class Visit{
@Entity
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Visit {

    @EmbeddedId
    private VisitId visitId = new VisitId();

    @ManyToOne
    @MapsId("customerId")
    @JoinColumn(name = "id_customer", nullable = false)
    private Customer customer;

    @ManyToOne
    @MapsId("productId")
    @JoinColumn(name = "id_product", nullable = false)
    private Product product;

    @Embeddable
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public static class VisitId implements Serializable {

        private static final long serialVersionUID = 1L;

        private int customerId;
        private int productId;
        private LocalDateTime date = LocalDateTime.now();

    }
}