如何在多对多映射中添加带有 instant 的列?
How to add column with instant in many-to-many mapping?
大家好!
我必须 table 具有多对多映射的“市场”和“客户”。 Hibernate 自动创建带有 id 的 table post_comment table 然后我可以得到对象和一切正常......但我需要客户在市场上买东西的日期
市场class
@Entity
@Setter
@Getter
@NoArgsConstructor
public class Market{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ElementCollection(targetClass = Customer.class, fetch = FetchType.EAGER)
@CollectionTable(name = "market_customer", joinColumns = @JoinColumn(name = "market_id"))
private Set<Customer> banks = new HashSet<>();
............
}
市场class
@Entity
@Setter
@Getter
@NoArgsConstructor
public class Customer{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
............
}
我可以仅使用 ID 自动创建 marcet_customer,但我不明白如何添加日期和最重要的是如何通过 JPA 存储库获取它...
这就是 Hibernate @ManyToMany 的问题 - 它并不意味着有额外的列也不是可查询的(很好)。也许您想使用带有复合主键的单独实体:
@Entity(name = "MarketCustomer")
@Table(name = "market_customer")
public class MarketCustomer {
@EmbeddedId
private MarketCustomerId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("marketId")
private Market market;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("customerId")
private Customer customer;
...
}
@Embeddable
public class MarketCustomerId implements Serializable {
@Column(name = "market_id")
private Long marketId;
@Column(name = "customer_id")
private Long customerId;
...
}
一篇关于此的好文章https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ . With https://bootify.io您也可以在线创建您的架构(没有复合主键,但单独的主列也可以)。
Link 与指南多对多。
https://medium.com/@samuelmumo.sm/spring-data-jpa-composite-key-mapping-example-750eb54a3d99
大家好!
我必须 table 具有多对多映射的“市场”和“客户”。 Hibernate 自动创建带有 id 的 table post_comment table 然后我可以得到对象和一切正常......但我需要客户在市场上买东西的日期
市场class
@Entity
@Setter
@Getter
@NoArgsConstructor
public class Market{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@ElementCollection(targetClass = Customer.class, fetch = FetchType.EAGER)
@CollectionTable(name = "market_customer", joinColumns = @JoinColumn(name = "market_id"))
private Set<Customer> banks = new HashSet<>();
............
}
市场class
@Entity
@Setter
@Getter
@NoArgsConstructor
public class Customer{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
............
}
我可以仅使用 ID 自动创建 marcet_customer,但我不明白如何添加日期和最重要的是如何通过 JPA 存储库获取它...
这就是 Hibernate @ManyToMany 的问题 - 它并不意味着有额外的列也不是可查询的(很好)。也许您想使用带有复合主键的单独实体:
@Entity(name = "MarketCustomer")
@Table(name = "market_customer")
public class MarketCustomer {
@EmbeddedId
private MarketCustomerId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("marketId")
private Market market;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("customerId")
private Customer customer;
...
}
@Embeddable
public class MarketCustomerId implements Serializable {
@Column(name = "market_id")
private Long marketId;
@Column(name = "customer_id")
private Long customerId;
...
}
一篇关于此的好文章https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ . With https://bootify.io您也可以在线创建您的架构(没有复合主键,但单独的主列也可以)。
Link 与指南多对多。 https://medium.com/@samuelmumo.sm/spring-data-jpa-composite-key-mapping-example-750eb54a3d99