Is it possible in plain JPA or JPA+Hibernate extensions to use a foreign key that is also part of the composite primary key?
@TableGenerator(name = "trial", table = "third",
pkColumnName = "a" , valueColumnName = "b", pkColumnValue = "first")
@Entity
public class First{
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "trial")
protected int a;
@OneToMany(mappedBy ="first", cascade = CascadeType.PERSIST)
@JoinColumn(name = "a")
protected List<Second> seconds;
}
class IDC implements Serializable{
//@Column(name = "a", insertable = false, updatable = false)
protected int a;
protected int b;
}
@Entity
@IdClass(IDC.class)
public class Second{
@Id
//@Column(name = "a", insertable = false, updatable = false)
protected int a;
@Id
protected int b;
@ManyToOne
@JoinColumn(name = "a"/*, insertable = false, updatable = false*/)
First first;
}
main:
public class Persister {
public static void main(String[] args) {
First aFirst = new First();
Second aSecond = new Second();
aSecond.b = 300;
List<Second> scnds = new ArrayList<Second>();
scnds.add(aSecond);
aFirst.seconds = scnds;
aSecond.first = aFirst;
aEntityManager.getTransaction().begin();
aEntityManager.persist(aFirst);
aEntityManager.getTransaction().commit();
}}
The problem is in class "Second":
If I set "insertable = false, updatable = false" at field "a", it throws exception:
"Parameter index out of bounds. 4 is not between valid values of 1 and 3"
And if I set "insertable = false, updatable = false" at the relation of @manyToOne, it run but set 0 in "a" at table "Second"
The Goal: Set the generated value of First.a in Second.a.
/////////////////////////////
the sql to create the DB:
create table first( a int, primary key (a) );
create table second( a int, b int, primary key (a, b) );
create table third( a varchar(20), b int );
please ... help
Thanx in advance