我有一個非常簡單的測驗來測驗在正常運行時正常作業的 spring 資料存盤庫。我真的不認為這樣做應該這么困難,但我不明白我做錯了什么,請幫忙。
當我嘗試測驗此存盤庫時,我開始收到類似以下內容的錯誤:
引起:org.hibernate.HibernateException:當配置的 BytecodeProvider 為“none”時,不允許在運行時生成 HibernateProxy 實體;您的模型需要啟用更高級的 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=true spring.jpa.properties.hibernate.enhancer.enableLazyInitialization=true
但現在我只是收到這個錯誤:
引起:java.lang.IllegalStateException:無法在沒有在 org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:83) 指定的 LoadTimeWeaver 的情況下應用類轉換器
所以我在測驗類中添加了@EnableLoadTimeWeaving,現在我收到這個錯誤
引起:java.lang.IllegalStateException:ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader] 不提供“addTransformer(ClassFileTransformer)”方法。指定自定義 LoadTimeWeaver 或使用 Spring 的代理啟動 Java 虛擬機:-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 的建議更新測驗。丹尼姆
@DataJpaTest
@Transactional
@TestPropertySource(properties = "spring.liquibase.change-log=classpath:db/changelog/changelog.master.xml")
class DivisionRepositoryTest {
@Autowired
private DivisionRepository repository;
uj5u.com熱心網友回復:
好的,我終于找到了解決方案,原因出在我什至沒有懷疑的地方,我的應用程式使用 Spring Native 來創建優化的生產構建,顯然它以某種方式干預了應用程式的開發構建程序。現在,我剛剛從我的應用程式中洗掉了它。稍后我可能會嘗試將開發 build.gradle 與生產版本分開。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311348.html
