|
Re: Transform foreign keys in One-To-One Relationship [message #557871 is a reply to message #557784] |
Thu, 09 September 2010 10:01 |
Mark Hoffmann Messages: 113 Registered: July 2009 Location: Jena |
Senior Member |
|
|
Hi,
I am a step further. It is now possible to map the entity B using TRansformationMapping:
tmapping.setAttributeTransformer(new AttributeTransformer() {
@Override
public void initialize(AbstractTransformationMapping map) {
}
@Override
public Object buildAttributeValue(Record record, Object object, Session session) {
UnitOfWork uow = session.acquireUnitOfWork();
B b = new B();
b.setId(Long.valueOf((long)3));
B bc = (B)uow.readObject(b);
A a = (A)object;
a.setB(bc);
return pc;
}
});
descriptor.addMapping(tmapping);
But here are two problems:
1. A is cached with the B instance, that is stored in the object parameter given by the buildAttributeValue method and not "bc" in my case. If I access A using em.find(A.class, 1), everything is fine. When I call em.find(A.class, 1) again my "bc" instance isn't available in A anymore.
2. This is just reading. What about insert, update and deletes? Do I have to use the FieldTransformer? I tried it but got the message:
ultiple writable mappings exist for the field [a.id_b]. Only one may be defined as writable, all others must be specified read-only.
Can anybody help about this?
Regards,
Mark
|
|
|
Re: Transform foreign keys in One-To-One Relationship [message #558625 is a reply to message #557784] |
Mon, 13 September 2010 17:58 |
|
This does not make much sense.
A has a generated IDENTITY Id and a foreign key to B, and you want B's Id to be A.id+100??? This means A can't possible have the foreign key to B until after it is inserted.
Not sure what you are trying to accomplish, perhaps just use a generated Id for B. If you want B to share A's Id then A should not have a b_id, just a OneToOne using a mappedBy and B with have its Id mapped through a OneToOne back to A.
If you really want to have B's id A.id+100, then use a prePersist/preInsert event in B to set its Id based on A. You will need to ensure that A already has an Id though, so IDENTITY is not a good idea, perhaps TABLE. Using a transformation mapping does not make any sense.
James : Wiki : Book : Blog : Twitter
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03473 seconds