Asad Ali
Asad Ali

Reputation: 417

How to fetch data from Spring Repository using mapped attribute

Below are the two tables, where mapping between tables are defined. I want to write a JPA query through which i can retrieve all the data based on userProfile. my query is as follows

@Repository("userEthnicityRepository")
public interface UserEthnicityRepository extends JpaRepository<UserEthnicity, Long> {

    @Query("select e from UserEthnicity ue where ue.userProfile = ?1 ")
    Set<UserEthnicity> findByUserProfile(UserProfile userProfile);
}

doing so is not giving desired result. Here is my entities

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

and

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

and Ethnicity some enum

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

Upvotes: 0

Views: 154

Answers (1)

Essex Boy
Essex Boy

Reputation: 7950

You just need

UserEthnicity findByUserProfileId(Long id)

or

UserEthnicity findByEthnicityId(Long id)

Spring JPA will break up the search string on CAPs case, so find-By-UserProfile-Id

or

UserEthnicity findByEthnicityIdAndUserProfileId(Long ethnicityId, Long profileId)

See here for a full definition of query methods.

Upvotes: 2

Related Questions