简体   繁体   中英

How to insert a row to an existing auto increment table in oracle?

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';

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

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.

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.

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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