简体   繁体   English

如何使@ManyToOne实体成为冬眠中的类的ID

[英]How to make an @ManyToOne entity the Id of a class in hibernate

I have a problem and tried already allot of ways to solve it. 我有一个问题,并已经尝试分配解决方法。

the problem is quite simple how can I use both the Item ID and the amount as a primary key? 问题很简单,我如何同时使用商品ID和数量作为主键?

Since this will make the item a tiny blob. 因为这将使该项目成为一个很小的斑点。 And if I use @MapsId it gives the exact same thing. 如果我使用@MapsId,它将给出完全相同的结果。

@Entity
public class C_Drop extends LightEntity implements Serializable {

    @Id
    @ManyToOne
    private C_Item item;
    @Id
    private double amount;
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class C_Drops extends LightEntity implements Serializable {

    @Id
    double id;

    @OneToMany
    private List<C_Drop> drops;
}

I haven't done this myself, but you could probably use a combination of a Composite Primary Key and double-mapping the column. 我自己还没有完成此操作,但是您可以结合使用Composite Primary Key和双重映射列。 eg: 例如:

@Entity
@IdClass(C_DropPK.class)
public class C_Drop extends LightEntity {
    @Id
    private double amount;

    @Id
    @Column(name = "ITEM_ID")
    private double itemId;

    @ManyToOne
    @JoinColumn(name="ITEM_ID")
    private C_Item item;
}

Then: 然后:

public class C_DropPK {
    private double itemId;

    private double amount;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM