0
是否有可能擁有一個集合的地圖?對於下面的代碼,用戶有一些購買,但我想映射它,所以產品實體(或產品ID)是關鍵,而這些值是所有類型產品的購買。JPA,Hibernate,使用@MapKeyColumn的集合地圖
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Purchase.class, orphanRemoval = true)
@JoinColumn(name = "user_id", updatable = false)
@MapKeyColumn(name = "product_id")
private Map<Long, List<Purchase<Product>>> purchases = new HashMap<>();
}
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}
@Entity
public class Purchase<T extends Product> implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
@ManyToOne(targetEntity = Prodect.class)
@JoinColumn(name = "product_id", referencedColumnName = "id")
private T product;
@Column(name = "purchase_date")
private Date purchaseDate;
}
異常;
org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of test.domain.Purchase.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of test.domain.Purchase.id
...
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of test.domain.Purchase.id
...
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field test.domain.Purchase.id to java.util.ArrayList