Search code examples
javasqljpaopenjpa

JPA ManyToMany issue


I have two tables, Users and Products. And im trying to automatically create a new Table TestedProducts, which is a many to many relationship between User and Product. However, i get this error.

org.apache.jasper.JasperException: javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
<openjpa-1.2.1-r752877:753278 fatal user error> org.apache.openjpa.persistence.ArgumentException: 
"ejb.UserEntity.products<element:interface ejb.Product>" has columns with targets, but OpenJPA does not support any joins on this mapping in this context.

The UserEntity looks lite this.

long id;

String firstname;
String lastname;
String password;
String email;
String description;

List<Product> products;

public void setId(long id){
    this.id = id;
}

@Id
@GeneratedValue
public long getId() {
    return id;
}

@ManyToMany
@JoinTable(name="TestedProducts", 
joinColumns=
    @JoinColumn(name="usrId", referencedColumnName="id"),
  inverseJoinColumns=
  @JoinColumn(name="prodId", referencedColumnName="id")
  )
  public List<Product> getProducts() {
    return products;
  }

  public void setProducts(List<Product> products) {
    this.products = products;
  }

And ProductEntity like this:

long id;

String name;
String description;
String type;

List<User> users;

@ManyToMany(mappedBy="products") 
protected List<User> getUsers() {
    return users;
}

protected void setUsers(List<User> users) {
this.users = users;
}

I skipped the rest of the setters and getters.


Solution

  • You need to annotate the list containing users in the product entity the same way you did in the user entity, only inverted. I don't think the mappedby value is handled the same.