0

JPA join: can join to Entity, Embeddable or basic type.

@Entity
public class Book {
   @Id String id;

   @Embedded
   Author author;

   String name;
}

Root<Book> root = criteriaQuery.from(Book.class);
root.join("author", JoinType.LEFT);
// or
root.join("author", JoinType.INNER);
root.join("name", JoinType.INNER);

From JPA api, it is legal to join to an Embeddable and Basic type, but what does the JoinType mean? For inner join attribute "name", if book.name is null, the query will return empty results? the same for embeddable type?

eastwater
  • 4,624
  • 9
  • 49
  • 118

1 Answers1

1

I think you're misinterpreting the documentation.

A join can be used with OneToOne, ManyToOne, OneToMany, ManyToMany and ElementCollection mappings. This means the target type of a join may be an entity (when joining along entity associations), an embeddable (when joining to an ElementCollection of embeddables), or a basic type (when joining to an ElementCollection of basic types).

Nowhere does the above imply that it makes sense to use joins with a singular attribute of a basic type. JPA implementations might handle attempts to join to such attributes in a vendor specific way, though.

crizzis
  • 9,978
  • 2
  • 28
  • 47
  • If an Embeddable type has an association, it must join the Embedded attribute first before join to the association. So it can join to a singular attribute of Embeddable type. – eastwater Oct 25 '17 at 02:19
  • Not really. The only thing you need to do is navigate the path from the entity to the embedded attribute using `root.get()`. I'm guessing what you're really asking is how null values of the embedded attribute are handled. The answer is in JPA, an embedded attribute with null value is indistinguishable from a non-null embedded attribute filled with default values. – crizzis Oct 25 '17 at 08:27
  • root.get("embeddedAttrName") return a Path instance, which can not Join to an association. Join source must be a From. – eastwater Oct 25 '17 at 12:15
  • As you mentioned, the joinType for non-Entity may be just ignored. JPA spec does not say anything about it. – eastwater Oct 25 '17 at 12:18
  • You are right, my bad. In any case, due to the null value semantics that I described above, the type of join does not matter for embedded attributes – crizzis Oct 25 '17 at 16:19