@JoinColumn: Using a function in "name" or "referencedColumnName"

View: New views
2 Messages — Rating Filter:   Alert me  

@JoinColumn: Using a function in "name" or "referencedColumnName"

by Zarar Siddiqi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I've come across a situation where I need to use a DB2 function inside @JoinColumn attributes.  I want to have something like this:

   @OneToMany @JoinColumns({
           @JoinColumn(name="PERSON_ID", referencedColumnName = "PERSON_ID"),
           @JoinColumn(name="PERSON_TYPE", referencedColumnName = "SUBSTR(PERSON_TYPE,1,5)")
   })

However, I get the following exception message when I do this since SUBSTR(PERSON_TYPE,1,5) isn't really a column.  If I put PERSON_TYPE, everything works fine but it's incorrect business logic.

When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referenceColumnName elements must be specified in each such @JoinColumn.

I understand that this isn't exactly how most composite key relationship are modeled but I'm working with a legacy system.  Any ideas for a workaround?  I really don't want to use a native query.

Thanks

_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: @JoinColumn: Using a function in "name" or "referencedColumnName"

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

JPA only allows basic fields/foreign keys in JoinColumns.  You can do this in EclipseLink using a DescriptorCustomizer.  Get the mapping from the descriptor and set its selectionCriteria using an EclipseLink Expression.  The Expression can use the function or any other complex criteria.

Zarar Siddiqi wrote:
Hello,

I've come across a situation where I need to use a DB2 function inside
@JoinColumn attributes.  I want to have something like this:

   @OneToMany @JoinColumns({
           @JoinColumn(name="PERSON_ID", referencedColumnName =
"PERSON_ID"),
           @JoinColumn(name="PERSON_TYPE", referencedColumnName = "*
SUBSTR(PERSON_TYPE,1,5)*")
   })

However, I get the following exception message when I do this since
SUBSTR(PERSON_TYPE,1,5) isn't really a column.  If I put PERSON_TYPE,
everything works fine but it's incorrect business logic.

*When the source entity class uses a composite primary key, a @JoinColumn
must be specified for each join column using the @JoinColumns. Both the name
and the referenceColumnName elements must be specified in each such
@JoinColumn.*

I understand that this isn't exactly how most composite key relationship are
modeled but I'm working with a legacy system.  Any ideas for a workaround?
I really don't want to use a native query.

Thanks