我是Hibernate的新手,非常需要大家的帮助......
我有以下类: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....
}
在MYSQL中,UserActionPerUserResult是一张表,对于电子邮件消息,还有另一张名为UserActionPerUserResultEmailMessage的表,该表与UserActionPerUserResult表中的id相关联。
我已经将所有数据存储在MySQL表中,但无法查询它。我需要编写一个查询来获取emailMessages列表。我尝试了以下方法,但出现异常。
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();