Java 休眠列表映射
我正在尝试映射实体,因此我将具有以下或类似效果(最好没有OrderItem.quantity): 这是我的实体:Java 休眠列表映射,java,hibernate,jpa,mapping,Java,Hibernate,Jpa,Mapping,我正在尝试映射实体,因此我将具有以下或类似效果(最好没有OrderItem.quantity): 这是我的实体: public class Orders implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToOne private UserCreds
public class Orders implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private UserCreds user;
@OneToMany
private List<Item> orderedItems;
等等。。
但是我就是不能让它工作,或者我的解决方案完全错了?在这里使用元素集合可能是一个更好的选择。元素集合可以很好地提供与属性的关系 在您的
订单
类中,它可能是这样的:
@ElementCollection
private Map<Item, Integer> itemQuantities = new HashMap<Item, Integer>();
@ElementCollection
私有映射itemQuantilities=新HashMap();
编辑:当映射
的值类型是基本类型或可嵌入
时,可以使用@ElementCollection
<代码>整数是一种基本类型
如果您决定使用实体作为映射的值类型
,则必须使用@OneToMany
或@ManyToMany
在这种情况下,键类型对注释选择没有影响,因此您可以使用
项
或长
作为键,而无需更改注释。但是,这确实会对您可以使用的物理映射注释产生影响。“JPA 2.0规范明确规定@ElementCollection用于基本类型和可嵌入对象”,我认为我需要将这两个类都用作@Entity。以下解决方案导致org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany针对未映射的类:Entity.Orders.ItemQuantilities[java.lang.Integer](targetClass)没有帮助
@ElementCollection
private Map<Item, Integer> itemQuantities = new HashMap<Item, Integer>();