How to generate a hibernate ID with auto generate with a starting value

核能气质少年 提交于 2019-12-01 16:40:12

问题


Hi I have written code like this

@Id
@Column(nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO)
public int getUserID() {
    return UserID; 
}

But I manually setting it from DAO like "e.setUserID(01);" to insert.Otherwise row not inserting Is there any process to get value to id and retrieve what value generated automatically. Im thinking i will get some help


回答1:


Use

@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")



回答2:


Use the IDENTITY generation type instead of auto. Use a Long for id. I also recommend changing the name from UserID to userId. Do not forget the @Entity for the class name.

@Entity
public class MyClass{

private Long userId;

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column
    public Long getUserID(){
        return userId;
    }

    //.. rest of class

}

Be very careful with the naming conventions and make sure your field names and types match the field names and types from the database.



来源:https://stackoverflow.com/questions/10100970/how-to-generate-a-hibernate-id-with-auto-generate-with-a-starting-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!