Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 保存null时的多对多和完整性_Hibernate_Playframework - Fatal编程技术网

Hibernate 保存null时的多对多和完整性

Hibernate 保存null时的多对多和完整性,hibernate,playframework,Hibernate,Playframework,我有两个类软件,Tag,它们有很多联系。 我们不能在不加标签的情况下创建软件 我想写一个测试来检查: @试验 public void createSoftwareWithNullTags(){ 列表标签=null; 软件=新软件(“软件1”,“说明1”,标签); 试一试{ save(); 失败(“标记不应为空”); }捕获(例外情况除外){ //永远不要来这里!!! } } 所以,这个测试失败了。我想这不是一个很好的测试,因为它甚至不尝试将数据保存到软件标签表中。当然,我可以手动进行验证,

我有两个类软件,Tag,它们有很多联系。 我们不能在不加标签的情况下创建软件

我想写一个测试来检查:

@试验 public void createSoftwareWithNullTags(){

列表标签=null;
软件=新软件(“软件1”,“说明1”,标签);
试一试{
save();
失败(“标记不应为空”);
}捕获(例外情况除外){
//永远不要来这里!!!
}
}

所以,这个测试失败了。我想这不是一个很好的测试,因为它甚至不尝试将数据保存到软件标签表中。当然,我可以手动进行验证,但我想用hibernate实现它,使用一些注释或其他东西。可能吗?或者你会怎么做

我的实体:


@实体
公共类标记扩展模型{

public String title;

public Tag(String title) {
    this.title = title;
}

@ManyToMany(
    cascade = {CascadeType.ALL},
    mappedBy = "tags",
    targetEntity = Software.class
)
public List<Software> softwares;
public String title;
public String description;

@ManyToOne(optional = false)
public Author author;


@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(

        name = "SOFTWARE_TAG",
        joinColumns = @JoinColumn(name = "Software_id"),
        inverseJoinColumns = @JoinColumn(name = "Tag_id")

)
public List<Tag> tags;

public Software(String title, String description, Author author, List<Tag> tags) {
    this.title = title;
    this.description = description;
    this.author = author;
    this.tags = tags;
}
公共字符串标题;
公共标记(字符串标题){
this.title=标题;
}
@许多(
cascade={CascadeType.ALL},
mappedBy=“标签”,
targetEntity=Software.class
)
公开列表软件;
}

@实体 公共类软件扩展模型{

public String title;

public Tag(String title) {
    this.title = title;
}

@ManyToMany(
    cascade = {CascadeType.ALL},
    mappedBy = "tags",
    targetEntity = Software.class
)
public List<Software> softwares;
public String title;
public String description;

@ManyToOne(optional = false)
public Author author;


@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(

        name = "SOFTWARE_TAG",
        joinColumns = @JoinColumn(name = "Software_id"),
        inverseJoinColumns = @JoinColumn(name = "Tag_id")

)
public List<Tag> tags;

public Software(String title, String description, Author author, List<Tag> tags) {
    this.title = title;
    this.description = description;
    this.author = author;
    this.tags = tags;
}
公共字符串标题;
公共字符串描述;
@多通(可选=假)
公共作者;
@多个(级联=级联类型.ALL)
@可接合(
name=“软件标签”,
joinColumns=@JoinColumn(name=“Software\u id”),
inverseJoinColumns=@JoinColumn(name=“Tag\u id”)
)
公共列表标签;
公共软件(字符串标题、字符串描述、作者、列表标记){
this.title=标题;
this.description=描述;
this.author=作者;
this.tags=标签;
}
}

您不能使用“普通”Hibernate。您需要使用HibernateValidator,它是“BeanValidation”JSR的一个实现。在这种特定情况下,您将使用
@Size
注释,其
min
属性为1


<>编辑:我真的认为如果你不期望一个参数为空,你应该考虑抛出一个Null PoExtExchange。在我看来,您应该只使用Bean验证来防止“用户”错误。为了防止编程错误(如不可接受的空值),您应该坚持使用异常:-)

M:N在Hibernate中有点“棘手”,不适合您的场景。我建议:

  • 将M:N关系转换为两个rels 1:M.Software-middle-class-Tag。这样,您可以在软件端添加@Required,并使用更简单的1:M rels
  • 为此添加一些用户验证

如果保持当前的M:N,请记住更改级联属性。我想说的是,如果您现在删除一个软件实体,您将删除它的所有相关标记(其他软件实体可能会使用这些标记)并造成混乱。

我不想使用这样的附加框架,因为play framework使用嵌入式hibernate,然后我应该考虑应该使用哪个版本的验证框架。但也许这不是问题。。。我不知道我的箱子应该把
@Size
放在哪里。