Hello everyone,
I'm having some trouble annotating entities for a certain type of datamodel. The datamodel looks as following:
Customer has a composite PK based on an FK with Country and a customer_id.
Department has a composite PK based on an FK with Country and a project_id.
Project has an FK relationship with Country, Department and Customer.
Now when @ManyToOne relationships are mapped, we'll get three of them in the Project entity, for Country, Customer and Department. However, all three of these are based on the underlying country_id field.
The Project entity will look a bit like this:
Code:
@Entity
@Table(name = "Project")
public class Project implements Serializable {
private static final long serialVersionUID = 646965562304786L;
@Id
@Column(name = "Project_id", nullable = false)
String projectId;
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
@Column(name = "Description", nullable = true)
String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
})
private Country country;
public Country getCountry() { return country; }
public void setCountry(Country country) { this.country = country; }
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
@JoinColumn(name = "Customer_id", referencedColumnName = "Customer_id"),
})
private Customer customer;
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "Country_id", referencedColumnName = "Country_id"),
@JoinColumn(name = "Department_id", referencedColumnName = "Department_id"),
})
private Department department;
public Department getDepartment(){ return department; }
public void setDepartment(Department department) { this.department = department; }
}
Hibernate doesn't like this, because the column Country_id will be mapped three times in the Project entity:
org.hibernate.MappingException:
Repeated column in mapping for entity: hibernateentities.entities.Project column: Country_id (should be mapped with insert="false" update="false")I changed the @JoinColumns to the following in order to prevent the repeated mappings.
Code:
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "Country_id", referencedColumnName = "Country_id", insertable = false, updatable = false),
@JoinColumn(name = "Department_id", referencedColumnName = "Department_id"),
})
My theory was that I'd set the Country entity in the setter method based on the provided Department. However, unfortunately it gave another exception:
org.hibernate.AnnotationException:
: Mixing insertable and non insertable columns in a property is not allowed: hibernateentities.entities.ProjectdepartmentHow can I map this properly? Thank you for your time.
Edit: Found a topic with a simlar problem, but based around XML declaration instead of JPA annotations.