Search code examples
javasqloracle-databasehibernatehibernate-annotations

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

Solution

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