简体   繁体   English

Hibernate saveOrUpdate不更新而是创建新记录

[英]Hibernate saveOrUpdate does not update instead creates new record

I have 2 tables mapped bidirectional as follows 我有2个表双向映射如下

@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 . 我已从saveOrUpdate更改为更新,但仍处于休眠状态,正在插入新的子记录。

What am I doing wrong? 我究竟做错了什么?

For your method getTestPlan, 对于您的方法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. 在执行查询之前不应该关闭会话,因此您将获得SessionException而不是预期的testPlan ,并休眠保存非持久实体。

From this question , you may find that saveOrUpdate() does the following: 这个问题中 ,您可能会发现saveOrUpdate()执行以下操作:

  • 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 如果对象由或进行版本控制,并且版本属性值与分配给新实例化的对象的值相同,则save()否则将对象更新()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate saveOrUpdate不更新现有记录,而是将记录另存为新条目 - Hibernate saveOrUpdate does not update existing record instead it saves the record as new entry 在Hibernate中使用saveOrUpdate()创建新记录而不是更新现有记录 - Using saveOrUpdate() in Hibernate creates new records instead of updating existing ones 休眠 saveOrUpdate 不更新 - Hibernate saveOrUpdate does not update 休眠会话saveOrUpdate不更新 - Hibernate Session saveOrUpdate does not update Hibernate saveOrUpdate-method创建新的条目/行而不是更新现有的条目/行 - Hibernate saveOrUpdate-method creates new entry/row instead of updating the existing one 如果somcolumn!=“ somevalue”,则使用Hibernate saveorupdate()更新记录。 - Update record if somcolumn!=“somevalue”, using Hibernate saveorupdate() Spring Hibernate一对一关系在子表上创建新记录,而不是更新 - Spring Hibernate One-To-One relationship creates new record on child table instead of update saveorupdate()不会在hibernate中更新集合(列表)一对多映射 - saveorupdate() does not update collection (list) one-to-many mapping in hibernate Hibernate saveOrUpdate创建一个对象的两个实例 - Hibernate saveOrUpdate creates two instances of an object Hibernate为每个OneToMany连接创建新的记录 - Hibernate creates new Record for each OneToMany connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM