简体   繁体   English

Hibernate + PostgreSQL +串行PK(ID)-无法将对象保存到DB

[英]Hibernate + PostgreSQL + serial PK(ID) - cannot save object to DB

can sameone help me with problem with saving object to PostgreSQL databse? Sameone可以帮助我解决将对象保存到PostgreSQL数据库的问题吗? I use serial id and annotation @SequenceGenerator cannot get me some id from sequence, witch have been created automaticaly. 我使用序列号和注释@SequenceGenerator无法从序列中获取一些ID,已经自动创建了。 (serial) The sequence is "user_id_user_sec". (序列)序列为“ user_id_user_sec”。 I can save one entity, but second throw exception: 我可以保存一个实体,但是第二个抛出异常:

ERROR:   ERROR: duplicate key value violates unique constraint "user_pkey"
  Detail: Key (id_user)=(0) already exists.
Info:   HHH000010: On release of batch it still contained JDBC statements
Severe:   org.hibernate.exception.ConstraintViolationException: could not execute  statement
    at      org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelega te.java:129)

My entity, witch I can save to database: 我的实体,我可以保存到数据库中:

@Entity
@Table(name = "\"User\"")
public class User implements java.io.Serializable {

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq")
   @SequenceGenerator(name="seq", sequenceName="user_id_user_seq", allocationSize=1)
   private int idUser;
   @Column
   private String apiKey;
   @Column
   private String email;
   @Column
   private String name;
   @Column
   private String password;
   @Column
   private boolean isAdmin;
   @Column
   private boolean enabled;
   private Set deviceWatchedByUsers = new HashSet(0);

and getters and setters and my UserDAO's method, witch save this entity is: 以及getter和setter以及我的UserDAO的方法,除此实体外:

public static Integer addUser(User u){
  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction tx = null;
  Integer userId = null;
  try{
     tx = session.beginTransaction();
     User user = u;

     userId = (Integer) session.save(user); 
     tx.commit();
  }catch (HibernateException e) {
     if (tx!=null) tx.rollback();
     e.printStackTrace(); 
  }finally {
     session.close(); 
  }
  return userId;

} }

And script to create database is: 创建数据库的脚本是:

CREATE TABLE "user"
(
  id_user serial NOT NULL,
  api_key character varying(255),
  email character varying(255) NOT NULL,
  name character varying(255) NOT NULL,
  password character varying(255) NOT NULL,
  is_admin boolean NOT NULL,
  enabled boolean NOT NULL,
  CONSTRAINT user_pkey PRIMARY KEY (id_user),
  CONSTRAINT user_email_key UNIQUE (email)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE "user"
  OWNER TO postgres;

You need to change the DB table id_user to: 您需要将数据库表id_user更改为:

id_user bigint NOT NULL

You don't need the serial type, since it's Hibernate assigning the id column using a sequence call. 您不需要串行类型,因为它是Hibernate使用序列调用分配id列的。

If you wanted to have the database in charge of assigning ids, then the SEQUENCE generator would simply compete against the database id generation strategy. 如果您想让数据库负责分配ID,则SEQUENCE生成器将与数据库ID生成策略竞争。

In that case you would need a "select" generator instead. 在这种情况下,您将需要一个“选择”生成器

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

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