Reputation: 59
I have a large DB on MySql Workbench and I'm trying to map the relationship between the entities on Eclipse Mars thanks to Hibernate and the JPA module. The fact is that I receive the error: "In attribute 'personAddresses', the "mapped by" attribute 'peopleAdd' has an invalid mapping type for this relationship." This are the entities involved.
I've to say that making a forward engineering, Hibernate creating for me an AddressId class, where the composite primary key of Address is mapped. I suspect that the problem could be this, but I'm not certain, can you help me please? Under I post the code so that it's more clear to understand how the classes are implemented.
@Entity
@IdClass(AddressId.class)
@Table(schema = "YouDroop", name = "Address")
public class Address implements Serializable
{
...
private Collection<Person> peopleAdd = new HashSet<Person>();
@Id
@Column(name = "Address", length = 45, unique = true, nullable = false)
private String address;
@Id
@Column(name = "Number", unique = true, nullable = false)
private int number;
...
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(
name = "PersonHasAddress",
joinColumns = {
@JoinColumn(name = "Address_Address", referencedColumnName = "Address", nullable = false),
@JoinColumn(name = "Address_Number", referencedColumnName = "Number", nullable = false)
},
inverseJoinColumns = {@JoinColumn(name = "Person_Email", referencedColumnName = "Email", nullable = false)}
)
public Collection<Person> getPeopleAddressed(){
return this.peopleAdd;
}
public void setPeopleAddressed(Collection<Person> people){
this.peopleAdd = people;
}
}
public class AddressId implements Serializable
{
private String address;
private int number;
public AddressId(){}
public AddressId(String address, int number) {
super();
this.address = address;
this.number = number;
}
...
}
@Entity
@Table(name = "Person", schema = "YouDroop", uniqueConstraints =
{ @UniqueConstraint(columnNames = "NickName"),
@UniqueConstraint(columnNames = "Password") })
public class Person implements Serializable
{
...
private Collection<Address> addresses = new HashSet<Address>();
...
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "peopleAdd")
public Collection<Address> getPersonAddresses(){
return this.addresses;
}
public void setPersonAddresses(Collection<Address> addresses){
this.addresses = addresses;
}
}
Upvotes: 1
Views: 3273
Reputation: 4154
Since you placed you @ManyToMany annotation on your getter method (or property) and not on the field. The mappedBy attribute should reference the property instead and not the field.
@ManyToMany
public Collection<Person> getPeopleAddressed() {
...
}
So your mappedBy attribute should have been
@ManyToMany(mappedBy="peopleAddressed")
public Collection<Address> getPersonAddresses() {
...
}
Upvotes: 2