Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Transform foreign keys in One-To-One Relationship
Transform foreign keys in One-To-One Relationship [message #557784] Wed, 08 September 2010 17:52 Go to next message
Mark Hoffmann is currently offline Mark Hoffmann
Messages: 102
Registered: July 2009
Location: Jena
Senior Member
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 Go to previous messageGo to next message
Mark Hoffmann is currently offline Mark Hoffmann
Messages: 102
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 13:58 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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
Re: Transform foreign keys in One-To-One Relationship [message #558697 is a reply to message #558625] Tue, 14 September 2010 01:43 Go to previous messageGo to next message
Mark Hoffmann is currently offline Mark Hoffmann
Messages: 102
Registered: July 2009
Location: Jena
Senior Member
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 Wink.

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 Go to previous message
Tom Eugelink is currently offline Tom Eugelink
Messages: 806
Registered: July 2009
Senior Member
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
>
>
Previous Topic:NoSuchMethodError _persistence_set(Ljava/lang/String;Ljava/lang/Object;)V
Next Topic:Query with multiple matches in collection
Goto Forum:
  


Current Time: Mon Jul 28 04:19:34 EDT 2014

Powered by FUDForum. Page generated in 0.02688 seconds