I have following entity classes in Spring Boot application using Spring Data JPA
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "branch_type", discriminatorType = DiscriminatorType.STRING)
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, name = "branch_id")
private String branchId;
}
@Entity
@DiscriminatorValue("LOCAL")
public class LocalBranch extends Branch {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "division_head_id", referencedColumnName = "branch_id")
private DivisionHead divisionHead;
// other fields and mappings
// getters and setters
}
@Entity
@DiscriminatorValue("DIVISION")
public class DivisionHead extends Branch {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "region_head_id", referencedColumnName = "branch_id")
private RegionHead regionHead;
@OneToMany(mappedBy = "divisionHead", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<LocalBranch> localBranches = new HashSet<>();
// other fields and mappings
// getters and setters
}
@Entity
@DiscriminatorValue("REGION")
public class RegionHead extends Branch {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "circle_head_id", referencedColumnName = "branch_id")
private CircleHead circleHead;
@OneToMany(mappedBy = "regionHead", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<DivisionHead> divisions = new HashSet<>();
// other fields and mappings
// getters and setters
}
@Entity
@DiscriminatorValue("CIRCLE")
public class CircleHead extends Branch {
@OneToMany(mappedBy = "circleHead", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<RegionHead> regions = new HashSet<>();
// other fields and mappings
// getters and setters
}
If I start the application, I am getting A '@JoinColumn' references a column named 'branch_id' but the target entity divisionHead.RegionHead has no property which maps to this column.
I don't understand why Hibernate/Jpa can't see branch_id
column in entity classes even though they are extending from Branch class which has unique column branch_id
.
If I don't provide any referencedColumnName then it's working fine. But, I need foreign key reference to branch_id
column. I try changing the referencedColumnName = 'branchId' (field name), but it didn't work as well (as expected).
How to get out of this error. Is ther any issues with associations of my entity classes. As They are self referncing associations, I doubt something is not right?
Just especially for you, I opened an issue:
https://hibernate.atlassian.net/browse/HHH-16501
and fixed it. You'll need to wait for Hibernate 6.3.