Hibernate不在@DataJpaTest中创建代理



我有一个非常简单的测试,用于测试在正常运行时正常工作的spring数据存储库。我真的不认为做这件事应该这么困难,但是我不明白我做错了什么,请帮助。

当我尝试测试这个存储库时,我开始收到如下错误:

由:org.hibernate.HibernateException: Generation of .造成配置时不允许在运行时使用HibernateProxy实例BytecodeProvider为'none';你的模型需要更高级的BytecodeProvider要启用。在org.hibernate.bytecode.internal.none.DisallowedProxyFactory.getProxy (DisallowedProxyFactory.java: 37)在org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy (AbstractEntityTuplizer.java: 746)在org.hibernate.persister.entity.AbstractEntityPersister.createProxy (AbstractEntityPersister.java: 5049)

似乎hibernate不能为实体类创建代理,因为由于某种原因它已经为代理工厂分配了一个DisallowedProxyFactory实现。所以我说这款:

spring.jpa.properties.hibernate.enhancer.enableDirtyTracking = truespring.jpa.properties.hibernate.enhancer.enableLazyInitialization = true

但是现在我只是收到这个错误:

由:java.lang.IllegalStateException: Cannot apply class引起没有LoadTimeWeaver的变压器org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer (SpringPersistenceUnitInfo.java: 83)

所以我添加了@EnableLoadTimeWeaving到测试类,现在我收到这个错误

肇因:java.lang.IllegalStateException: ClassLoader[jdk.internal.loader.ClassLoaders$AppClassLoader]不提供addTransformer (ClassFileTransformer)的方法。指定自定义LoadTimeWeaver或启动Java虚拟机与Spring的代理:- 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持久化配置类

@Configuration
@PropertySource("classpath:application-liquibase.properties")
@EntityScan("com.nflp.processingapplication.main.modules.persistence.sql")
public class RdsPersistenceConfigration {
}

根据@M的建议更新测试。Denium

@DataJpaTest
@Transactional
@TestPropertySource(properties = "spring.liquibase.change-log=classpath:db/changelog/changelog.master.xml")
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;

好吧,我终于找到了解决方案,原因是我甚至没有怀疑,我的应用程序使用spring原生来创建优化的生产构建,显然它以某种方式干预了应用程序的开发构建过程。现在,我只是把它从我的应用程序中删除了。稍后,我可能会尝试分离开发构建。Gradle from the production .

最新更新