简体   繁体   English

带注释的休眠字符串主键

[英]Hibernate String Primary Key with Annotation

I am trying to create a Privilege class with Annotations whose Primary Key is a String.我正在尝试使用主键是字符串的注释创建一个权限类。 I will assign them manually while inserting.我将在插入时手动分配它们。 Therefore no need for hibernate to generate a value for it.因此不需要休眠为其生成值。 I'm trying to do something like that:我正在尝试做这样的事情:

@Id
@GeneratedValue(generator = "assigned")
@Column(name = "ROLE_NAME", nullable = false)
private String roleName;

But it throws that exception:但它抛出该异常:

Caused by: org.hibernate.AnnotationException: Unknown Id.generator: assigned

How can I configure a String primary key with annotations?如何使用注释配置String主键?

Since the roleName is not auto-generated, you should simply not annotate it with @GeneratedValue : 由于roleName不是自动生成的,因此您不应该使用@GeneratedValue对其进行注释:

@Id
@Column(name = "ROLE_NAME", nullable = false)
private String roleName;

Just use the @Id annotation which lets you define which property is the identifier of your entity. 只需使用@Id批注,该批注可让您定义哪个属性是您实体的标识符。 You don't need to use the @GeneratedValue annotation because I don't think you want hibernate to generate this property for you. 您不需要使用@GeneratedValue批注,因为我认为您不希望休眠为您生成此属性。

Even in the XML configuration based approach its an optional tag and can be skipped. 即使在基于XML配置的方法中,它也是一个可选标记,可以跳过。

@Id
@Column(name = "USER_ID",unique=true,columnDefinition="VARCHAR(64)")
private String userId;

This worked for me by representing the columnDefinition type with column annotation during the save or update. 在保存或更新过程中,我用列注释表示columnDefinition类型,从而为我工作。

you can enter this way, suppose position is primary key of that Salary entity,你可以这样输入,假设职位是那个薪水实体的主键,

@Id
@Column (name = "POSITION", nullable = false)
private String position;

then, it will work然后,它会工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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