Java – is there any way to use secondarytable to jump multiple tables?
Suppose I have a main user table named userprofile and a display name
I have various modules. You can use userprofile and moduleid to represent your modulemembership Then, you can store data related to each different module's profile. For example, if you register a pokermodule, you will get a pokerprofile
I want to put the display name in the userprofile on the pokerprofile, but I want to do it in the normal way I can do this through hibernate or SQL The exact relationship will be pokerprofile membership. userProfile. DisplayName – how do I put it in the @ column of the pokerprofile class?
Solution
You can use derived property to get displayName in your pokerprofile class, as follows:
@Formula( "(SELECT up.displayName" + " FROM ModuleMembership mm" + " JOIN UserProfile up" + " ON (mm.userProfileId = up.userProfileId)" + " WHERE mm.moduleMembershipId = membershipId)") String displayName;
Note that the derived properties use only SQL, not HQL Membershipid is defined as the @ joincolumn name of the membership Other column names are similarly defined
However, although this is not what you require, I usually implement my own shortcut properties in Java, as shown below:
@Transient public String getDisplayName() { if (membership == null) { return null; } UserProfile userProfile = membership.getUserProfile(); return userProfile == null ? null : userProfile.getDisplayName(); }
For me, the properties of shortcuts are easier to read and write than the corresponding SQL
The test code example uses hibernate 5.0 6. Final H2 database version 1.4 190 perform the test