Home > Net >  How To Convert hibernate xml many-to-many mapping having formula into annotation?
How To Convert hibernate xml many-to-many mapping having formula into annotation?

Time:01-20

<set name="users"
table = "USER_RESTRICTION_ITEMS"
schema = "ABC"
lazy = true >
<key>
<column name = "USER_RESTRICTION_ID">
</key>
<many-to-many

formula="TRIM(USER_ID)"
<many-to-many>
</set>

I have 3 tables USER,USER_RESTRICTION and USER_RESTRICTION_ITEMS and i have 2 classes User and UserRestriction .

Pojo for UserRestriction is below having User class as a parameter I have to create @ManyToMany Relationship between them using formula like above xml code

@Entity
@Table(name="USER_RESTRICTION")
class UserRestriction {

  @Id
  @Column(name = "USER_RESTRICTION_ID")
  private Long id;

  private Set<User> users;

}

the third table USER_RESTRICTION_ITEMS contains column USER_ID,USER_RESTRICTION_ID

I have tried simple Many to many mapping but not able to use TRIM(USER_ID) in formula

CodePudding user response:

It looks like there's currently no way to do this using annotations, and I think I can see why.

The problem is that for @ManyToMany, the @JoinColumn annotation is supposed to belong to @JoinTable, since you can have both joinColumns and inverseJoinColumns. (This is in compliance with the JPA spec.)

That doesn't leave us with a natural place to put a @JoinFormula annotation. At least, not without introducing our own ugly @JoinTableWithFormula annotation or something. And I can see why nobody ever wanted to add that.

But let me think about this a bit. It's annoying that Hibernate has useful functionality only accessible via XML.

  • Related