2012-03-09 7 views
1

まず最初に、このような基本的なことを聞​​いて申し訳ありませんが、刺激的かもしれませんが、間違った方向へ移動するかどうかを知りたいと思います。もちろん、私はドキュメンテーションの段階的な読書に行きますが、今私が望むのはこの問題を解決することだけです。Hibernate内の2つのテーブルから選択する

多対1の関連付けを持つ2つのテーブルから値を選択する必要があります(各カテゴリは異なるコンピュータに適しています)。私は${computer.computerName}を見ることができます(私はリストに入れて)HQLクエリーfrom Computersで値を選択しますが、${computer.categories.categoryName}のいずれも表示されない場合

@Table(name="computers", catalog="dbname") 
public class Computers implements java.io.Serializable { 

    private Integer computerId; 
    private Categories categories; 
    private String computerName; 

    public Computers() { 
    } 

    public Computers(Categories categories, String computerName) { 
     this.categories = categories; 
     this.computerName = computerName; 
    } 

    @Id 
    @GeneratedValue(strategy=IDENTITY) 
    @Column(name="computerId", unique=true, nullable=false) 
    public Integer getComputerId() { 
     return this.computerId; 
    } 

    public void setComputerId(Integer computerId) { 
     this.computerId = computerId; 
    } 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name="categoryId", nullable=false) 
    public Categories getCategories() { 
     return this.categories; 
    } 

    public void setCategories(Categories categories) { 
     this.categories = categories; 
    } 

    @Column(name="computerName", nullable=false) 
    public String getComputerName() { 
     return this.computerName; 
    } 

    public void setComputerName(String computerName) { 
     this.computerName = computerName; 
    } 

} 



@Entity 
@Table(name="categories" 
    ,catalog="dbname" 
) 
public class Categories implements java.io.Serializable { 


    private Integer categoryId; 
    private String categoryName; 
    private Set<Computers> computerss = new HashSet<Computers>(0); 
    private Set<Customers> customerss = new HashSet<Customers>(0); 

    public Categories() { 
    } 

    public Categories(String categoryName) { 
     this.categoryName = categoryName; 
    } 
    public Categories(String categoryName, Set<Computers> computerss, Set<Customers> customerss) { 
     this.categoryName = categoryName; 
     this.computerss = computerss; 
     this.customerss = customerss; 
    } 

    @Id @GeneratedValue(strategy=IDENTITY)   
    @Column(name="categoryId", unique=true, nullable=false) 
    public Integer getCategoryId() { 
     return this.categoryId; 
    } 

    public void setCategoryId(Integer categoryId) { 
     this.categoryId = categoryId; 
    } 

    @Column(name="categoryName", nullable=false) 
    public String getCategoryName() { 
     return this.categoryName; 
    } 

    public void setCategoryName(String categoryName) { 
     this.categoryName = categoryName; 
    } 
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categories") 
    public Set<Computers> getComputerss() { 
     return this.computerss; 
    } 

    public void setComputerss(Set<Computers> computerss) { 
     this.computerss = computerss; 
    } 
    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="categories") 
    public Set<Customers> getCustomerss() { 
     return this.customerss; 
    } 

    public void setCustomerses(Set<Customers> customerss) { 
     this.customerss = customerss; 
    } 

:ここ

+------------+    +------------+ 
|computers |    |categories | 
+------------+    +------------+ 
|categoryId |------------>|categoryId | 
|computerId |    |categoryName| 
|computerName|    +------------+ 
+------------+ 

が自動的に生成されたPOJOクラスです。 idによってそれぞれcategoryNameを選択します。すべてのカテゴリを名前とともに表示する必要があります。このタスクのSQLクエリーは、書くのが難しくありませんが、私はHibernateを使用したいと思います。私は現時点でそれをよく理解していません。私は必要なのは、クラスで表現されたマッピングだと思った。そのような選択のために私の単純なfrom Computersが間違っていますか?私は何が間違っているのかを少しでも簡単に理解できるような誤りはありません。どんな助けもありがとう。

+1

'@ ManyToOne'アノテーションのフェッチタイプを' FetchType.LAZY'から 'FetchType.EAGER'に変更するとどうなりますか?これはあなたの質問の根源ではないかもしれませんが、それは私が似たような状況で終わったものです。 –

+0

返信いただきありがとうございます、私はこのオプションを試すのが待つことができません。あまりにも悪い私は日曜日にそれを試してみるチャンスがあります。しかし、私は必ず 'FetchType.EAGER'がそのトリックを行うことができるかどうかを確かめます。あなたのHQLクエリはこのように簡単でしたか? –

+0

ManyToOneの代わりにOneToManyが使用されます。 –

答えて

3

グローバルマッピングメタデータで@ManyToOne(fetch=FetchType.EAGER)を使用してEAGERとして「カテゴリーへのコンピュータ」の計画をフェッチ、あなたも怠惰としてこの関係を保つことができると熱心のためのカテゴリーをフェッチするHQLでに参加フェッチを使用する設定に加えて特定のユースケース

from Computers computer join fetch computer.categories 

次に、返されるComputersインスタンスのカテゴリが完全に初期化されます。

+0

ありがとう、私はそれを試みます。私はそれが役に立つかもしれないと思う。 –

関連する問題