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.