0

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

SpicySandwich
  • 49
  • 1
  • 10
  • Perhaps you want some unique constraints in your table? – Kayaman Jun 09 '22 at 17:24
  • @Kayaman I tried the ID as unique constraints but its still desame multiple saving – SpicySandwich Jun 09 '22 at 17:34
  • Yes but you also have it as auto increment. – Kayaman Jun 09 '22 at 17:36
  • @Kayaman yes, i also updated my post question with database query – SpicySandwich Jun 09 '22 at 17:45
  • is the table initially empty before you insert data? because in your query you inserting the existing data, or you can say that it will duplicate existing data – hphp Jun 09 '22 at 17:51
  • @hphp yes its empty. and yes i just notice just now it will duplicate existing data – SpicySandwich Jun 09 '22 at 17:56
  • So everything seems to be in order. You're inserting new rows and they're getting inserted. Since you don't have any unique columns besides the PK, you get duplicates. The primary key is autoincremented for each insert. – Kayaman Jun 09 '22 at 17:59
  • so by `suddenly inserting multiple data`, did you call the method multiple times and expect it is not inserting duplicate data? or did you call the method once, but the data inserted multiple times? – hphp Jun 09 '22 at 18:03
  • @hphp i call it 1 time then its saving multiple time and duplicating other data. – SpicySandwich Jun 09 '22 at 18:09
  • idk, i don't see where you set your `product` to your query, but maybe for now can you try creating the query using `PreparedStatement` first? just to make sure it didn't duplicated because of your query – hphp Jun 09 '22 at 18:15
  • @hphp here for full detail this link [https://stackoverflow.com/questions/72561268/hql-orm-hibernate-implementation-error-null ] – SpicySandwich Jun 09 '22 at 18:23

2 Answers2

0

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

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

SpicySandwich
  • 49
  • 1
  • 10
0

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!

v.ladynev
  • 19,275
  • 8
  • 46
  • 67