如何使用映射属性从 Spring 存储库中获取数据

How to fetch data from Spring Repository using mapped attribute

下面是两个表,定义了表之间的映射关系。我想编写一个 JPA 查询,通过它我可以检索基于 userProfile 的所有数据。我的查询如下

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

你只需要

UserEthnicity findByUserProfileId(Long id)

UserEthnicity findByEthnicityId(Long id)

Spring JPA 将分解 CAP 大小写的搜索字符串,因此按 UserProfile-Id 查找

UserEthnicity findByEthnicityIdAndUserProfileId(Long ethnicityId, Long profileId)

有关查询方法的完整定义,请参阅 here