user10214830
user10214830

Reputation:

@JoinFormula in Hibernate

I am getting started with Hibernate and I have such problem I do not understand such line in code I am analysing:

@JoinFormula( "REGEXP_REPLACE(phoneNumber, '\\+(\\d+)-.*', '\\1')::int" )

The source I am using is: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#embeddables

I don't know where is the dependency between country Id and phoneNumber. Regards

Upvotes: 1

Views: 1355

Answers (1)

LppEdd
LppEdd

Reputation: 21104

Reading a little bit after that snippet, you'd find

The country association in the User entity is mapped by the country identifier provided by the phoneNumber property.

And

Therefore, the @JoinFormula annotation is used to define a custom join association between the parent-child association.


Basically, the JOIN between User and Country is made by that calculated value.

For example, a User with a number

+40-123-4567

is JOINed with a Country that had an ID of

40

because the REGEXP_REPLACE function is able to extrapolate that 40 from the number.

::int is a type cast.

Upvotes: 2

Related Questions