Hibernate 不在@DataJpaTest 中创建代理
Hibernate does not create proxy within @DataJpaTest
我有一个非常简单的测试,用于测试 spring 数据存储库在正常运行时工作正常。我真的不认为做这件事应该这么难,但我不明白我做错了什么,请帮忙。
当我尝试测试这个存储库时,我开始收到类似这样的错误:
Caused by: org.hibernate.HibernateException: Generation of
HibernateProxy instances at runtime is not allowed when the configured
BytecodeProvider is 'none'; your model requires a more advanced
BytecodeProvider to be enabled. at
org.hibernate.bytecode.internal.none.DisallowedProxyFactory.getProxy(DisallowedProxyFactory.java:37)
at
org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:746)
at
org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:5049)
hibernate 似乎无法为实体 classes 创建代理,因为出于某种原因已为代理工厂分配了 DisallowedProxyFactory 实现。所以我添加了这个配置:
spring.jpa.properties.hibernate.enhancer.enableDirtyTracking=true
spring.jpa.properties.hibernate.enhancer.enableLazyInitialization=true
但现在我只是收到了这个错误:
Caused by: java.lang.IllegalStateException: Cannot apply class
transformer without LoadTimeWeaver specified at
org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:83)
所以我在测试中添加了@EnableLoadTimeWeaving class,现在我收到这个错误
Caused by: java.lang.IllegalStateException: ClassLoader
[jdk.internal.loader.ClassLoaders$AppClassLoader] does NOT provide an
'addTransformer(ClassFileTransformer)' method. Specify a custom
LoadTimeWeaver or start your Java virtual machine with Spring's agent:
-javaagent:spring-instrument-{version}.jar
初始测试设置:
@DataJpaTest
@Transactional
@Import({RdsPersistenceConfigration.class})
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;
@Test
@Sql(scripts = "classpath:sql/division-repository-test.sql")
void crudOperations() {
// test case logic
}
}
部门实体:
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "division")
public class Division {
private transient static final int HASH_CODE = Division.class.hashCode();
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "division_name", nullable = false)
private String divisionName;
@OneToMany(mappedBy = "division", fetch = FetchType.LAZY)
private Set<Branch> branches = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tenant_id", nullable = false)
private Tenant tenant;
public void setTenant(Tenant tenant) {
if (tenant != null) {
this.tenant = tenant;
tenant.addDivision(this);
} else {
if (this.tenant != null) this.tenant.removeDivision(this);
this.tenant = tenant;
}
}
@Transient
public void addBranch(Branch branch) {
if (branch != null) {
if (branch.getDivision() != this) {
branch.getDivision().removeBranch(branch);
}
branches.add(branch);
}
}
@Transient
public void removeBranch(Branch branch) {
if (branch != null) {
branches.remove(branch);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Division division = (Division) o;
return Objects.equals(id, division.id);
}
@Override
public int hashCode() {
return Division.HASH_CODE;
}
}
存储库:
public interface DivisionRepository extends JpaRepository<Division, Integer> {
Page<Division> findAll(Pageable pageable);
}
Rds 持久性配置 class
@Configuration
@PropertySource("classpath:application-liquibase.properties")
@EntityScan("com.nflp.processingapplication.main.modules.persistence.sql")
public class RdsPersistenceConfigration {
}
根据@M 的建议更新了测试。丹姆
@DataJpaTest
@Transactional
@TestPropertySource(properties = "spring.liquibase.change-log=classpath:db/changelog/changelog.master.xml")
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;
好的,我终于找到了解决方案,原因出在我什至没有怀疑的地方,我的应用程序使用 spring 本机来创建优化的生产构建,显然它以某种方式干预了开发构建过程应用程序。现在,我刚刚将它从我的应用程序中删除。
稍后我可能会尝试将开发 build.gradle 与生产分开。
我有一个非常简单的测试,用于测试 spring 数据存储库在正常运行时工作正常。我真的不认为做这件事应该这么难,但我不明白我做错了什么,请帮忙。
当我尝试测试这个存储库时,我开始收到类似这样的错误:
Caused by: org.hibernate.HibernateException: Generation of HibernateProxy instances at runtime is not allowed when the configured BytecodeProvider is 'none'; your model requires a more advanced BytecodeProvider to be enabled. at org.hibernate.bytecode.internal.none.DisallowedProxyFactory.getProxy(DisallowedProxyFactory.java:37) at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:746) at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:5049)
hibernate 似乎无法为实体 classes 创建代理,因为出于某种原因已为代理工厂分配了 DisallowedProxyFactory 实现。所以我添加了这个配置:
spring.jpa.properties.hibernate.enhancer.enableDirtyTracking=true spring.jpa.properties.hibernate.enhancer.enableLazyInitialization=true
但现在我只是收到了这个错误:
Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:83)
所以我在测试中添加了@EnableLoadTimeWeaving class,现在我收到这个错误
Caused by: java.lang.IllegalStateException: ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-instrument-{version}.jar
初始测试设置:
@DataJpaTest
@Transactional
@Import({RdsPersistenceConfigration.class})
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;
@Test
@Sql(scripts = "classpath:sql/division-repository-test.sql")
void crudOperations() {
// test case logic
}
}
部门实体:
@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "division")
public class Division {
private transient static final int HASH_CODE = Division.class.hashCode();
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "division_name", nullable = false)
private String divisionName;
@OneToMany(mappedBy = "division", fetch = FetchType.LAZY)
private Set<Branch> branches = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tenant_id", nullable = false)
private Tenant tenant;
public void setTenant(Tenant tenant) {
if (tenant != null) {
this.tenant = tenant;
tenant.addDivision(this);
} else {
if (this.tenant != null) this.tenant.removeDivision(this);
this.tenant = tenant;
}
}
@Transient
public void addBranch(Branch branch) {
if (branch != null) {
if (branch.getDivision() != this) {
branch.getDivision().removeBranch(branch);
}
branches.add(branch);
}
}
@Transient
public void removeBranch(Branch branch) {
if (branch != null) {
branches.remove(branch);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Division division = (Division) o;
return Objects.equals(id, division.id);
}
@Override
public int hashCode() {
return Division.HASH_CODE;
}
}
存储库:
public interface DivisionRepository extends JpaRepository<Division, Integer> {
Page<Division> findAll(Pageable pageable);
}
Rds 持久性配置 class
@Configuration
@PropertySource("classpath:application-liquibase.properties")
@EntityScan("com.nflp.processingapplication.main.modules.persistence.sql")
public class RdsPersistenceConfigration {
}
根据@M 的建议更新了测试。丹姆
@DataJpaTest
@Transactional
@TestPropertySource(properties = "spring.liquibase.change-log=classpath:db/changelog/changelog.master.xml")
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;
好的,我终于找到了解决方案,原因出在我什至没有怀疑的地方,我的应用程序使用 spring 本机来创建优化的生产构建,显然它以某种方式干预了开发构建过程应用程序。现在,我刚刚将它从我的应用程序中删除。 稍后我可能会尝试将开发 build.gradle 与生产分开。