0

以下は2つのテーブルで、テーブル間のマッピングが定義されています。 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

のCAPのケースに検索文字列を分割するので、見つけバイのUserProfile-IDになる

または

UserEthnicity findByEthnicityIdAndUserProfileId(Long ethnicityId, Long profileId) 

は、クエリメソッドの完全な定義については、hereを参照してください。

+0

gotax @Essex –

+0

上記の解を使用して複数のマップされたエンティティを取得する方法私は単一のテーブルに複数のエンティティを持っています。出来ますか ? – keshav

+0

@keshav更新された回答をご覧ください –

関連する問題