简体   繁体   中英

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?

Since this will make the item a tiny blob. And if I use @MapsId it gives the exact same thing.

@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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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