Transform foreign keys in One-To-One Relationship [message #557784] |
Wed, 08 September 2010 17:52  |
Eclipse User |
|
|
|
Hi,
I have a One-To-One relationship between two entities A and B:
<entity class="test.A">
<customizer class="test.customizer.TestCustomizer"/>
<attributes>
<id name="id">
<generated-value strategy="IDENTITY"/>
</id>
<basic ...
<one-to-one name="B">
<join-column name="id_b"/>
</one-to-one>
</attributes>
</entity>
<entity class="test.B">
<table name="person"/>
<attributes>
<id name="id">
</id>
<basic ...
</attributes>
</entity>
</entity-mappings>
The Id of B should be calculated in Java (eg. B.id = A.id_b + 100). Is the customizer the best way? I tried the TransformationMapping in the customizer like this:
TransformationMapping mapping = new TransformationMapping(); mapping.addFieldTransformer("test.id_b", new FieldTransformer() { ...
descriptor.addMapping(mapping);
I didnt succeed with this. What is the right pattern here?
Please, can anybody point me to the right direction?
Regards,
Mark
|
|
|
Re: Transform foreign keys in One-To-One Relationship [message #557871 is a reply to message #557784] |
Thu, 09 September 2010 06:01   |
Eclipse User |
|
|
|
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 #558770 is a reply to message #558697] |
Tue, 14 September 2010 07:37  |
Eclipse User |
|
|
|
I'd suggest you maintain this kind of relation in code. You cannot put an actual foreign on it, so a simple getter in each entity that does a B.findByPK(pkA + 100) and A.findByPK(pkB - 100) should go a long way. IMHO.
Tom
On 14-9-2010 7:43, Mark Hoffmann wrote:
> Hi James,
>
> thank you for your response.
>
> A has a foreign key to B A.id_B and I want the following:
>
> How can I achieve a one-to-one mapping where:
>
> - The foreign key in A (A.ID_B) is calculated in Java like e.g. A.ID_B = B.ID + 100
> OR
> - The id of B (B.ID) is calculated from the foreign key A.ID_B in Java B.ID = A:ID_B + 100.
>
> (But I think calculating the foreign key from (A.ID_B) from the id of B (B.ID) makes more sense)
>
> I want this work for SELECT, INSERT, UPDATE AND DELETE. A cascading UPDATE; INSERT, DELETE from A to B is possible.
>
> This is a test of pseudonymization. If you watch to the tables A and B you shouldn't recognize the relation between the two tables. For this the calculation from above should be better ;).
>
> Regards,
> Mark
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.03948 seconds