Search code examples
javamysqlhibernatejpahibernate-mapping

How to join fetch two associations with JPA/Hibernate


I am new to Hibernate and really need help from you guys.... I have following class: UserActionPerUserResult

@Entity
@Table(name = "user_action_per_user_result")
public class UserActionPerUserResult {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "user_email", nullable = false)
    private String userEmail;

    @ManyToOne
    @JoinColumn(name = "user_action_id", nullable = false)
    private UserAction userAction;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="user_action_per_user_result_email_message",
                  joinColumns={@JoinColumn(name="user_action_per_user_result_id", referencedColumnName="id")},
                  inverseJoinColumns={@JoinColumn(name="email_message_id", referencedColumnName="id")})
    @JsonIgnore
    private List<EmailMessage> emailMessages;

....getters/setter....
}

In MYSQL, the UserActionPerUserResult is one table and for email Messages, there is another table called as UserActionPerUserResultEmailMessage which has email messages associate with id of the table UserActionPerUserResult.

I have all the data stored in the MySQL table, however I am not able to query it. I need to write a query to fetch the list of emailMessages. I am trying the following, but it is throwing exception.

TypedQuery<UserActionPerUserResult> messagesQuery =     entityManager.createQuery(
                    "SELECT e from UserActionPerUserResult e JOIN UserActionPerUserResult.emailMessages e1 WHERE e.id = 1 and e.userAction = 1", UserActionPerUserResult.class);
List<UserActionPerUserResult> resultList = messagesQuery.getResultList();

Solution

  • Try writing the query like this:

    TypedQuery<UserActionPerUserResult> messagesQuery = entityManager.createQuery(
       "SELECT e from UserActionPerUserResult e JOIN FETCH e.emailMessages em WHERE e.id = 1 and e.userAction.id = 1", UserActionPerUserResult.class);
    List<UserActionPerUserResult> resultList = messagesQuery.getResultList();
    
    1. The join is using the root entity alias
    2. The userAction.id is used against userAction, when matching against a numerical value