-->
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: Problem with @SecondaryTable
PostPosted: Wed Sep 29, 2010 11:17 pm 
Newbie

Joined: Wed Sep 29, 2010 9:29 pm
Posts: 2
This is my scenario.

Table: forum_topics
id : int (pk)
board_id : int
firstMessage_id : int

Table: forum_messages
id : int (pk)
subject : varchar(100)

I have a Topic Entity with forum_topics as base table, I want to include the topic "subject" from forum_messages table by a join on column forum_topics.firstMessage_id -> forum_messages.id

Code:
@Entity
@Table(name = "forum_topics")
@SecondaryTables
(
   {
      @SecondaryTable(name="forum_messages", pkJoinColumns={@PrimaryKeyJoinColumn(name="id", referencedColumnName="firstMessage_id")})
   }
)

public class Topic extends Domain
{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "id", updatable = false, nullable = false)
   Integer id;

   @Column(name = "board_id")
   Integer board_id;
   
   @Column(name = "firstMessage_id")
   Integer firstMessage_id;

   @Column(name = "subject", table="forum_messages")
   String title;


When I run the application (WebApp) I got the following message:

Code:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException: SecondaryTable JoinColumn cannot reference a non primary key


If I swap the columns on the pkJoinColumns like:

Code:
@SecondaryTable(name="forum_messages", pkJoinColumns={@PrimaryKeyJoinColumn(name="firstMessage_id", referencedColumnName="id")})


I can run the application, but when I try to resolve Topic objects from a board_id I get the following error:

could not execute query; SQL [select topic0_.id as id1_, topic0_.board_id as board2_1_, topic0_.firstMessage_id as firstMes3_1_, topic0_1_.subject as subject2_ from forum_topics topic0_ left outer join forum_messages topic0_1_ on topic0_.id=topic0_1_.firstMessage_id where topic0_.board_id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

It looks to me like the validation of the columns involved on the Join is done in one direction, and the execution is done on reverse.

I am doing something wrong or missing something ? I am using hibernate3.

Appreciate any help you could provide to me.

Thanks.


Last edited by fdeutsch on Wed Sep 29, 2010 11:23 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Problem with @SecondaryTable
PostPosted: Wed Sep 29, 2010 11:20 pm 
Newbie

Joined: Wed Sep 29, 2010 9:29 pm
Posts: 2
This is the kind of query I am looking for

select A.id, A.board_id, A.firstMessage_id, B.subject from forum_topics A, forum_messages B where A.firstMessage_id = B.id and A.board_id = ? (of course with a left outer join)


Top
 Profile  
 
 Post subject: Re: Problem with @SecondaryTable
PostPosted: Wed Oct 09, 2013 1:12 pm 
Newbie

Joined: Wed Oct 09, 2013 1:08 pm
Posts: 1
I have the same problem, you are found a solution for this issue ?! please help 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.