1

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?

1
  • Something looks odd about your database. The exception indicates that the LOG_TAG table has a foreign key (PEOPLE_ID) to the TAG.ID. Either you need to drop and recreate you tables, or there is a problem with you mappings. Is the @Id column of Model named "PEOPLE_ID"? Commented Nov 16, 2011 at 21:30

2 Answers 2

2

Are you using H2 as your database, with db=fs so it saves to the file system? You might try deleting your db directory so it gets recreated. The schema looks like it is out of sync with the model, since your error message references PEOPLE_ID and you have no "people" field in your example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I feel a little silly not realizing the exception was referencing people. It was late at night :)
1

to see what is going wrong you could turn on the ouput of sql querys of hibernate and then see if you actually save the tags before save the log

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.