-->
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.  [ 3 posts ] 
Author Message
 Post subject: One to Many relationship with secondary table
PostPosted: Wed Jul 28, 2010 9:07 am 
Newbie

Joined: Wed Jul 28, 2010 8:54 am
Posts: 3
Hi,

I'm sorry if my question is stupid, I am a bit new on this. I am searching for a solution on this for some time but I didn't find anything.

I have a database to save Articles that are tagged

I have the following tables (reduced to clarify the problem)

------
Article|
------
art_id |
title |
... |
------

----------
Tag |
----------
tag_name |
----------

----------------
Article_has_Tag |
----------------
art_id |
tag_name |
----------------

And I want to map this into 2 entities

Code:
@Entity
public class Tag implements Serializable{
   
   @Id
   private String name;

   //getters and setters..
}


Code:
@Entity
@SecondaryTables({
    @SecondaryTable(name="Article_has_Tag", pkJoinColumns={
             @PrimaryKeyJoinColumn(name="id", referencedColumnName="art_id")})
    })
public class Article implements IClusterable{
   
   @Id
   @GeneratedValue
   @Column(name = "art_id", nullable = false)
   private int id;   
   private String title;

   @OneToMany
        @JoinColumn(name="tag_name", referencedColumnName="tag_name",
                table="Article_has_Tag")
   private List<String> tags;

   //getters and setters..
}


but I am not able to map it... this is my last try... and I got
org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table

some help?

Thanks


Top
 Profile  
 
 Post subject: Re: One to Many relationship with secondary table
PostPosted: Wed Jul 28, 2010 9:53 am 
Newbie

Joined: Wed Jul 28, 2010 8:54 am
Posts: 3
I don't understand it completely but I found a solution that, at least, compiles

Code:
@Entity
public class Article implements IClusterable{
   
   @Id
   @GeneratedValue
   @Column(name = "art_id", nullable = false)
   private int id;   
   private String title;

   @ManyToMany(cascade=CascadeType.PERSIST)
        @JoinTable(name="article_has_tag",
           joinColumns=@JoinColumn(name="art_id", referencedColumnName="art_id"),
           inverseJoinColumns=@JoinColumn(name="tag_name", referencedColumnName="name"))
   private List<Tag> tags;

  //...
}


http://openjpa.apache.org/builds/1.0.2/ ... field.html


Top
 Profile  
 
 Post subject: Re: One to Many relationship with secondary table
PostPosted: Tue Dec 21, 2010 7:09 am 
Newbie

Joined: Tue Dec 21, 2010 7:03 am
Posts: 1
If you make it a bidirectional relation and use the mapedBy attribute, it may work.
It solved the problem for me


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.