gpt4 book ai didi

java - Hibernate 5 字节码增强关联管理仅朝一个方向工作

转载 作者:行者123 更新时间:2023-11-30 02:45:55 42 4
gpt4 key购买 nike

我有 2 个 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;
}

}

所有类均使用 hibernate 增强 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.Final User Guide 中据说

Bytecode-enhanced bi-directional association management makes that first example work by managing the "other side" of a bi-directional association whenever one side is manipulated.

引用“第一个示例”的位置

Example 204. Incorrect normal Java usage

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

最佳答案

在单元测试中,类可能没有得到增强,特别是当您通过 IDE 运行它们时。

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

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

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

无论如何,我认为这个功能并不是真的有必要。 Syncing both ends of the associations is the way to go ,并且只需要您提供addChild和removeChild方法。

关于java - Hibernate 5 字节码增强关联管理仅朝一个方向工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204113/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com