You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Craig L Russell <Cr...@Sun.COM> on 2009/07/08 01:06:24 UTC

Help with FieldMapping metadata

Hi,

I've got two classes with a one-many relationship based on column  
values, but there's no foreign key.

I've looked up the ClassMapping and FieldMapping for the fields in the  
"many" class and have the FieldMapping object for the relationship  
that's mapped to the columns.

What I'm looking for is how to extract the column values from the oid  
class corresponding to the primary key of the related class.

If I can get the pk columns that correspond to the joined columns then  
I can get the corresponding FieldMapping for the primary key fields.  
But I can't find how to get either the corresponding joined columns or  
the corresponding primary key field mapping. FieldMapping has  
getJoinForeignKey but there's no foreign key, so it's null.

Seems there should be a getJoinSomething that gives me the pairs of  
<Column localColumn, Column relatedColumn> corresponding to the  
@JoinColumn annotation but I can't find it.

Any ideas?

Thanks,

Craig

/** Schema
  *
create table longlongstringfk (
  longpk1 bigint not null,
  longpk2 bigint not null,
  stringpk varchar(10) not null,
  longfk1 bigint not null,
  longfk2 bigint not null,
  stringfk varchar(10) not null,
  stringvalue varchar(10),
         CONSTRAINT PK_longlongstringfk PRIMARY KEY (longpk1, longpk2,  
stringpk)
*/

public class LongLongStringFKRelationship extends  
LongLongStringConstants implements Serializable {

     @javax.persistence.Id
     @javax.persistence.Column(name="longpk1")
     private long longpk1;

     @javax.persistence.Id
     @javax.persistence.Column(name="longpk2")
     private long longpk2;

     @javax.persistence.Id
     @javax.persistence.Column(name="stringpk")
     private String stringpk;


     @ManyToOne
     @JoinColumns({
         @JoinColumn(name="longfk1", referencedColumnName="longpk1"),
         @JoinColumn(name="longfk2", referencedColumnName="longpk2"),
         @JoinColumn(name="stringfk", referencedColumnName="stringpk")
         })
     private LongLongStringPKRelationship longLongStringPKRelationship;

Here's the related class:

/** Schema
  *
create table longlongstringpk (
  longpk1 bigint not null,
  longpk2 bigint not null,
  stringpk varchar(10) not null,
  stringvalue varchar(10),
         CONSTRAINT PK_longlongstringpk PRIMARY KEY (longpk1, longpk2,  
stringpk)

public class LongLongStringPKRelationship extends  
LongLongStringConstants implements Serializable {

     @javax.persistence.Id
     @javax.persistence.Column(name="longpk1")
     private long longpk1;

     @javax.persistence.Id
     @javax.persistence.Column(name="longpk2")
     private long longpk2;

     @javax.persistence.Id
     @javax.persistence.Column(name="stringpk")
     private String stringpk;

     @javax.persistence.Column(name="stringvalue")
     private String stringvalue;

     @OneToMany(mappedBy = "longLongStringPKRelationship")
     private Collection<LongLongStringFKRelationship>  
longLongStringFKRelationships;



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


Re: Help with FieldMapping metadata

Posted by Craig L Russell <Cr...@Sun.COM>.
I found the information. It was in the fieldMapping.getForeignKey  
which returns a ForeignKey that contains the mapping information from  
JoinColumn metadata.

Thanks,

Craig

On Jul 7, 2009, at 10:08 PM, Craig L Russell wrote:

> Here's some more information:
>
> I thought getJoinForeignKey should return a logical foreign key, but  
> with this mapping, it doesn't work. None of the possible  
> relationship column mapping information seems to be filled in. This  
> is after resolving the metadata at runtime.
>
> I've tried getColumn.getTarget, getColumn.getTargetField,  
> fieldMapping.getForeignKey, fieldMapping.getJoinForeignKey,  
> fieldMapping.getValueInfo.getForeignKey.
>
> Here's what I've tried, to no avail:
>                relatedTypeMapping =  
> fieldMapping.getDeclaredTypeMapping();
>                relatedType = relatedTypeMapping.getDescribedType();
>                fieldMapping.getColumns();
>                Class oid = relatedTypeMapping.getObjectIdType();
>                StringBuffer message = new StringBuffer("For class: "  
> + domainTypeHandler.getName() +
>                        " field: " + name + " related type is: " +  
> relatedType.getName() +
>                        " objectid type: " + oid.getName());
>                // get the column corresponding to the local column
>                for (Column localColumn: columns) {
>                    message.append(" column: " +  
> localColumn.getName());
>                    String target = localColumn.getTarget();
>                    message.append(" target-> " + target);
>                    String targetField = localColumn.getTargetField();
>                    message.append(" targetField-> " + targetField);
>                    ForeignKey jfk = fieldMapping.getJoinForeignKey();
>                    message.append(" join-> " + jfk);
>                    ForeignKey fk = fieldMapping.getForeignKey();
>                    message.append(" fk-> " + jfk);
>                    ValueMappingInfo vinfo =  
> fieldMapping.getValueInfo();
>                    message.append(" vinfo-> " + vinfo);
>                    ForeignKey vfk = vinfo.getForeignKey();
>                    message.append(" vfk-> " + jfk);
>
> For class:  
> com.mysql.clusterj.jpatest.model.LongLongStringFKRelationship field:  
> longLongStringPKRelationship related type is:  
> com.mysql.clusterj.jpatest.model.LongLongStringPKRelationship  
> objectid type: com.mysql.clusterj.jpatest.model.LongLongStringOid  
> column: longfk1 target-> null targetField-> null join-> null fk->  
> null vinfo-> org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628  
> vfk-> null column: longfk2 target-> null targetField-> null join->  
> null fk-> null vinfo->  
> org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628 vfk-> null  
> column: stringfk target-> null targetField-> null join-> null fk->  
> null vinfo-> org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628  
> vfk-> null
>
>
> Thanks,
>
> Craig
>
> On Jul 7, 2009, at 4:06 PM, Craig L Russell wrote:
>
>> Hi,
>>
>> I've got two classes with a one-many relationship based on column  
>> values, but there's no foreign key.
>>
>> I've looked up the ClassMapping and FieldMapping for the fields in  
>> the "many" class and have the FieldMapping object for the  
>> relationship that's mapped to the columns.
>>
>> What I'm looking for is how to extract the column values from the  
>> oid class corresponding to the primary key of the related class.
>>
>> If I can get the pk columns that correspond to the joined columns  
>> then I can get the corresponding FieldMapping for the primary key  
>> fields. But I can't find how to get either the corresponding joined  
>> columns or the corresponding primary key field mapping.  
>> FieldMapping has getJoinForeignKey but there's no foreign key, so  
>> it's null.
>>
>> Seems there should be a getJoinSomething that gives me the pairs of  
>> <Column localColumn, Column relatedColumn> corresponding to the  
>> @JoinColumn annotation but I can't find it.
>>
>> Any ideas?
>>
>> Thanks,
>>
>> Craig
>>
>> /** Schema
>> *
>> create table longlongstringfk (
>> longpk1 bigint not null,
>> longpk2 bigint not null,
>> stringpk varchar(10) not null,
>> longfk1 bigint not null,
>> longfk2 bigint not null,
>> stringfk varchar(10) not null,
>> stringvalue varchar(10),
>>       CONSTRAINT PK_longlongstringfk PRIMARY KEY (longpk1, longpk2,  
>> stringpk)
>> */
>>
>> public class LongLongStringFKRelationship extends  
>> LongLongStringConstants implements Serializable {
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="longpk1")
>>   private long longpk1;
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="longpk2")
>>   private long longpk2;
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="stringpk")
>>   private String stringpk;
>>
>>
>>   @ManyToOne
>>   @JoinColumns({
>>       @JoinColumn(name="longfk1", referencedColumnName="longpk1"),
>>       @JoinColumn(name="longfk2", referencedColumnName="longpk2"),
>>       @JoinColumn(name="stringfk", referencedColumnName="stringpk")
>>       })
>>   private LongLongStringPKRelationship longLongStringPKRelationship;
>>
>> Here's the related class:
>>
>> /** Schema
>> *
>> create table longlongstringpk (
>> longpk1 bigint not null,
>> longpk2 bigint not null,
>> stringpk varchar(10) not null,
>> stringvalue varchar(10),
>>       CONSTRAINT PK_longlongstringpk PRIMARY KEY (longpk1, longpk2,  
>> stringpk)
>>
>> public class LongLongStringPKRelationship extends  
>> LongLongStringConstants implements Serializable {
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="longpk1")
>>   private long longpk1;
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="longpk2")
>>   private long longpk2;
>>
>>   @javax.persistence.Id
>>   @javax.persistence.Column(name="stringpk")
>>   private String stringpk;
>>
>>   @javax.persistence.Column(name="stringvalue")
>>   private String stringvalue;
>>
>>   @OneToMany(mappedBy = "longLongStringPKRelationship")
>>   private Collection<LongLongStringFKRelationship>  
>> longLongStringFKRelationships;
>>
>>
>>
>> Craig L Russell
>> Architect, Sun Java Enterprise System http://db.apache.org/jdo
>> 408 276-5638 mailto:Craig.Russell@sun.com
>> P.S. A good JDO? O, Gasp!
>>
>
> Craig L Russell
> Architect, Sun Java Enterprise System http://db.apache.org/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>

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


Re: Help with FieldMapping metadata

Posted by Craig L Russell <Cr...@Sun.COM>.
Here's some more information:

I thought getJoinForeignKey should return a logical foreign key, but  
with this mapping, it doesn't work. None of the possible relationship  
column mapping information seems to be filled in. This is after  
resolving the metadata at runtime.

I've tried getColumn.getTarget, getColumn.getTargetField,  
fieldMapping.getForeignKey, fieldMapping.getJoinForeignKey,  
fieldMapping.getValueInfo.getForeignKey.

Here's what I've tried, to no avail:
                 relatedTypeMapping =  
fieldMapping.getDeclaredTypeMapping();
                 relatedType = relatedTypeMapping.getDescribedType();
                 fieldMapping.getColumns();
                 Class oid = relatedTypeMapping.getObjectIdType();
                 StringBuffer message = new StringBuffer("For class: "  
+ domainTypeHandler.getName() +
                         " field: " + name + " related type is: " +  
relatedType.getName() +
                         " objectid type: " + oid.getName());
                 // get the column corresponding to the local column
                 for (Column localColumn: columns) {
                     message.append(" column: " +  
localColumn.getName());
                     String target = localColumn.getTarget();
                     message.append(" target-> " + target);
                     String targetField = localColumn.getTargetField();
                     message.append(" targetField-> " + targetField);
                     ForeignKey jfk = fieldMapping.getJoinForeignKey();
                     message.append(" join-> " + jfk);
                     ForeignKey fk = fieldMapping.getForeignKey();
                     message.append(" fk-> " + jfk);
                     ValueMappingInfo vinfo =  
fieldMapping.getValueInfo();
                     message.append(" vinfo-> " + vinfo);
                     ForeignKey vfk = vinfo.getForeignKey();
                     message.append(" vfk-> " + jfk);

For class:  
com.mysql.clusterj.jpatest.model.LongLongStringFKRelationship field:  
longLongStringPKRelationship related type is:  
com.mysql.clusterj.jpatest.model.LongLongStringPKRelationship objectid  
type: com.mysql.clusterj.jpatest.model.LongLongStringOid column:  
longfk1 target-> null targetField-> null join-> null fk-> null vinfo->  
org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628 vfk-> null  
column: longfk2 target-> null targetField-> null join-> null fk-> null  
vinfo-> org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628 vfk->  
null column: stringfk target-> null targetField-> null join-> null fk- 
 > null vinfo-> org.apache.openjpa.jdbc.meta.ValueMappingInfo@d6f628  
vfk-> null


Thanks,

Craig

On Jul 7, 2009, at 4:06 PM, Craig L Russell wrote:

> Hi,
>
> I've got two classes with a one-many relationship based on column  
> values, but there's no foreign key.
>
> I've looked up the ClassMapping and FieldMapping for the fields in  
> the "many" class and have the FieldMapping object for the  
> relationship that's mapped to the columns.
>
> What I'm looking for is how to extract the column values from the  
> oid class corresponding to the primary key of the related class.
>
> If I can get the pk columns that correspond to the joined columns  
> then I can get the corresponding FieldMapping for the primary key  
> fields. But I can't find how to get either the corresponding joined  
> columns or the corresponding primary key field mapping. FieldMapping  
> has getJoinForeignKey but there's no foreign key, so it's null.
>
> Seems there should be a getJoinSomething that gives me the pairs of  
> <Column localColumn, Column relatedColumn> corresponding to the  
> @JoinColumn annotation but I can't find it.
>
> Any ideas?
>
> Thanks,
>
> Craig
>
> /** Schema
> *
> create table longlongstringfk (
> longpk1 bigint not null,
> longpk2 bigint not null,
> stringpk varchar(10) not null,
> longfk1 bigint not null,
> longfk2 bigint not null,
> stringfk varchar(10) not null,
> stringvalue varchar(10),
>        CONSTRAINT PK_longlongstringfk PRIMARY KEY (longpk1, longpk2,  
> stringpk)
> */
>
> public class LongLongStringFKRelationship extends  
> LongLongStringConstants implements Serializable {
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="longpk1")
>    private long longpk1;
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="longpk2")
>    private long longpk2;
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="stringpk")
>    private String stringpk;
>
>
>    @ManyToOne
>    @JoinColumns({
>        @JoinColumn(name="longfk1", referencedColumnName="longpk1"),
>        @JoinColumn(name="longfk2", referencedColumnName="longpk2"),
>        @JoinColumn(name="stringfk", referencedColumnName="stringpk")
>        })
>    private LongLongStringPKRelationship longLongStringPKRelationship;
>
> Here's the related class:
>
> /** Schema
> *
> create table longlongstringpk (
> longpk1 bigint not null,
> longpk2 bigint not null,
> stringpk varchar(10) not null,
> stringvalue varchar(10),
>        CONSTRAINT PK_longlongstringpk PRIMARY KEY (longpk1, longpk2,  
> stringpk)
>
> public class LongLongStringPKRelationship extends  
> LongLongStringConstants implements Serializable {
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="longpk1")
>    private long longpk1;
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="longpk2")
>    private long longpk2;
>
>    @javax.persistence.Id
>    @javax.persistence.Column(name="stringpk")
>    private String stringpk;
>
>    @javax.persistence.Column(name="stringvalue")
>    private String stringvalue;
>
>    @OneToMany(mappedBy = "longLongStringPKRelationship")
>    private Collection<LongLongStringFKRelationship>  
> longLongStringFKRelationships;
>
>
>
> Craig L Russell
> Architect, Sun Java Enterprise System http://db.apache.org/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>

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