Home > Software design >  Hibernate does not create proxy within @DataJpaTest
Hibernate does not create proxy within @DataJpaTest

Time:10-12

I have a very simple test that tests the spring data repository that works ok in normal runtime. I really don't think it should be this difficult to do this, but I do not understand what I am doing wrong, please help.

When I try to test this repository I start receiving errors that say something like:

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)

It appears that hibernate can not create a proxy for entity classes, because it for some reason has been assigned a DisallowedProxyFactory implementation for proxy factory. So I added this configs:

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

But now I simply receive this error instead:

Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:83)

So I added @EnableLoadTimeWeaving to the test class, and now I receive this error

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

The initial test set up:

@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       
    }
}

Division entity:

@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;
    }

}

Repository:

public interface DivisionRepository extends JpaRepository<Division, Integer> {

    Page<Division> findAll(Pageable pageable);

}

Rds Persistence config class

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

Updated test after suggestion from @M. Denium

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

    @Autowired
    private DivisionRepository repository;

CodePudding user response:

Ok, I finally found the solution, the reason was in something I did not even suspect, my application uses spring native to create optimized production builds, apparently it has somehow intervened with the development build process of the application. For now, I have just removed it from my application. Later on I will probably try to separate the development build.gradle from the production one.

  • Related