Hibernate transaction duplication issue

I want to update some of my tables in the database and I want all these works to be performed in one transaction, first of all, I delete some record in branchbuildin (Table) and insert a new one after this action. A problem occurred when I insert and enter with that the same name of the building and branch_fk (because of this I have this restriction in this table (uniqueConstraints = {@UniqueConstraint (columnNames = {"buildingname", "branch_fk"})})), but when I don’t know, t use the session sleep mode and use normal JDBC transaction. I do not have this problem.

List<Integer> allBranchBuilding = branchBuildingDao.getAllBranchBuildingID(pkId, sess);
            for (Integer integer : allBranchBuilding) {
                branchBuildingDao.delete(integer, sess); // delete kardane tamame BranchBuilding ha va tel haie aanha
            }

            Address myAdr = new Address();
            setAddress(myAdr, centralFlag, city, latit, longit, mainstreet, remainAdr, state);
            BranchBuildingEntity bbe = new BranchBuildingEntity();
            setBranchBuildingEntity(bbe, be, myAdr, city, centralFlag, latit, longit, mainstreet, buildingName, remainAdr, state, des);
            branchBuildingDao.save(bbe, sess);//Exception Occurred                

I get my session with the first method:

        Session sess = null;
        sess = HibernateUtil.getSession();
        Transaction tx = sess.beginTransaction();
+3
1

, Hibernate.

, . , .

, , , .

, (.. ) , ( , - , - ):

sess.flush();
// sess.clear(); // if needed or convenient for you
branchBuildingDao.save(bbe, sess);

, , .

, ( , ...) ( , ), :

  • Loop , ( sess.delete(e)) "".
  • 50 ( , ) ( ):
    • , Hibernate ,
    • "", ( sess.evict(e)).
    • "deleteeds".

, flush SQL . - .

+6

Source: https://habr.com/ru/post/1714117/


All Articles