Reputation: 1952
I have the entity User
, this entity is used twice in another entity Product
. I use hibernate and foreign keys between tables are created. There are also two foreign keys - for create_by and modified_by.
class User
class User {
private Set<Product> products = new HashSet<Product>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
public Set<ProductgetProducts{
return this.products
}
public void setProducts(Set<Product> products) {
this.products = products
}
}
class Product
class Product {
private User createdBy;
private User modifiedBy;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "modified_by", nullable = false)
public User getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(User createdBy) {
this.modifiedBy= modifiedBy;
}
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "created_by", nullable = false)
public User getCreatedBy() {
return createdBy;
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}
}
It throws me this exception and I don't know how to fix it.
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: Product.user in User.products
Upvotes: 0
Views: 194
Reputation: 691943
mappedBy = "user"
means: I'm the inverse side of a bidirectional association, whose owner side is the field user
in the target entity (i.e. Product). There is no user
field in Product. So it doesn't make sense.
If you want the createdBy
ad modifiedBy
associations to be bidirectional, you need two fields in User:
@OneToMany(mappedBy = "createdBy")
private Set<Product> createdProducts;
@OneToMany(mappedBy = "modifiedBy")
private Set<Product> modifiedProducts;
Upvotes: 2