-->
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.  [ 1 post ] 
Author Message
 Post subject: FK part of a composite PK
PostPosted: Sun Oct 07, 2007 5:22 am 
Newbie

Joined: Sun Sep 30, 2007 5:04 am
Posts: 2
Hello guys,

Hibernate annotations 3.3.0

I'm trying to implements the following scenario:

I got the following classes - Test, TestDetails and Language. Test contains an auto generated id, TestDetails contains language dependent details on Test and has a composite PK - id and langId. Id is a reference to Test and langId should be a FK to a Language.
Bellow, you may find my code:

PK class
Code:
@Embeddable
public class IdLangIdPK implements Serializable {
  private Long id;
 
  @Column(name = "lang_id")
  private Long langId;
   
  @OneToOne(cascade = CascadeType.ALL, optional = false)
  @JoinColumn(name = "lang_id" insertable = false, updatable = false)
  private Language lang;

  ...
}


Test.java
Code:
@Entity
@Table(name = "test")
public class Test implements Serializable {
  @Id @GeneratedValue
  private Long id;

  @OneToMany
  private List <TestInfo> test_info;
 
  ...
}


TestInfo.java
Code:
@Entity
@Table(name = "test_info")
@IdClass(IdLangIdPK.class)
public class TestInfo implements Serializable {

  @Id
  private Long id;
 
  @Id @Column(name = "lang_id")
  private Long langId;
 
  @Column(name = "name")
  private String name;
 
  @ManyToOne
  @JoinColumn(name = "id")
  private Test test;

  ...
}


and finally
Language.java
Code:
@Entity
@Table(name = "language")
public class Language implements Serializable {
  @Id @GeneratedValue
  private Long id;
 
  @Column(nullable = false, length = 30)
  private String name;

  ..



The question - is it possible to have FKs as a part of a composite PK? Playing around with it, I get different error messages but cant bring it to generate the correct SQL, which logically could look like:

Code:
CREATE TABLE test_info (
  id bigint references test(id),
  lang_id bigint reference language(id),
  name varchar(255),
  primary key(id, lang_id)
);


Thank you.

--


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

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.