Home > Mobile >  how to stop HQL hibernate insert multiple data inserted?
how to stop HQL hibernate insert multiple data inserted?

Time:06-11

Implementation: HQL hibernate. Im trying to save 1 data but what happen is its saving many data the only i stop it is by result > 1 to NullPointerException. I already put some session.close() still saving multiple.

Hibernate has save but where not gonna use that.

@Override
    public Product save(Product product) {
        Transaction transaction = null;
  
        Session session = HibernateUtil.getSessionFactory().openSession();

            transaction = session.beginTransaction();

            String hql = "INSERT INTO Product  ( productname, productbrand, productprice, productdescription, productquantity, productexpirationdate) "  
                "SELECT   productname, productbrand, productprice, productdescription, productquantity, productexpirationdate FROM Product ";
            Query query = session.createQuery(hql);
            Integer result =  query.executeUpdate();
        
            transaction.commit();
            session.close();

             if (result == 0 ||result == null  ) {
                   
                throw new NullPointerException();
            }
            
           // if (result > 1 || result >= 0 ||result == null  ) {
           //      
           ///  throw new NullPointerException();
            //}
            

        return product;
    }

Example in data in database insert 1 data then suddenly inserting multiple data. how to stop this?

'1', 'Hello1234', 'Hello', '2022-10-27 00:00:00', 'Hello1234', '1.4', '10', NULL
'2', 'Hello1234', 'Hello', '2022-10-27 00:00:00', 'Hello1234', '1.4', '10', NULL
'3', 'Hello1234', 'Hello', '2022-10-27 00:00:00', 'Hello1234', '1.4', '10', NULL
'4', 'Hello1234', 'Hello', '2022-10-27 00:00:00', 'Hello1234', '1.4', '10', NULL

Database query

CREATE TABLE `tb_product` (
  `purchase_item` int NOT NULL AUTO_INCREMENT,
  `productbrand` varchar(255) NOT NULL,
  `productdescription` varchar(255) NOT NULL,
  `productexpirationdate` datetime NOT NULL,
  `productname` varchar(255) NOT NULL,
  `productprice` double NOT NULL,
  `productquantity` int NOT NULL,
  PRIMARY KEY (`purchase_item`),
  UNIQUE KEY `purchase_item_UNIQUE` (`purchase_item`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

CodePudding user response:

fix getSessionFactory().openSession() to getSessionFactory().getCurrentSession()

reference link https://stackoverflow.com/questions/8046662/hibernate-opensession-vs-getcurrentsession#:~:text=openSession() always opens a,t need to close this.

CodePudding user response:

What's a weird query?

INSERT INTO Product ( productname, productbrand, productprice, productdescription, productquantity, productexpirationdate) 
SELECT  productname, productbrand, productprice, productdescription, productquantity, productexpirationdate 
FROM Product";

You select data from Product and insert it to the same Product table simultaneously. That's why!

  • Related