Hibernate合并和更新

问题描述 投票:0回答:1

我一直在尝试使用Hibernate更新表行但没有成功。我有一行的值不是null。我想只更新null值并保持其余值相等。我该如何解决这个问题?

我是Hibernate的新手,我不想使用HQL

// Transaction is a parameter
// Transaction contains the values I want to update with the null columns
// but it does not contain values already saved, so the fields are null   

Session session = SessionUtil.getSession();
Transaction tx = null;

try{

    tx = session.beginTransaction();

    Criteria criteria = session.createCriteria(com.business.database.Transaction.class);
    com.business.database.Transaction  savedTransaction = (com.business.database.Transaction) criteria.add(Restrictions.eq("txReference", transaction.getTxReference())).uniqueResult();

    savedTransaction = (com.business.database.Transaction) session.merge(transaction);
    session.update(savedTransaction);

    tx.commit();
    session.close(); 


    HashMap<String, Object> txMap = new HashMap<String, Object>();
    txMap.put("success", true);
    txMap.put("savedtx", savedTransaction);
    txMap.put("updatedtx", transaction);
    return txMap;

} catch (HibernateException e) {

    try {
       session.close(); 
    }  catch(Exception ex) {

    }
    System.out.println("hibernate: "+ e.getMessage());
    throw new ServiceException(ServiceStatus.FAILED_TO_UPDATE_TRANSACTION, Response.Status.BAD_REQUEST);

}
hibernate
1个回答
0
投票

我一直在尝试使用hibernate更新表行但没有成功。我有一行值不同的行,当我想只更新空值并保留其余值时。


修改附加对象

在Hibernate中,附加到会话are automatically updated的对象。如果需要更改某些atttributes的值,可以设置值,并在提交时自动将对象存储在数据库中。

// 'id' is the id of the person to modify
// 'value' is the new value for a property
public void modifyPersonProperty(Integer id, String value) {
    Session session = SessionUtil.getSession();

    // When you read the object from the database (even with a Criteria query).
    // it will be attached to a session.
    Person person = session.get(Person.class, id);

    // you can modify the object directly
    Transaction tx = session.beginTransaction();
    person.setProperty(value);
    tx.commit();

    session.close();

}

用分离的对象替换数据

如果要使用分离对象(例如,在其他模块中创建或从用户界面获取的对象)中的值替换附加对象(即数据库中的对象),则可以使用merge

merge method does

  • 通过从传递的对象获取的id查找实体实例(检索来自持久化上下文的现有实体实例,或从数据库加载的新实例);
  • 将传递的对象中的字段复制到此实例;
  • 返回新更新的实例。

例如,

// 'newPerson' is the object with the new person. It has the id of the person to modify
public void modifyPerson(Person newPerson) {

    // you do not need to find the object

    Session session = SessionUtil.getSession();
    Transaction tx = session.beginTransaction();
    session.merge(newPerson);
    tx.commit();

    session.close();

}

请注意,merge返回一个对象。例如,您可以使用该对象获取生成的id。如果您不需要获取更多信息,则可以忽略返回的对象。


分离和附加对象

Hibernate有一个update方法,在其他JPA实现中不存在。如果您有从会话中获取的对象(即附加对象),则可以分离该对象并再次重新附加。

// the original object is transient
Person person = new Person();
person.setName("John");

// when I store the object, it turns attached
session.save(person);

// I can use evict to detach the object
session.evict(person);

// If I change a value in the object, Hibernate will not
// update the database automatically
person.setName("Mary");

// I can attach again the object to force Hibernate to 
// update the object
session.update(person);

如果你试图update一个瞬态的对象,即在会话之前没有存储或附加的对象,你将得到一个例外。

// When I create a Person, it is transient
Person person = new Person();
person.setName("John");

// If I wanna use update, i will get an exception
session.update(person); // PersistenceException!
© www.soinside.com 2019 - 2024. All rights reserved.