Reputation: 1372
I am trying to make a OneToMany relationship between User and Project but I am getting following error:
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ProjectDTO.FKUser in UserDTO.projectDTOs
My User class look like below
@Entity
@Table(name = "USER")
public class UserDTO implements java.io.Serializable {
private Integer iduser;
private Set<ProjectDTO> projects = new HashSet<ProjectDTO>(0);;
public UserDTO() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "IDUser", unique = true, nullable = false)
public Integer getIduser() {
return this.iduser;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "FKUser")
public Set<ProjectDTO> getProjectDTOs() {
return projects;
}
}
And my Project class looks like
@Entity
@Table(name = "Project")
public class ProjectDTO implements java.io.Serializable {
private Integer idproject;
private UserDTO FKUser;
public ProjectDTO() {
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "IDProject", unique = true, nullable = false)
public Integer getIdproject() {
return this.idproject;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FKUser", referencedColumnName = "IDUser")
public UserDTO getUserDTO() {
return FKUser;
}
}
I went through some similar questions but I was unable to figure out where I was going wrong. Maybe I am missing something very obvious.
Thanks a lot
Upvotes: 0
Views: 3108
Reputation: 3951
In mappedBy
you must use field name (or part of the getter name without get
for property-based access), not column name.
//...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userDTO")
public Set<ProjectDTO> getProjectDTOs() {
return projects;
}
//...
Note: also need to add setters.
Upvotes: 4
Reputation: 1820
mappedBy needs to point to the entity that does the mapping, so I believe it should be mappedBy="USER"
Upvotes: 1