繁体   English   中英

Hibernate 5字节码增强关联管理仅在一个方向上工作

[英]Hibernate 5 Bytecode Enhancement Association Management works just in one direction

我有两个这样映射的JPA实体:

@MappedSuperclass
public class AbstractEntity {

    private static final String INCREMENT_STRATEGY = "increment";

    private static final String INCREMENT_ID_GENERATOR_NAME = "INCREMENT_ID_GENERATOR";

    @Id
    @GenericGenerator(name = INCREMENT_ID_GENERATOR_NAME, strategy = INCREMENT_STRATEGY)
    @GeneratedValue(generator = INCREMENT_ID_GENERATOR_NAME)
    private Long id;

    public AbstractEntity() {
        super();
    }

    public Long getId() {
        return id;
    }
}

@Entity
public class Department extends AbstractEntity{


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "department")
    private List<Employee> employees = new ArrayList<Employee>();

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

}

@Entity
public class Employee extends AbstractEntity {

    @ManyToOne(optional = true, cascade= CascadeType.ALL)
    @JoinColumn(name = "DEPARTMENT_ID")
    private Department department;

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Department getDepartment() {
        return department;
    }

}

所有类都使用休眠增强maven插件进行字节码增强:

<build>
    <plugins>
        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <version>5.2.2.Final</version>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <version>5.2.2.Final</version>
                </dependency>
            </dependencies>
            <configuration>
                <enableDirtyTracking>true</enableDirtyTracking>
                <enableLazyInitialization>true</enableLazyInitialization>
                <enableAssociationManagement>true</enableAssociationManagement>
            </configuration>
        </plugin>
    </plugins>
</build>

我运行两个测试以验证增强类是否正常工作:

@Test
public void test1() {
    Department department = new Department();
    Employee employee = new Employee();
    department.getEmployees().add(employee);
    assertThat(employee.getDepartment(), is(not(nullValue())));
}

@Test
public void test2() {
    Department department = new Department();
    Employee employee = new Employee();
    employee.setDepartment(department);
    assertThat(department.getEmployees().size(), is(1));
    assertThat(department.getEmployees().get(0), is(employee));
}

只有第二次测试成功通过,因此在通过父级集合操作关联的同时,不会更新子级的父级字段,而在《 Hibernate ORM 5.2.3》中则是《 最终用户指南》。

字节码增强的双向关联管理通过操纵双向关联的“另一侧”来管理第一个示例。

所引用的“第一个示例”是

例子204.普通Java用法不正确

为什么在我的test1案例中,关联管理不起作用? 我做错了什么?

在单元测试中,可能会发生未增强类的情况,尤其是在通过IDE运行它们时。

确保增强的类包含在您在进行测试的项目中导入的其他模块中。

或者,您可以运行增强过程,验证类是否得到增强,然后才运行单元测试。

总而言之,我想您可能正在运行实体类的未增强版本。

无论如何,我认为此功能并不是真正必要的。 同步关联的两端都是一种方法 ,它仅需要您提供addChild和removeChild方法。

追踪Andrei的JIRA问题后,我了解到:

要触发关联管理,有时必须更改* ToMany字段,即使该字段具有相同的集合。 集合本身不会跟踪更改。

因此,代替:

customer.getInventories().add( customerInventory );

需要调用设置器:

Collection<CustumerInventory> inventories = customer.getInventories();
inventories.add( customerInventory ); 
custumer.setInventories( inventories );

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM