Gaetano Piazzolla
Gaetano Piazzolla

Reputation: 1557

JPA differences between Join and JoinSet

As the title say, I want to know the differences among these two methods.

Specifically I want to know the difference between:

join(String arg) AND joinSet(String arg)

As I can use join(String arg) even if the attribute is a Set, but not the opposite, namely using joinSet(String arg) on an attribute that is not a Set.

Thanks.

Upvotes: 9

Views: 5461

Answers (2)

ConMan
ConMan

Reputation: 1652

The join method is used to create an inner join on a single attribute i.e. a one to one relationship.

   /*Create an inner join to the specified single-valued attribute.
    Parameters:
    attribute target of the join
    Returns:
    the resulting join*/
74 
75     <Y> Join<X, Y> More ...join(SingularAttribute<? super X, Y> attribute);

While the method joinSet is used to create an inner join for a set of attribtues, i.e. a one to many relationship.

 /*Create an inner join to the specified Set-valued attribute.
    Parameters:
    attributeName name of the attribute for the target of the join
    Returns:
    the resulting join
    Throws:
    java.lang.IllegalArgumentException if attribute of the given name does not exist*/
182
183    <X, Y> SetJoin<X, Y> More ...joinSet(String attributeName);  

However, if you look at the return types of the methods, join returns type Join and joinSet returns type SetJoin, which implements Join. This means that it would be entirely possible for the implementing application to put in some logic that detects if you are trying to join a set or single attribute and forward the process onto the joinSet method if necessary. Without knownig what implementation you are using, I cant really comment much further on that.

Source code found on grep code here

Upvotes: 4

Antoniossss
Antoniossss

Reputation: 32535

It is all about type-safety. Straightforward example, you won't be able to use criterion isEmpty on join attribute, as isEmpty requires argument to be an collection attribute so there is lower chance of getting human error involved. If you will use proper joins, you will get compile time error insteed of runtime error.

All in all, it is all about type-safety - thats why you can still join to collections, but cannot collection join to singular attributes.

Upvotes: 3

Related Questions