You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by saravanan_tvm <sk...@yahoo.com> on 2007/07/27 06:28:17 UTC

How to map multiple properties to same column as writable in jpa

Hi,
I am using toplink from oracle to support JPA.
I have the following requirement...
i have to map same column for multiple properties using @joincolumn
annotation.. eg its something like this

@joincolumn(name="PARENT_RECORD_ID")
private Drug drug

@joincolumn(name="PARENT_RECORD_ID")
private Reaction reaction

PARENT_RECORD_ID is nothing but primary key of parent table.
but while accessing the application i am getting the following exception....

 MULTIPLE_WRITE_MAPPINGS_FOR_FIELD  

Cause: Multiple writable mappings for the field fieldName are defined in the
descriptor. Exactly one must be defined as writable; the others must be
specified as read-only. When multiple write mappings are defined for the
field, TopLink is unable to choose the appropriate mapping for writing the
value of the field in the database row. Therefore, the exception is raised
during the validation process of descriptors. 

Action: Make one of those mappings read-only

But i cannot make read only, if i make read only then while inserting new
records it will be problem

please can anyone provide the solution for this...
Regards,
Saravanan.

-- 
View this message in context: http://www.nabble.com/How-to-map-multiple-properties-to-same-column-as-writable-in-jpa-tf4155235.html#a11822480
Sent from the OpenJPA Developers mailing list archive at Nabble.com.


Re: How to map multiple properties to same column as writable in jpa

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi Saravanan,

The code snippet below doesn't look like the source of the problem.

Perhaps you could post all of the annotations for the fields in  
question? And try using OpenJPA...

Regards,

Craig

On Jul 26, 2007, at 9:28 PM, saravanan_tvm wrote:

>
> Hi,
> I am using toplink from oracle to support JPA.
> I have the following requirement...
> i have to map same column for multiple properties using @joincolumn
> annotation.. eg its something like this
>
> @joincolumn(name="PARENT_RECORD_ID")
> private Drug drug
>
> @joincolumn(name="PARENT_RECORD_ID")
> private Reaction reaction
>
> PARENT_RECORD_ID is nothing but primary key of parent table.
> but while accessing the application i am getting the following  
> exception....
>
>  MULTIPLE_WRITE_MAPPINGS_FOR_FIELD
>
> Cause: Multiple writable mappings for the field fieldName are  
> defined in the
> descriptor. Exactly one must be defined as writable; the others  
> must be
> specified as read-only. When multiple write mappings are defined  
> for the
> field, TopLink is unable to choose the appropriate mapping for  
> writing the
> value of the field in the database row. Therefore, the exception is  
> raised
> during the validation process of descriptors.
>
> Action: Make one of those mappings read-only
>
> But i cannot make read only, if i make read only then while  
> inserting new
> records it will be problem
>
> please can anyone provide the solution for this...
> Regards,
> Saravanan.
>
> -- 
> View this message in context: http://www.nabble.com/How-to-map- 
> multiple-properties-to-same-column-as-writable-in-jpa- 
> tf4155235.html#a11822480
> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: How to map multiple properties to same column as writable in jpa

Posted by Elias Torres <el...@torrez.us>.
Thanks Gokhan.

I definitely should read the OpenJPA like a novel instead of only going
through when I need help.

I'll be trying insertable=false and updatable=false first. That's
exactly what I think I need. Later, I'll try @Transient and the family
of events, but like you said, it's much riskier.

I had already read about fetchgroups but have not tried it.

-Elias

Gokhan Ergul wrote:
> Elias Torres wrote:
> 
>> @ManyToOne(fetch=FetchType.LAZY)
>> @JoinColumn(name="userid", referencedColumnName="id")
>> private User user;
>>
>> and
>>
>> @Column(name="userid")
>> private String userId;
>>   
> @Column(name="userid", insertable=false, updatable=false)
> private String userId;
> 
>> How would I go about keeping them in sync? Especially, when someone
>> calls setUser(user) with a new instance that might not have an id.
>>   
> AFAIK there's no built-in mechanism to keep those two members in sync.
> One possibility is to write the sync code in setX() methods (pre-flush
> sync) and provide a PostPersist method to sync after commit. A similar
> approach would be making "userId" non-managed (i.e. @Transient), and
> using a combination of PostLoad/PostPersist/setX to keep the two members
> in sync.
> 
> Though I should warn that things get pretty hairy if you have a
> highly-connected object model and manipulating entities in a detached
> context (as in moving entities to a different tier, "editing" them and
> moving them back to server tier, over rmi, webservices or whatnot).
> 
>> SELECT f FROM Foo f JOIN f.user u WHERE u.id = ?
>>
>> as opposed to when I have the user id already.
>>
>> SELECT f FROM Foo f WHERE f.userid = ?
> SELECT f FROM Foo f WHERE f.user.id = ?
> 
>> Is it a practice to create multiple versions of an entity (light vs
>> heavy) to optimize the queries? One with @ManyToOne and another with
>> @Basic.
>>   
> That really depends on your application requirements, but for most
> distributed applications you'll have to come up with a way to retrieve
> "shallow" vs. "deep" objects --for standalone apps that's much less of a
> concern. In case you need to do it, see
> 
> http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_fetch
> 
> Note that this is not part of the JPA spec, but an OpenJPA extension.
> 
> Gokhan.
> 
> 

Re: How to map multiple properties to same column as writable in jpa

Posted by Gokhan Ergul <go...@telenity.com>.
Elias Torres wrote:

> @ManyToOne(fetch=FetchType.LAZY)
> @JoinColumn(name="userid", referencedColumnName="id")
> private User user;
>
> and
>
> @Column(name="userid")
> private String userId;
>   
@Column(name="userid", insertable=false, updatable=false)
private String userId;

> How would I go about keeping them in sync? Especially, when someone
> calls setUser(user) with a new instance that might not have an id.
>   
AFAIK there's no built-in mechanism to keep those two members in sync. 
One possibility is to write the sync code in setX() methods (pre-flush 
sync) and provide a PostPersist method to sync after commit. A similar 
approach would be making "userId" non-managed (i.e. @Transient), and 
using a combination of PostLoad/PostPersist/setX to keep the two members 
in sync.

Though I should warn that things get pretty hairy if you have a 
highly-connected object model and manipulating entities in a detached 
context (as in moving entities to a different tier, "editing" them and 
moving them back to server tier, over rmi, webservices or whatnot).

> SELECT f FROM Foo f JOIN f.user u WHERE u.id = ?
>
> as opposed to when I have the user id already.
>
> SELECT f FROM Foo f WHERE f.userid = ?
SELECT f FROM Foo f WHERE f.user.id = ?

> Is it a practice to create multiple versions of an entity (light vs
> heavy) to optimize the queries? One with @ManyToOne and another with @Basic.
>   
That really depends on your application requirements, but for most 
distributed applications you'll have to come up with a way to retrieve 
"shallow" vs. "deep" objects --for standalone apps that's much less of a 
concern. In case you need to do it, see

http://openjpa.apache.org/docs/latest/manual/manual.html#ref_guide_fetch

Note that this is not part of the JPA spec, but an OpenJPA extension.

Gokhan.


Re: How to map multiple properties to same column as writable in jpa

Posted by Elias Torres <el...@torrez.us>.
Furthermore,

Because I haven't been able to do this dual-mapping of a column, I'm
forced to write JPQL queries like this:

SELECT f FROM Foo f JOIN f.user u WHERE u.id = ?

as opposed to when I have the user id already.

SELECT f FROM Foo f WHERE f.userid = ?

Is it a practice to create multiple versions of an entity (light vs
heavy) to optimize the queries? One with @ManyToOne and another with @Basic.

BTW, I'm neither an ORM/Relational Algebra expert just trying to
understand [Open]JPA. I have worked with Hibernate on Apache Roller and
also with iBatis.

-Elias

Elias Torres wrote:
> I was wondering the same thing (but with JPA of course). I tried and was
> getting a message that the members were out of sync.
> 
> @ManyToOne(fetch=FetchType.LAZY)
> @JoinColumn(name="userid", referencedColumnName="id")
> private User user;
> 
> and
> 
> @Column(name="userid")
> private String userId;
> 
> How would I go about keeping them in sync? Especially, when someone
> calls setUser(user) with a new instance that might not have an id.
> 
> Is there a way in OpenJPA to say that the userId column is only for
> read-only and not persist operations?
> 
> Thanks.
> 
> -Elias
> 
> Patrick Linskey wrote:
>> Hi,
>>
>> This forum is for the OpenJPA product, so we don't have much
>> experience with Toplink.
>>
>> However, from an OpenJPA standpoint, you can map the same column to
>> multiple fields without a problem, as long as you don't attempt to set
>> the fields to different values during the same transaction.
>>
>> -Patrick
>>
>> On 7/26/07, saravanan_tvm <sk...@yahoo.com> wrote:
>>> Hi,
>>> I am using toplink from oracle to support JPA.
>>> I have the following requirement...
>>> i have to map same column for multiple properties using @joincolumn
>>> annotation.. eg its something like this
>>>
>>> @joincolumn(name="PARENT_RECORD_ID")
>>> private Drug drug
>>>
>>> @joincolumn(name="PARENT_RECORD_ID")
>>> private Reaction reaction
>>>
>>> PARENT_RECORD_ID is nothing but primary key of parent table.
>>> but while accessing the application i am getting the following exception....
>>>
>>>  MULTIPLE_WRITE_MAPPINGS_FOR_FIELD
>>>
>>> Cause: Multiple writable mappings for the field fieldName are defined in the
>>> descriptor. Exactly one must be defined as writable; the others must be
>>> specified as read-only. When multiple write mappings are defined for the
>>> field, TopLink is unable to choose the appropriate mapping for writing the
>>> value of the field in the database row. Therefore, the exception is raised
>>> during the validation process of descriptors.
>>>
>>> Action: Make one of those mappings read-only
>>>
>>> But i cannot make read only, if i make read only then while inserting new
>>> records it will be problem
>>>
>>> please can anyone provide the solution for this...
>>> Regards,
>>> Saravanan.
>>>
>>> --
>>> View this message in context: http://www.nabble.com/How-to-map-multiple-properties-to-same-column-as-writable-in-jpa-tf4155235.html#a11822480
>>> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>>>
>>>
>>
> 

Re: How to map multiple properties to same column as writable in jpa

Posted by Elias Torres <el...@torrez.us>.
I was wondering the same thing (but with JPA of course). I tried and was
getting a message that the members were out of sync.

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="userid", referencedColumnName="id")
private User user;

and

@Column(name="userid")
private String userId;

How would I go about keeping them in sync? Especially, when someone
calls setUser(user) with a new instance that might not have an id.

Is there a way in OpenJPA to say that the userId column is only for
read-only and not persist operations?

Thanks.

-Elias

Patrick Linskey wrote:
> Hi,
> 
> This forum is for the OpenJPA product, so we don't have much
> experience with Toplink.
> 
> However, from an OpenJPA standpoint, you can map the same column to
> multiple fields without a problem, as long as you don't attempt to set
> the fields to different values during the same transaction.
> 
> -Patrick
> 
> On 7/26/07, saravanan_tvm <sk...@yahoo.com> wrote:
>> Hi,
>> I am using toplink from oracle to support JPA.
>> I have the following requirement...
>> i have to map same column for multiple properties using @joincolumn
>> annotation.. eg its something like this
>>
>> @joincolumn(name="PARENT_RECORD_ID")
>> private Drug drug
>>
>> @joincolumn(name="PARENT_RECORD_ID")
>> private Reaction reaction
>>
>> PARENT_RECORD_ID is nothing but primary key of parent table.
>> but while accessing the application i am getting the following exception....
>>
>>  MULTIPLE_WRITE_MAPPINGS_FOR_FIELD
>>
>> Cause: Multiple writable mappings for the field fieldName are defined in the
>> descriptor. Exactly one must be defined as writable; the others must be
>> specified as read-only. When multiple write mappings are defined for the
>> field, TopLink is unable to choose the appropriate mapping for writing the
>> value of the field in the database row. Therefore, the exception is raised
>> during the validation process of descriptors.
>>
>> Action: Make one of those mappings read-only
>>
>> But i cannot make read only, if i make read only then while inserting new
>> records it will be problem
>>
>> please can anyone provide the solution for this...
>> Regards,
>> Saravanan.
>>
>> --
>> View this message in context: http://www.nabble.com/How-to-map-multiple-properties-to-same-column-as-writable-in-jpa-tf4155235.html#a11822480
>> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>>
>>
> 
> 

Re: How to map multiple properties to same column as writable in jpa

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

This forum is for the OpenJPA product, so we don't have much
experience with Toplink.

However, from an OpenJPA standpoint, you can map the same column to
multiple fields without a problem, as long as you don't attempt to set
the fields to different values during the same transaction.

-Patrick

On 7/26/07, saravanan_tvm <sk...@yahoo.com> wrote:
>
> Hi,
> I am using toplink from oracle to support JPA.
> I have the following requirement...
> i have to map same column for multiple properties using @joincolumn
> annotation.. eg its something like this
>
> @joincolumn(name="PARENT_RECORD_ID")
> private Drug drug
>
> @joincolumn(name="PARENT_RECORD_ID")
> private Reaction reaction
>
> PARENT_RECORD_ID is nothing but primary key of parent table.
> but while accessing the application i am getting the following exception....
>
>  MULTIPLE_WRITE_MAPPINGS_FOR_FIELD
>
> Cause: Multiple writable mappings for the field fieldName are defined in the
> descriptor. Exactly one must be defined as writable; the others must be
> specified as read-only. When multiple write mappings are defined for the
> field, TopLink is unable to choose the appropriate mapping for writing the
> value of the field in the database row. Therefore, the exception is raised
> during the validation process of descriptors.
>
> Action: Make one of those mappings read-only
>
> But i cannot make read only, if i make read only then while inserting new
> records it will be problem
>
> please can anyone provide the solution for this...
> Regards,
> Saravanan.
>
> --
> View this message in context: http://www.nabble.com/How-to-map-multiple-properties-to-same-column-as-writable-in-jpa-tf4155235.html#a11822480
> Sent from the OpenJPA Developers mailing list archive at Nabble.com.
>
>


-- 
Patrick Linskey
202 669 5907