简体   繁体   English

如何在oracle中的现有自动增量表中插入一行?

[英]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,例如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. 我试图从蟾蜍控制台中插入用户,也将user_id值手动添加。 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)? 我必须使用过程从数据库中插入一些值,那么如何使用现有序列或ID(无论如何)插入新行?

UPDATE: 更新:

I resolve the problem thanks to @Afridi as follow. 我通过@Afridi解决了以下问题。

firstly, I added sequence annotation to user_id 首先,我向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; 我检查了这个SQL;

select * from user_sequences where sequence_name = 'T_USER_SEQ';

Hibernate automatically fills column markes with @GeneratedValue . Hibernate自动使用@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. @GeneratedValue批注指定使用标识列,数据库序列或表生成器自动生成实体标识符值。 Hibernate supports the @GeneratedValue mapping even for UUID identifiers. Hibernate即使对UUID标识符也支持@GeneratedValue映射。

You can set type of @GeneratedValue . 您可以设置@GeneratedValue类型。 See https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators 参见https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators

This have nothing to do with @GeneratedValue annotation. 这与@GeneratedValue批注无关。 Even if you have annotated your field with this annotation and you provide your own id, it will use that instead of generating it. 即使您已使用此注释对字段进行注释并提供了自己的ID,它也会使用该ID而不是生成它。

You're getting this error because of @Id annotation. 由于@Id注释,您遇到此错误。 This annotation makes your field userId unique. 此注释使您的字段userId唯一。 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) . 您可以做的是代替调用session.save(entity) ,而可以调用session.update(entity)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. Hibernate使用一个序列为@GeneratedValue字段生成ID,并且Hibernate将缓存从该序列中选择的一些ID。 So when you manually assign id for new row, this id may violate the cached id. 因此,当您手动为新行分配ID时,此ID可能会违反缓存的ID。

I resolve the problem thanks to @Afridi as follow. 我通过@Afridi解决了以下问题。

firstly, I added sequence annotation to user_id 首先,我向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; 我检查了这个SQL;

select * from user_sequences where sequence_name = 'T_USER_SEQ';

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

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