I'm using JPA 2.0, Hibernate 4.1.0.Final, and MySQL 5.5.27. I want to construct a JPA query that will return two entities per row and I want to perform a right outer join. The two entities are:
@Entity
@Table(name = "user",
uniqueConstraints = { @UniqueConstraint(columnNames = { "USER_NAME" }) }
)
public class User implements Comparable<User>, Serializable
{
...
@Column(name = "first_name")
@NotNull
/* the first name of the User */
private String firstName;
and
@Entity
@Table(name="code_user",
uniqueConstraints = {
@UniqueConstraint(columnNames = { "CODE_ID", "USER_ID" }) }
)
public class CodeUser
{
@Id
@NotNull
@GeneratedValue(generator = "uuid-strategy")
@Column(name = "ID")
private String id;
@ManyToOne
@JoinColumn(name = "CODE_ID", nullable = false, updatable = true)
private Code code;
@ManyToOne
@JoinColumn(name = "USER_ID", nullable = false, updatable = true)
private User user;
Here's my JPA so far, but the problem is that an inner join is being performed, and only one entity per row (the User object) is returned ...
final CriteriaBuilder builder = m_entityManager.getCriteriaBuilder();
final CriteriaQuery<User> criteria = builder.createQuery(User.class);
final Root<User> user = criteria.from(User.class);
final Root<CodeUser> codeUser = criteria.from(CodeUser.class);
final Join<CodeUser, User> joinUser = codeUser.join(CodeUser_.user);
criteria.select(joinUser);
…
return criteria.where(builder.and(preds.toArray(new Predicate[preds.size()])));
How do I perform an outer join so that all Users are returned and any matching CodeUser records are also returned? It's not an option to add extra member fields to my User entity.