database 如何使用带有起始值的自动生成生成休眠 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10100970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a hibernate ID with auto generate with a starting value
提问by Vidya
Hi I have written code like this
嗨,我写过这样的代码
@Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() { return UserID; }
@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
但我从 DAO 手动设置它,如“e.setUserID(01);” 插入。否则行不插入是否有任何过程可以将值获取到 id 并检索自动生成的值。我想我会得到一些帮助
采纳答案by Shehzad
Use
用
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
回答by Raul Rene
Use the IDENTITY generation type instead of auto. Use a Long for id. I also recommend changing the name from UserIDto userId. Do not forget the @Entity for the class name.
使用 IDENTITY 生成类型而不是自动。对 id 使用 Long。我还建议将名称从UserID更改为userId。不要忘记类名的@Entity。
@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.
命名约定要非常小心,并确保您的字段名称和类型与数据库中的字段名称和类型匹配。

