Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Saving lazy-loaded entity with IdClass throws TypeMismatchException
package org.sapzil;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.ManyToOne;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.support.TransactionTemplate;
@Repository
interface CompanyRepository extends CrudRepository<JpaBugTest.Company, Integer> {}
@Repository
interface EmployeeRepository extends CrudRepository<JpaBugTest.Employee, JpaBugTest.EmployeeId> {}
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaBugTest {
@Autowired
TransactionTemplate transactionTemplate;
@Autowired
CompanyRepository companyRepository;
@Autowired
EmployeeRepository employeeRepository;
@Test
public void testBug() {
transactionTemplate.execute(status -> {
Company company = new Company();
company.setId(1);
companyRepository.save(company);
Employee employee = new Employee();
employee.setId(1);
employee.setCompany(company);
employeeRepository.save(employee);
return null;
});
transactionTemplate.execute(status -> {
Employee employee = employeeRepository.findById(new EmployeeId(1, 1)).get();
Company proxiedCompany = employee.getCompany();
System.out.println(proxiedCompany.getClass()); // class org.sapzil.JpaBugTest$Company_$$_jvst170_3
Employee employee2 = new Employee();
employee2.setId(2);
employee2.setCompany(proxiedCompany);
employeeRepository.save(employee2);
return null;
});
}
@SpringBootApplication
public static class Configuration {}
@Entity
public static class Company {
private Integer id;
public Company() {}
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
public static class EmployeeId implements Serializable {
private Integer company;
private Integer id;
public EmployeeId() {}
public EmployeeId(Integer company, Integer id) {
this.company = company;
this.id = id;
}
public Integer getCompany() {
return company;
}
public void setCompany(Integer company) {
this.company = company;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity
@IdClass(EmployeeId.class)
public static class Employee {
private Integer id;
private Company company;
public Employee() {}
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Id
@ManyToOne(fetch = FetchType.LAZY)
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.