Reputation: 271
I created my db tables with hibernate, and my user class has an auto generated id like as follwos.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_Id")
private int userId;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
I am trying to insert a user from toad console also I add the user_id value as manually. When I want to insert a new user from my application, I get an error as follow,
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (TDM.SYS_C001668192) violated
I have to insert some values from database with a procedure, so how to insert new rows with using the existing sequence or id (whatever)?
UPDATE:
I resolve the problem thanks to @Afridi as follow.
firstly, I added sequence annotation to user_id
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "t_user_id_seq_generator")
@SequenceGenerator(name = "t_user_id_seq_generator", sequenceName = "t_user_seq")
@Column(name = "user_Id")
private int userId;
after that I called it like that;
insert into t_user (user_id, username) values(T_USER_SEQ.nextval, 'newUser');
I checked it this sql;
select * from user_sequences where sequence_name = 'T_USER_SEQ';
Upvotes: 1
Views: 2373
Reputation: 271
I resolve the problem thanks to @Afridi as follow.
firstly, I added sequence annotation to user_id
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "t_user_id_seq_generator")
@SequenceGenerator(name = "t_user_id_seq_generator", sequenceName = "t_user_seq")
@Column(name = "user_Id")
private int userId;
after that I called it like that;
insert into t_user (user_id, username) values(T_USER_SEQ.nextval, 'newUser');
I checked it this sql;
select * from user_sequences where sequence_name = 'T_USER_SEQ';
Upvotes: 0
Reputation: 266
Hibernate use a sequence to generate ids for @GeneratedValue
field, and Hibernate will cache some ids which is selected from that sequence. So when you manually assign id for new row, this id may violate the cached id.
Upvotes: 1
Reputation: 31891
This have nothing to do with @GeneratedValue
annotation. Even if you have annotated your field with this annotation and you provide your own id, it will use that instead of generating it.
You're getting this error because of @Id
annotation. This annotation makes your field userId
unique. It's a primary key so you can't have duplicate entries.
What you can do is instead of calling session.save(entity)
you can call session.update(entity)
or session.saveOrUpdate(entity)
. It will either save it if it doesn't exists, otherwise update/overwrite it.
Upvotes: 1
Reputation: 2490
Hibernate automatically fills column markes with @GeneratedValue
. From doc:
The @GeneratedValue annotation specifies that the entity identifier value is automatically generated using an identity column, a database sequence, or a table generator. Hibernate supports the @GeneratedValue mapping even for UUID identifiers.
You can set type of @GeneratedValue
. See https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators
Upvotes: 1