下面是两个表,其中定义了表之间的映射。我想写一个JPA查询,我可以通过它查询基于userProfile的所有数据。我的查询如下
firstElement.Content = passData.Data;
base.OnNavigatedTo(args);
这样做并没有给出理想的结果。这是我的实体
@Repository("userEthnicityRepository")
public interface UserEthnicityRepository extends JpaRepository<UserEthnicity, Long> {
@Query("select e from UserEthnicity ue where ue.userProfile = ?1 ")
Set<UserEthnicity> findByUserProfile(UserProfile userProfile);
}
和
@Entity
public class UserProfile implements Serializable {
@Id
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Index(name = "ethnicity")
private Ethnicity ethnicity;
public Ethnicity getEthnicity() {
return ethnicity;
}
public void setEthnicity(Ethnicity ethnicity) {
this.ethnicity = ethnicity;
}
@OneToMany(mappedBy = "userProfile", fetch = FetchType.EAGER)
private Set<UserEthnicity> userEthnicity;
public Set<UserEthnicity> getUserEthnicity() {
return userEthnicity;
}
public void setUserEthnicity(Set<UserEthnicity> userEthnicity) {
for (UserEthnicity userEthnicityOne : userEthnicity) {
userEthnicityOne.setUserProfile(this);
}
this.userEthnicity = userEthnicity;
}
}
和种族有些枚举
@Entity
public class UserEthnicity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Index(name = "ethnicity")
private Ethnicity ethnicity;
@ManyToOne(fetch = FetchType.EAGER, targetEntity = UserProfile.class)
UserProfile userProfile;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Ethnicity getEthnicity() {
return ethnicity;
}
public void setEthnicity(Ethnicity ethnicity) {
this.ethnicity = ethnicity;
}
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}}
答案 0 :(得分:2)
你只需要
UserEthnicity findByUserProfileId(Long id)
或
UserEthnicity findByEthnicityId(Long id)
Spring JPA将在CAPs案例中拆分搜索字符串,因此find-By-UserProfile-Id
或
UserEthnicity findByEthnicityIdAndUserProfileId(Long ethnicityId, Long profileId)
有关查询方法的完整定义,请参阅here。