Hi guys,
I have a problem with MappedSuperClass. My structure is similar to this:
This is the Super Class of all VOs.
Code:
@MappedSuperclass
public abstract class AbstractContent implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
protected Integer id;
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(name = "content_comments",
joinColumns = {
@JoinColumn( name="content_id", referencedColumnName = "id"),
@JoinColumn( name="comment_id", referencedColumnName = "id") },
inverseJoinColumns = @JoinColumn( name="comment_id")
)
protected List<CommentVo> comments = new ArrayList<CommentVo>();
These are the VOs.
Code:
@Entity
public class NewsVo extends AbstractContent
...
@Entity
public class CommentVo extends AbstractContent
...
@Entity
public class ArticleVo extends AbstractContent
...
And that is the problem:
Code:
NewsVo news = new NewsVo();
news.setTitle("title");
news.setShortText("shortText");
news.setAdditionalText("additionalText");
news.addComment(new CommentVo("Comment 1", "text 1"));
news.addComment(new CommentVo("Comment 2", "text 2"));
newsDao.create(news);
And that is the error:
Code:
JDBCExceptionReporter:logExceptions | Cannot add or update a child row: a foreign key constraint fails (`jnuke/content_comments`, CONSTRAINT `FKE856D2FAA2B9AD07` FOREIGN KEY (`content_id`) REFERENCES `[b]articles[/b]` (`id`))
Note that Hibernate3 is generating the table with a FK pointing to the table ARTICLES. But this association should be made with all sons of AbstractContent, not only with Article.
Code:
CREATE TABLE `jnuke`.`content_comments` (
`id` INT DEFAULT '' NOT NULL,
`comment_id` INT DEFAULT '' NOT NULL
) ENGINE=InnoDB;
...
ALTER TABLE `jnuke`.`content_comments` ADD CONSTRAINT `FKE856D2FA92F4FB81` FOREIGN KEY (`id`)
REFERENCES `jnuke`.`articles` (`id`);
So, when I try to store a News with comments an error occours, because the relationship in the database is between Articles and comments.
If I remove the ArticleVo from teh soruce code, the hibernate will use another aleatory table in the relationship (FK).
Any ideas?