Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Filtering OneToMany and ManyToOne relationship mappings

Hello Tom.

Thanks for the response. I got how it works. Perfect flexibility to be able to express the additional criterion as JPA-QL or SQL Expressions.

I'll create a base DescriptionCustomizer implementation and reuse it for the occurrences. Although i am thinking a good way to spread this into code, because i have about 45 occurences of scalar references to UserRole (Not really UserRole, was just an example), and other 50 to Sets of UserRole. So i am thinking of creating a custom annotation such as @UserRoleFilter(holeType="STANDARD") and add into mapping.

Will i be able to access the declaring Column from the DescriptionCustomizer somehow so i can grab the annotation data?

If you think it's a good approach, would i be too pretentious about creating a processor for this custom annotation so we don't need to create a bunch of DescriptorCustomizer extensions and have to declare them individually on the classes containing UserRole relationship occurences?

Thanks

Luciano G Santos



2013/6/3 Tom Ware <tom.ware@xxxxxxxxxx>
Hi Luciano,

  Filtering relationships in EclipseLink requires the use of our native API. The way you do it is to create a DescriptorCustomizer

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_customizer.htm#CHDCCDGC

  In the DescriptorCustomizer, you will have to access the mapping you want to change.  Then you will have to alter the selection criteria for the mapping. Here is some rough code from our test framework:

  OneToOneMapping _oneToOneMapping_ = (OneToOneMapping)descriptor.getMappingForAttributeName("computer");
  _expression_ exp = oneToOneMapping.buildSelectionCriteria();
  ExpressionBuilder builder = exp.getBuilder();
  _expression_ addedExpression = builder.getField("MAP_HRW.EMP_LNAME").equal("Louis");
  oneToOneMapping.setSelectionCriteria(exp.and(addedExpression));

  Hopefully that will get you started.  If you have problems, we can help you on this list.

-Tom


On 03/06/2013 11:49 AM, Luciano Santos wrote:
Hello.

I am about to change our project's JPA implementation from Hibernate to EclipseLink.
There is a tough relationship i am struggling to accomplish.

The case is similar as this (as much it might seems to make no much sense):

@Entity @Table(name="tb_role")
class Role {
   @Id
   private Long id;

   @Enumerated(STRING)
   private ProductType productType; // STANDARD, SPECIAL
   ...
}

@Entity @Table(name="rl_user_role")
class UserRole {
     ...
    @ManyToOne
    private Role role;

    private String comments;
    ...
}

@Entity @Table(name="tb_user")
class User {
      @Id
      private Long id;

      @ManyToOne
      // NEED to filter for SPECIAL (property or column value on Role)
      // DO NOT HAVE A LOCAL FK column, should use USER_ID on rl_user_role
      private UserRole claimForOthersRole;

      @OneToMany
      // NEED to filter for STANDARD (property or column value on Role)
      @JoinColumn(name = "user_id")

      private Set<UserRole> roles;

}


Do you guys have any clue on how could i accomplish this? I was trying to use
Hibernate's @Where annotation, but i could just filter using columns on
"rl_user_role" (Couldn't be nested).

Thank you

Luciano Santos


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

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


Back to the top