从Hibernate中的两个表中选择

时间:2012-03-09 00:14:36

标签: hibernate jpa

首先,我真的很抱歉问这么基本的事情,我知道这可能很烦人,但我想知道我是否朝错误的方向前进。当然,我将逐步阅读文档,但现在我想要的只是解决这个问题。

我想从两个具有多对一关联的表中选择值(每个类别可能适合不同的计算机)。

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

以下是自动生成的POJO类:

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

如果我选择带有HQL查询from Computers的值(我将它们放在列表中),我可以看到${computer.computerName},但${computer.categories.categoryName}都没有出现。我想通过id选择每个categoryName。所有类别都需要与名称一起显示。这个任务的SQL查询不会很难编写,但我想使用Hibernate,目前我还不太了解它。我认为我需要的只是在课堂上表示的映射。这样的选择是我的简单from Computers错了吗?我没有任何错误会让我更容易理解我的错误。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

除了在映射元数据中使用@ManyToOne(fetch=FetchType.EAGER)全局配置“计算机到类别”的获取计划作为EAGER之外,您还可以将此关系保持为惰性并使用获取连接 HQL急切地获取特定用例的类别

  from Computers computer join fetch computer.categories

然后,将完全初始化返回的Computers实例的类别。