java 在 Hibernate 注释上分配生成器类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4602808/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:10:11  来源:igfitidea点击:

Assigned Generator Class on Hibernate Annotations

javahibernateorm

提问by Mark Estrada

Hibernate newbie here. I am working on a simple Hibernate mapping file. When I am using the xml approach, I set the generator class to assigned. There are certain logic that must be checked before an employee id is assigned so I cant generate it automatically.

在这里休眠新手。我正在处理一个简单的 Hibernate 映射文件。当我使用 xml 方法时,我将生成器类设置为分配。在分配员工 ID 之前必须检查某些逻辑,因此我无法自动生成它。

<id name="id" type="string" column="emp_id">
        <generator class="assigned">
        </generator>
</id>

But I am also studying the annotation type and annotation seems to be in thing nowadays as frameworks are moving away from configuration files. But I cant find any generation type to match the assigned value

但我也在研究注释类型,随着框架逐渐远离配置文件,现在注释似乎很流行。但我找不到任何生成类型来匹配分配的值

public class Employee{
 String id; 
 @column(name="emp_id", unique=true)
 public String getID(){
  return id;
 }
}

Does this mean that I dont need to add any sequence generator annotation when it is assigned? Thanks

这是否意味着我在分配时不需要添加任何序列生成器注释?谢谢

回答by dogbane

Just use the @Idannotation which lets you define which property is the identifier of your entity. You don't need to use the @GeneratedValueannotation because I don't think you want hibernate to generate this property for you.

只需使用@Id可让您定义哪个属性是实体标识符的注释。您不需要使用@GeneratedValue注释,因为我认为您不希望 hibernate 为您生成此属性。

@Id
String id;