0

下面是两个表,其中定义了表之间的映射。我想写一个JPA查询,通过它我可以检索所有基于userProfile的数据。我的查询如下如何使用映射属性从Spring存储库中获取数据

@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; 
}} 

和种族一些枚举

public enum Ethnicity { 
    Acadian, Afghans, Albanians, Arabs, Armenians, Assyrians, Azerbaijanis, Balochis, Bamars, Basques, Bengali, Berbers, Bihari, Bosniaks, Brahui, Bulgarians, Cajun, Catalans, Ceylonese, Cham, Chechens, Cherokees, Chicanos, Chitpavan, Chuvash, Circassians, Congolese, Copts, Croats, Czechs, Danes, Dougla, Dutch, English, Estonians, Eritreans, Faroese, Finns, Flemings, French, Frisians, Gagauz, Galicians, Gerashis, Germans, Greeks, Georgians, Gujarati, Hakka, HanChinese, Hapa, Hindustani, Hmong, Hongkonger, Hui, Hungarians, Icelanders, Igbo, Inuits, Indians, Indochinese, Irish, Istrian, Italians, Japanese, Jassic, Javanese, Jews, Macedonians, Malayali, Kannada, Kazakhs, Khmer, Koreans, Kosovans, Kurds, Kyrgyz, Maghrebis, Malays, Marathi, Moluccan, Norwegians, Laz, Lebanese, Manchu, Marabou, Moldovans, Mongols, Muscogee, Navajo, NEWCaledoniaKanaks, Nepali, Pashtuns, Occitans, Okinawan, Oromo, Persians, Poles, Portuguese, Punjabi, Quebecois, Romanians, Romani, Russians, Saharauis, Scottish, Serbs, Sindhis, Sinhalese, Slovaks, Slovenes, Spaniards, Sundanese, Swedes, Tamils, Tartars, Tejanos, Telugu, Thais, Tibetan, Tuaregs, Turks, Turkmens, Ukrainians, Uyghur, Vietnamese, VolgaTatars, Walloons, Welsh, Yoruba, Zhuang, DoesntMatter; 
    public static Ethnicity getEthnicity(String value) { 
     try { 
      value = URLDecoder.decode(value, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     if (value.equalsIgnoreCase("Doesn’t Matter") || value.contains("Matter")) { 
      return Ethnicity.DoesntMatter; 
     } else { 
      return Ethnicity.valueOf(value); 
     } 
    } 

回答

2

你只需要

UserEthnicity findByUserProfileId(Long id) 

UserEthnicity findByEthnicityId(Long id) 

春天JPA会分手瓶盖情况下,搜索字符串,所以找到-BY-用户配置-ID

UserEthnicity findByEthnicityIdAndUserProfileId(Long ethnicityId, Long profileId) 

为查询方法的完整定义见here

+0

明白了,thanx @Essex –

+0

如何使用上述给定的解决方案获取多个映射实体。我在单个表中有多个实体。可能吗 ? – keshav

+0

@keshav看看更新后的答案 –

相关问题