I'm getting the following exception:
Referential integrity constraint violation: "FK779B6FDFD4D56C1: PUBLIC.LOG_TAG FOREIGN KEY(PEOPLE_ID) REFERENCES PUBLIC.TAG(ID)";
Here's what I'm trying to do:
Set<String> tagList = getTags();
Log log = new Log(content, user);
log.addTags(tagList);
log.save();
I think understand the error (trying to save an object with a reference to an object that hasn't been saved yet), but I've tried every combination of order of saving each object, and nothing seems to be working. I'm looking at the Play Framework tutorial for creating a blog as a reference. Here's my model class:
@Entity
public class Log extends Model {
@Lob
public String content;
@ManyToOne
public User author;
@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> tags;
public Log(String content, User author) {
this.author = author;
this.content = content;
this.tags = new TreeSet<Tag>();
}
public void addTags(Set<String> tags) {
for (String tag : tags) {
Tag newTag = Tag.findOrCreateByName(tag); //since the DB is empty, this method is simply creating and .save()'ing tags
this.tags.add(newTag);
}
}
}
Right now the Tag class is a simple entity that only has one field.
What am I doing wrong here? How do I make this work?