2012-02-16 8 views
0

私は4つのテーブルを持っています。JPAとテーブルを結合するには?

郵便番号

ジップコード(PK)

townCode

タウン

townCode(PK)

townName

cityCode

cityCode(PK)

cityName

prefectureCode

prefectureCode(PK)

prefectureName

私だけ郵便番号で検索すべてのパラメータを取得したいです。 今、私はこのコードを書いた。

@Entity 
public class ZipCode extends GenericModel { 
@Id 
@Column(length = 7, nullable = false) 
public String zipCode; 
@Column(length = 8, nullable = false) 
public String townCode; 
@ManyToMany(targetEntity=Town.class) 
@JoinTable(name="town") 
@JoinColumn(referencedColumnName="townCode",insertable=true,table="town",name="townCode") 
public Set<Town> towns; 

@Entity 
public class Town extends GenericModel { 
@Id 
@Column(length = 8, nullable = false) 
public String townCode; 
@Column(length = 255, nullable = false) 
public String townName; 
@Column(length = 5, nullable = false) 
public String cityCode; 
@ManyToMany(mappedBy="townCode") 
public Set<ZipCode> zipCode; 

と、私はこれを実行し、

リストジップコード= ZipCode.find( "郵便番号=?"、郵便番号).fetch(); TableStatusがある

郵便番号 郵便番号(PK)|タウンコード 1111111 | 123

townCode(PK)|タウン名| cityCode 123 |東京| 12345

私はすべてのパラメータを取得したいのですが、応答は唯一の郵便番号のステータスを持っている...すべてのパラメータを取得する方法、教えてください

してください。

助けてください!

+0

CityとPrefectureのエンティティクラスを定義しましたか?すべてのパラメータによってどういう意味ですか? – Rocky

+0

申し訳ありませんが、市と川はまだ定義されていません。 – user1212872

+0

すべてのパラメータ 郵便番号 townCode townName cityCode cityName prefectureCode prefectureName これらのパラメータです。 – user1212872

答えて

0

zipCodeとTownの間のマッピングを定義したのと同じように、TownとCityの間、およびCityとPrefectureの間のマッピングを提供する必要があります。

これを済ませたら、郵便番号のすべての町を取得することができます。町から町を得ることができ、市からあなたが県を得ることができるでしょう さらに探してあなたは行くことをお勧めします一つ

public class Zipcode { 
// all the other attributes 
// Provide the mappings via annotation 
public Town town ; 

} 

public class Town { 
// all the other attributes 
// provide the mappings via annotation 
public City city ; 
} 

public class City { 
// all the other attributes 
// Provide the mappings via annotation 
public Prefecture prefecture ; 

} 

public class Prefecture { 
// all the other attributes 

} 

そして最後に

List zipcodes = ZipCode.find("zipcode = ? ", zipcode).fetch(); 
foreach(Zipcode zip : zipcodes) { 
    zip.getTown() // to get the town 
    zip.getTown().getCity() // to get the city 
    zip.getTown().getCity().getPrefecture() // to get the Prefecture 
} 

に多くにそれを変更することを検討可能性があるので、あなたのテーブルで、私は、郵便番号と表の間に多くの関係に多くが表示されませんあなたが私たちに行かないならば、遅延読み込み用すべての詳細は毎回。

+0

ありがとうMr.Rocky !! 注釈を使用する方法を理解していません... 注釈を使用する必要がありますか教えてください。 – user1212872

+0

エンティティのマッピングにアノテーションを使用する方法については、こちらを参照してください。http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-association Many to Oneマッピング – Rocky

+0

Mr.Rockyありがとう!! JPAは私にとってはとても難しいです...この場合(ZipCodeとTownの間)、所有者ですか? – user1212872

関連する問題