John Doe
John Doe

Reputation: 9764

Selecting from two tables in Hibernate

First of all I'm really sorry for asking such a basic thing, I know it can be irritating, but I want to know if I make a move in the wrong direction. Of course, I will get to the step-by-step reading of the documentation, but now all I want is to solve this problem.

I want to select values from two tables with many-to-one association (each category may suit 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 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; 
    } 

If I select values with an HQL query from Computers (I put them in a list) I can see ${computer.computerName}, but none of the ${computer.categories.categoryName} appear. I want each categoryName to be selected by the id. All the categories need to be displayed along with the names. The SQL query for this task wouldn't be to hard to write, but I want to use Hibernate and I don't understand it well at the moment. I thought all I need were mappings represented in the classes. Is my simple from Computers wrong for such a selection? I don't get any errors that would make understanding of what I do wrong a little easier. Any help would be appreciated.

Upvotes: 1

Views: 1910

Answers (1)

Ken Chan
Ken Chan

Reputation: 90517

Besides globally configuring the fetch plan of "Computers to Categories" as EAGER using @ManyToOne(fetch=FetchType.EAGER) in the mapping meta-data , you can also keep this relationship as lazy and use fetch join in HQL to eagerly fetch the Categories for a particular use case

  from Computers computer join fetch computer.categories

Then , the Categories of the returned Computers instance will be fully initialized .

Upvotes: 3

Related Questions