nab*_*nas 2 java playframework ebean playframework-2.0 playframework-2.1
美好的一天!我总是得到一个错误,以保存我的实体模型
Error inserting bean [class models.CategoryEntity] with unidirectional relationship. For inserts you must use cascade save on the master bean [class models.CategoryEntity].]
Run Code Online (Sandbox Code Playgroud)
在这里我的课
@Entity
public class CategoryEntity extends Model {
@Id
private String categoryId;
private String Name;
private Integer level;
@OneToMany(targetEntity = CategoryEntity.class, cascade = CascadeType.ALL)
private List<CategoryEntity> categories;
//GETERS SETRES
}
Run Code Online (Sandbox Code Playgroud)
我试图保存标题类别,但错误是相同的
如果我正确理解了这个问题,你想要的是每个CategoryEntity包含其他CategoryEntities的列表,我会想到两种可能的方法(尽管它们都没有使用@OneToMany):
方法1:
您可以创建@ManyToMany关系并定义@JoinTable,同时命名其键:
@Entity
public class CategoryEntity extends Model {
@Id
private String categoryId;
private String name;
private Integer level;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable( name = "category_category",
joinColumns = @JoinColumn(name = "source_category_id"),
inverseJoinColumns = @JoinColumn(name = "target_category_id"))
public List<CategoryEntity> category_entity_lists = new ArrayList<CategoryEntity>();
}
Run Code Online (Sandbox Code Playgroud)
方法2:
或者您可以为类别实体列表创建新实体并创建@ManyToMany关系,例如:
@Entity
public class CategoryList extends Model
{
@Id
public Long id;
@ManyToMany
@JoinTable(name="categorylist_category")
public List<CategoryEntity> category_list = new ArrayList<CategoryEntity>();
}
Run Code Online (Sandbox Code Playgroud)
然后在你的模型中:
@Entity
public class CategoryEntity extends Model {
@Id
private String categoryId;
private String name;
private Integer level;
@OneToOne
public CategoryList this_category_list;
@ManyToMany(mappedBy="category_list")
public List<CategoryList> in_other_category_lists = new ArrayList<CategoryList>();
}
Run Code Online (Sandbox Code Playgroud)
没有测试代码,但是应该做的是每个CategoryEntity都可以是几个CategoryLists的一部分.每个CategoryList都包含CategoryEntities列表.
您必须初始化this_category_list并将CategoryEntities添加到其category_list字段.