简体   繁体   中英

Primary key not updating when inserting data to database using hibernate

I'm new to hibernate and postgres. I need help regarding primary key in hibernate. This is the problem: When I inserted data using persist() for the first time, it was a success. Now, when I inserted the next data, my IDE gave me this error:

Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique
constraint "test1_tbl2_pkey"
Detail: Key (id)=(0) already exists.

It seems like the value in the sequence is not used. Because the initial value of a sequence in postgres is 1. Also, when I looked at the tables, their primary key values are 0. How to tell hibernate to use the sequence in my postgres database? I'm new to both technologies thus, I'm having a hard time solving this problem.

tbl1_mappings.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
<class name="pojo.Test1Tbl1" table="test1_tbl1" schema="public">
  
  <id name="id" type="integer" column="id">
     <generator class="increment"/>
  </id>
  
  <property name = "name" column = "tbl1_name" type = "string"/>
  <one-to-one name="tbl2"></one-to-one>
  
</class>
</hibernate-mapping>

tbl2_mappings.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
<class name="pojo.Test1Tbl2" table="test1_tbl2" schema="public">
  
  <id name = "id" type = "integer" column = "id">
     <generator class="foreign"/>
  </id>
  
  <property name = "name" column = "tbl2_name" type = "string"/>
  
</class>
</hibernate-mapping>

Event code of my button

button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                Test1Tbl2 tbl2 = new Test1Tbl2(field2.getText());
                Test1Tbl1 tbl1 = new Test1Tbl1(field1.getText(), tbl2);
                
                Session session = factory.openSession();
                Transaction tx = session.beginTransaction();
                session.persist(tbl2);
                session.persist(tbl1);
                tx.commit();
            }
            catch(HibernateException ex) {
                ex.printStackTrace();
            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
        }

After I've done my intensive research, I found a solution. Although, this solution doesn't use the persist() method. However, this solution does update the primary key via postgres sequence. Btw, I'm using Hibernate 6.1.2.Final version.

This is the code that I came up with:

Transaction tx = null;
            try (Session session = factory.openSession()){
                tx = session.beginTransaction();
                String query = "INSERT INTO test1_tbl1(tbl1_name)"  + 
                         "VALUES ('"+field1.getText()+"');";
                MutationQuery mq = session.createNativeMutationQuery(query);
                int result = mq.executeUpdate();
                System.out.println("Rows affected: " + result);
                tx.commit();
            }
            catch(Exception ex) {
                if (tx!=null) tx.rollback();
                ex.printStackTrace();
            }

Take note that I severed the relationship between test1_tbl1 and test1_tbl2 tables and I only used test1_tbl1 to make my answer more clear. This article helps me to create the solution above: MutationQuery and SelectionQuery

Other articles propose to use the 'sequence' generator class in the mapping file to fix the problem. Ex:

<id name="id" type="integer" column="id">
     <generator class="sequence">
       <param name="sequence-name">sequence_name</param>
     </generator>
  </id>

Although, this solution doesn't work for me. I also tried using different types of generator classes like native, identity and auto; still they didn't work.

These are some articles that I've stumbled upon that are related to the problem mentioned in this thread:

Hibernate doesn't use PostgreSQL sequence to generate primary key

JPA and PostgreSQL with GenerationType.IDENTITY

How To Use Sequence In Hibernate As A Property In XML Mapping

HIbernate 5: generator class="sequence" not working

Migrating Hibernate 3 to 5: relation hibernate_sequence does not exist

JPA GenerationType.AUTO not considering column with auto increment

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