many to many and integrity when saving the null

Posted by user369759 on Stack Overflow See other posts from Stack Overflow or by user369759
Published on 2011-03-16T12:59:34Z Indexed on 2011/03/16 16:09 UTC
Read the original article Hit count: 223

Filed under:
|

I have two classes Software, Tag - they are related by ManyToMany. We CAN NOT create Software without putting the Tag to it.

I want to write a test which check that:

@Test public void createSoftwareWithNullTags() {

    List<Tag> tags = null;
    Software software = new Software("software1", "description1", tags);
    try {
        software.save();
        fail("tags should not be null");
    } catch (Exception ex) {
         // NEVER COME HERE  !!!
    }

}

So, this test fails. I guess it is not good test for that case - because it even do not try to SAVE data to SOFTWARE_TAG table. Of course i could do validation manually but i want implement it with hibernate, using some annotation or something. Is it possible? Or how would you do this?

My entities:

@Entity public class Tag extends Model {

public String title;

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

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

}

@Entity public class Software extends Model {

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;
}

}

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about playframework