Since the roleName
is not auto-generated, you should simply not annotate it with @GeneratedValue
:
@Id
@Column(name = "ROLE_NAME", nullable = false)
private String roleName;
Question
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?
La solution
Since the roleName
is not auto-generated, you should simply not annotate it with @GeneratedValue
:
@Id
@Column(name = "ROLE_NAME", nullable = false)
private String roleName;
Autres conseils
@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.
Just use the @Id
annotation which lets you define which property is the identifier of your entity. You don't need to use the @GeneratedValue
annotation because I don't think you want hibernate to generate this property for you.
Even in the XML configuration based approach its an optional tag and can be skipped.
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