Emilla
Emilla

Reputation: 494

I can not not create JPA relation between class

Hi I am pretty new about play framework and JPA. I am creating web application and I need to have 2 classes in model side of application. When I run app I get this error: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements:

package models;
import java.util.ArrayList;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import play.db.jpa.Model;

   @Entity

public class SubCategory extends Model{
@Column(unique = true)
public String subCatName;

@ManyToOne
public Category category;

public SubCategory(String subCatName,Category category){
    this.subCatName=subCatName;
    this.category=category;
}
}

   package models;

 import java.util.ArrayList;

 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.OneToMany;
 import org.hibernate.engine.Cascade;
 import play.db.jpa.Model;

 @Entity

  public class Category extends Model{

@Column(unique = true)
public String catName;

@OneToMany(mappedBy="category", cascade=CascadeType.ALL)
public ArrayList<SubCategory> allSubCat;

public Category(String catName){
    this.catName=catName;
    allSubCat=new ArrayList<SubCategory>();
}
}

Upvotes: 0

Views: 120

Answers (1)

Seb Cesbron
Seb Cesbron

Reputation: 3833

ArrayList is an implemantion of the interface List.

You have to defined your field as type List because under the hood hibernate (which is the orm used by play to connect to the db) don't use the ArrayList implementation but its own custom implementation

Upvotes: 1

Related Questions