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.javaCode:
@Entity
@Table(name = "test")
public class Test implements Serializable {
@Id @GeneratedValue
private Long id;
@OneToMany
private List <TestInfo> test_info;
...
}
TestInfo.javaCode:
@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.javaCode:
@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.
--