I'm building a server that uses hibernate for it's data layer and I hava a client that creates entities through a remote interface.
Let's say that the client wants to create a new entity A which have many-to-one references to the already persisted entities B, C and D.
The entities B, C and D have primary keys generated by the server (hibernate) and they also have candidate keys. The clien't doesn't know the primary keys of the referenced objects but it knows the candidate keys. I would like the client to be able to be able to call the server like this:
Code:
b = new B();
b.setCandidateKey(XX);
c = new C();
b.setCandidateKey(YY);
d = new D();
b.setCandidateKey(ZZ);
a = new A();
a.setB(b);
a.setC(c);
a.setD(d);
server.create(a);
The point is that I don't want ther client to have to lookup all referenced objects to find their primary-keys. Now if I just try to save the value object 'a' on the server side (using session.merge()) I get an error because the referenced value objects doesn't contain primary keys and are treated as transient objects.
Do I have to make code that looks all referenced objects up and retrieves their primary keys or is there any way to make hibernate do this automatically (the candidate keys are unique, not-null and immutable)?