Choosing from two tables in Hibernate

First of all, I'm really sorry that I asked such a basic thing, I know it can be annoying, but I want to know if I am going in the wrong direction. Of course I'll get a step-by-step reading of the documentation, but now all I want is to solve this problem.

I want to select values ​​from two one-to-one tables (each category can correspond to different computers).

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


Here are the automatically generated POJO classes:

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

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

    public Computers() { 

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

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

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

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


public class Categories  implements { 

     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; 


If I select values ​​with an HQL query from Computers

(I put them in a list) I can see ${computer.computerName}

, but none of ${computer.categories.categoryName}

them appear. I want each categoryName

id to be selected. All categories should be displayed along with their names. The SQL query for this task won't be hard to write, but I want to use Hibernate and I don't understand that very well at the moment. I thought all I needed were the mappings provided in the classes. Is my simple from Computers

wrong for such a choice? I am not getting any errors that could better understand what I am doing wrong. Any help would be appreciated.


source to share

1 answer

Besides configuring the "Computers in Categories" fetch plan globally as EAGER using @ManyToOne(fetch=FetchType.EAGER)

mappings in the metadata, you can also keep this relationship lazy and use fetch join in HQL to look forward to the categories for a specific use case

  from Computers computer join fetch computer.categories


The instance categories of the returned computers will then be fully initialized.



All Articles