tags:

views:

1233

answers:

1

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

+1  A: 

First of all, are you sure you need to? ...

Anyway. I may show my ignorance here but I think you could be quite close, but the order of things are not.

Reading the generated key can be done like this:

...
First first = new First();
entityManager.save(first);  //<- the id is updated

Second second = new Second();
second.setB(300);     
second.setA(first.getA()); 

first.setSeconds( new ArrayList());
first.getSeconds().add(second);
entityManager.save(second);

entityManager.getTransaction().commit();

Have you read this?

Java Persistence : Mapping A Joing Table With Additional Columns

Its not JPA but if your are lucky hibernate may support having both an @Id and @ManyToOne tag set at the same field. Have you tried that? That would be nice wouldn't it :-) ...

 @Id  // may not work
 @ManyToOne // may not work
 @JoinColumn(name = "a")
 First a;

Oh and what's tha "a" prefix all about? Argument? Ambiguous ? Ambivalent? An-Enterprisy-Thing?

KarlP
Thanx for you, I tried the @id on @manytoone, but it seems not working.
Moro