how to make two column as a primary key in hibernate annotation class

前端 未结 2 1824
猫巷女王i
猫巷女王i 2021-02-02 01:30

This is my annotation class and i want userId and groupId column both as primary key. I have found more questions (Question) about this, but didn\'t fo

2条回答
  •  我在风中等你
    2021-02-02 02:14

    you can create a composite primary key in hibernate using @UniqueConstraint annotation.

    @Table(name="user_group",uniqueConstraints=@UniqueConstraint(columnNames= {"userId","groupId"}))
    public class user_group 
    {
           @Column(name="userId")
           private String userId;
    
           @Column(name="groupId")
           private String group;
    }
    

    above method is not feasible if we use spring because for creating composite primary key we have to create a class is not a good thing.
    in hibernate and spring you only have to create POJO classes which are available as an entity on your system.

提交回复
热议问题