java/Spring Hibernate 不更新实体

标签 java spring hibernate jpa persistence

我已经阅读并重读了 SO 和许多其他网站上的所有内容,但似乎无法弄清楚为什么我更新的对象没有更新。

我正在做的事情的基本概述:

  1. 服务层向 DAO 请求一些人员

  2. 从数据库(DAO/@Repository)返回People的ArrayList

  3. 服务层操作对象并将它们添加到新的数组列表中

  4. 服务层将新列表传递回 DAO 进行更新

  5. 没有任何更新

如果我在具有新值的对象中抛出一条日志消息,则 children 会正确地水合。我在代码中没有收到任何错误,只是没有提交我的更改。

这是一些代码:

PersonDAO 注释为@Repository

public void updatePeople(List<Person> people) {
    log.info("Updating " + people.size() + " people");
    try {
        Transaction tx = getCurrentSession().beginTransaction();
        for (Person person : people){
            getCurrentSession().saveOrUpdate(person);
        }
        getCurrentSession().flush();
        tx.commit();
        getCurrentSession().close();

    } catch (Exception e){
        log.error("Exception Updating all people " + e.toString());
        e.printStackTrace();
    }

}

public List<Person> getAssociatesByStoreId(String storeId) {
    try {

        List<Person> usersInStore = (List<Person>) getCurrentSession()
                .createCriteria(Person.class).createCriteria("store")
                .add(Restrictions.eq("storeId", storeId)).list();

        return usersInStore;
    } catch (Exception e) {
        log.error("exception in getAssociatesByStoreId ", e);
    }
    return null;
}

PersonService 注释为@Service - 相关方法

/* I put the people into a map to do some other logic on them */
for (Person person : personDAO.getAllPeople()){
            personMap.put(person.getEmployeeId() + "-" + person.getStore().getStoreId(), person);
}

/*    
 I iterate a list creating new Person objects (based on some random stuff), 
 including saving any children entities (managementcodes and stores) that need to be created
 After I have created a new Person I attempt to find it in the map from above.  
 If I find it pull it out of the map and put it into an array list 
 that is eventually passed back into the DAO
*/

注释为@Entity

private int personid;
private String firstName;
private String lastName;
private Store store;
private ManagementCode managementCode;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "personid", unique = true, nullable = false)
public int getPersonid() {
    return this.personid;
}

    /*a bunch of getters and setters*/
@ManyToOne(fetch = FetchType.LAZY, optional = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST})
@LazyCollection(LazyCollectionOption.EXTRA)
@JoinColumn(name = "managementlevel", nullable = true)
public ManagementCode getManagementCode() {
    return this.managementCode;
}

@ManyToOne(fetch = FetchType.LAZY, optional = true, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
//  @org.hibernate.annotations.Cascade( {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.PERSIST})
@JoinColumn(name = "storeid", nullable = false)
public Store getStore() {
    return this.store;
}

Store 注释为实体(管理代码相同)

/*fields + getters and setter */
@OneToMany(fetch = FetchType.LAZY, mappedBy = "store", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@LazyCollection(LazyCollectionOption.EXTRA)
@JsonIgnore 
public Set<Person> getPersons() {
    return this.persons;
}

编辑我在上面添加了两组级联类型注释,但仍然没有运气

编辑 2 更新了以显示主键定义 在我踢小狗之前请有人帮助我。 谢谢

最佳答案

JPA 的实体对象主要分为三种类型:

  1. 管理。这些是由 JPA 提供程序缓存的对象,因此 JPA 提供程序会跟踪它们的状态,并在事务提交时提交对它们所做的任何更改。这些是您查询时获得的对象。他们有一个数据库 ID。
  2. 新。这些是尚未保存在数据库中的实体对象。 JPA 提供者对它们一无所知。他们没有数据库 ID。
  3. 独立。这些对象曾经是受管理的,但现在不再受管理。通常,对象返回到事务外部的方法。这些对象有一个数据库 ID,但它们不是缓存的一部分,因此 JPA 提供者不知道它们。

为了保持一个实体与另一个实体之间的关系,必须管理引用的实体。有多种方法可以实现这一目标。

  1. 级联。从实体A到实体B的级联意味着当您在代码中对实体A执行级联操作时,JPA提供者将对实体B执行相同的操作。如果您添加CascadeType.MERGE关系,并让实体 A 引用分离的实体 B,然后对实体 A 执行 merge() 将首先对实体 B 执行 merge(),从而使其管理。当实体 A 被存储时,它具有对托管 B 的引用,并且关系被持久化。 (如果您还想保留对数据库中尚未存在的对象的引用,请同时添加 CascadeType.PERSIST。)
  2. 确保仅引用托管对象。在事务内部,加载引用的对象(托管对象)并在存储之前将引用替换为托管对象。
  3. 扩大您的交易范围。这样,引用的对象就永远不会分离。

关于java/Spring Hibernate 不更新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090486/

相关文章:

java - 如何通过java中的第二个参数对列表进行排序?

java - Spring Webservices 给出 406 错误

java - 为什么 Hibernate/JPA 在查询中报告空指针异常?

java - 如何以供应商中立的方式使用 JPA?

java - 如何使用选择列表缩短代码? Spring HTML

java - 我的 euler Project 函数结果错误

java - 使用 log4j 将 spring 消息从 sysout 获取到文件

java - 如何通过 Spring 使用脚本初始化内存中的 HSQLDB

java - Wildfly 和 MySQL 的不可重复读隔离级别

python 中的 java 的 printStackTrace() 等价物