-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problem storing children of a MappedSuperClass
PostPosted: Fri Jan 12, 2007 1:46 pm 
Newbie

Joined: Fri Jan 12, 2007 1:21 pm
Posts: 2
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?

_________________
Thanks,
Franklin Samir


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 6:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
interesting, what happens if you get rid of the FK constraints (manually) and execute your code?
Is it getting allright?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 21, 2007 6:55 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ah forget what I've said, what you need to do is map AbstractContent as @Entity @Inheritence(strategy=TABLE_PER_CLASS) and leave it abstract (as it is)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 5:14 pm 
Newbie

Joined: Fri Jan 12, 2007 1:21 pm
Posts: 2
Hi Emmanuel,

Using TABLE_PER_CLASS everything works fine, but all of my entities will be merged into a single table. And, I can't do that because I have a lot of specializations of AbstractContent. It would be a huge and confuse table.

I would like to use a solution where the discriminator was stored in the intermediate table. For exempla:

Code:
Table content_1 (content_id, filed_1, ...);

Table content_2 (content_id, filed_x, ...);

Table Comment(comment_id, fileds...)

table content_comments(content_id, comment_id, discriminator)


Is that possible?

_________________
Thanks,
Franklin Samir


Top
 Profile  
 
 Post subject: can't do this
PostPosted: Tue Jan 30, 2007 12:30 pm 
Newbie

Joined: Mon Nov 03, 2003 1:07 am
Posts: 14
I just upgraded to the latest hibernate annotations/hibernate core and was doing what you were doing.... While it worked in earlier versions, currently it does not work.

In my research, you can only do mapping of associations to Entities (@Entity) Only simple properties can be used in a @MappedSuperClass, not associations.

I found the MappedSuperClass to be of limited use with the inablility to map any associations in it.

Phillip


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I would say you're wrong, but if you can create a minimal running test case involving 2 entities, post it to JIRA

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.