简体   繁体   中英

Hibernate saveOrUpdate does not update instead creates new record

I have 2 tables mapped bidirectional as follows

@Entity
@Table(name="testplan")
public class TestPlan {

    @Id
    @Column(name="testplan_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int testplan_id;
    @OneToMany(mappedBy="testplan_id",cascade=CascadeType.ALL)
    private List<TestPlanDetails> testPlanDetails;
//getters and setters
}

@Entity
@Table(name="TestPlanDetails")
public class TestPlanDetails {

    @Id
    @Column(name="testplan_details_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int testplan_details_id;



    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="testplan_id")
    private TestPlan testplan_id;

    // getters and setters
    }

this is my service class

TestPlan testPlan=testPlanDao.getTestPlan(id, testPlanVersion);
        //saveGeneralInfo(rgi, testPlan);
        TestPlanGeneralInfo tpgi=testPlanGeneralInfo(rgi, testPlan);



        List<TestPlanDetails> testPlanDetails=getDetails(list,testPlan);
        testPlan.setTestPlanDetails(testPlanDetails);
        testPlanDao.updateTestPlan(testPlan);

this is my testplandao methods

public TestPlan getTestPlan(int  id,String testPlanVersion )
{

    String hql="from TestPlan tp where tp.id=:id and tp.testplan_version=:testPlanVersion";
    Query query=currentSession().createQuery(hql);
    query.setParameter("id", id);
    query.setParameter("testPlanVersion", testPlanVersion);
    closeSession();
    return (TestPlan) query.uniqueResult();
}

@Override
public void updateTestPlan(TestPlan tp) 
{
    // TODO Auto-generated method stub
            Session session=currentSession();
            Transaction tx=null;
        try{
                    tx=session.beginTransaction();
                    session.saveOrUpdate(tp);
                    session.flush();
                    session.clear();
                    tx.commit();
            }
        catch(HibernateException eq)
        {
            tx.rollback();
            eq.printStackTrace();

        }
            catch(Exception e)
            {
                tx.rollback();
                e.printStackTrace();


            }
        finally
        {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }

}

The problem I am facing is hibernate is inserting new records in child table. I have changed from saveOrUpdate to update, still hibernate is inserting new child records .

What am I doing wrong?

For your method getTestPlan,

public TestPlan getTestPlan(int  id,String testPlanVersion )
{

    String hql="from TestPlan tp where tp.id=:id and tp.testplan_version=:testPlanVersion";
    Query query=currentSession().createQuery(hql);
    query.setParameter("id", id);
    query.setParameter("testPlanVersion", testPlanVersion);
    closeSession();
    return (TestPlan) query.uniqueResult();
}

Session shouldn't be closed before query execute, so you would get SessionException but not expected testPlan , and hibernate save the un-persist entity.

From this question , you may find that saveOrUpdate() does the following:

  • if the object is already persistent in this session, do nothing
  • if another object associated with the session has the same identifier, throw an exception
  • if the object has no identifier property, save() it
  • if the object's identifier has the value assigned to a newly instantiated object, save() it
  • if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object

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