简体   繁体   English

postgres id 生成和休眠的问题

[英]Issues with postgres id generation and hibernate

For some reason when I insert to database from application id is always 0. This is table used for spring security auth table users, copied from pgadmin II:出于某种原因,当我从应用程序 id 插入数据库时​​总是 0。这是用于 spring 安全认证表用户的表,从 pgadmin II 复制:

 id serial NOT NULL,
  username character varying(100),
  password character varying(200),
  groupfk integer,
  CONSTRAINT users_pk PRIMARY KEY (id ),
  CONSTRAINT users_groupfk_fkey FOREIGN KEY (groupfk)
  REFERENCES groups (id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION

I have a following in my User domain class, relevant ID part :我的用户域类中有以下相关 ID 部分:

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @Generated(GenerationTime.INSERT)
    public int getId() {
        return id;
    }

    @ManyToOne
@JoinColumn(name = "groupfk")
public Group getGroup() {
    return group;
}

public void setGroup(Group group) {
    this.group = group;
}

So I'm inserting user like this :所以我插入这样的用户:

User user = new User();
        user.setPassword(password);
        user.setUsername(username);

        Group group = new Group();
        group.setId(usergroup);
        user.setGroup(group);
                dao.saveUser(user);

Dao method impl :道法实现:

public void save(User user) {
        Session session = sessionFactory.getCurrentSession();
        session.save(user);
    }

So here is the exception I get :所以这是我得到的例外:

Could not execute JDBC batch update; SQL [insert into users (groupfk, password, username, id) values (?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

Real query :真实查询:

Batch entry 0 insert into users (groupfk, password, username, id) values ('1', '098f6bcd4621d373cade4e832627b4f6', 'test3', '0') was aborted.  Call getNextException to see the cause.

So I inserted previous user with id 0 and it was successful.所以我插入了 id 为0前一个用户,它成功了。 And I tried to insert another one but because it had the same ID this error occurred.我尝试插入另一个,但因为它具有相同的 ID,所以发生了这个错误。

Normally if I try to insert it with sql trough sql client (pgadmin) it works, this works as expected :通常,如果我尝试使用 sql 槽 sql 客户端(pgadmin)插入它,它会正常工作,这会按预期工作:

insert into users (username, password, groupfk) VALUES ('test', 'c20ad4d76fe97759aa27a0c99bff6710',1);

So hibernate for some reason doesn't generate my id appropriately, the truth is if the above statement is working it shouldn't generate it at all.因此,出于某种原因,休眠无法正确生成我的 id,事实是,如果上述语句有效,则根本不应该生成它。

What do I do?我该怎么办?

Your ID should be an Integer , not an int .您的 ID 应该是一个Integer ,而不是一个int Everytime you create an instance of your class, it initializes the value of this.id to 0. Hibernate then will think of this object as already being persisted, but disconnected.每次创建类的实例时,它都会将this.id的值初始化为 0。然后 Hibernate 会认为该对象已经被持久化,但已断开连接。 You want it to be treated as transient, which is indicated by having a null ID.您希望将其视为瞬态,这通过具有空 ID 来表示。 See a discussion of Hibernate objects states in the manual .请参阅手册中有关Hibernate 对象状态的讨论。

When you session.save(user) , Hibernate will see the ID is null, realize the object is transient, generate an ID for it, and save it.当你session.save(user) ,Hibernate 会看到 ID 为空,意识到对象是瞬态的,为它生成一个 ID,并保存它。

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

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