Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate help with a criteria join query

Tags:

hibernate

I have four tables, USER, CONTACT, CONACT_TYPE, and USER_CONTACT

USER_CONTACT stores all of the contacts that a user has tables populated with dummy data are as follows

USER TABLE

USER_ID(int)| FIRST_NAME(varchar(2) | LAST_NAME(varchar(2) |
------------------------------------------------------------
|    1      |       TEST            |       USER           |
------------------------------------------------------------

USER_CONTACT

USER_CONTACT_ID(int) | USER_ID(int) | CONTACT_ID(int) |
-------------------------------------------------------
|     1              |       1      |         1       |
|     2              |       1      |         2       |
|     3              |       1      |         3       | 
-------------------------------------------------------

CONTACT

CONTACT_ID(int) |  CONTACT_TYPE_ID(int) | CONTACT(varchar(2)|
-------------------------------------------------------------
|      1        |         2             |  (555) 555-5555   |
|      2        |         2             |  (555) 593-3938   |
|      3        |         1             |  [email protected]  | 
-------------------------------------------------------------

CONTACT_TYPE

CONTACT_TYPE_ID(int) | CONTACT_TYPE | 
-------------------------------------
|       1            |   EMAIL      | 
|       2            |   PHONE      | 
-------------------------------------

What I am trying to do is create a query that will return a List that has contains only the PHONE CONACT_TYPE here is my hibernate function so far

public List<UserContact> getUserContactByType(Integer userId, String contactType) {
      Session session = getSessionFactory().openSession();

      try {
           Criteria criteria = session.createCriteria(UserContact.class, "USER_CONACT");

           criteria.add(Restrictions.eq("USER_CONACT.userId, userId");

           criteria.add(Restrictions.eq("USER_CONTACT.contact.contactType.contactType", contactType);

           return (List<UserContact>)criteria.list();

      }

}

Each table is mapped to a model class. The important class information is as follows.

Contact.java

Contains a @ManyToOne relationship to ContactType.java class

@ManyToOne(optional = false, fetch = FetchType.EAGER)
private ContactType contactType;

UserContact.java

Contains a @ManyToOne relationship to Contact.java class and a @ManyToOne on the User.java class

@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Contact contact;

@ManyToOne(optional = false, fetch = FetchType.LAZY)
private User user;

All of the classes have the standard getters and setters for all of the column attributes for the tables above also.

I keep getting an error stating that it cannot resolve the property contact.contactType of my UserContact class. Anyone know how to properly execute something like this in hibernate?

like image 420
medium Avatar asked Mar 17 '11 12:03

medium


People also ask

How do you use join in Hibernate query?

We can apply the Joins in Hibernate by using the HQL query or native SQL query. To make a join between the two tables, the two tables must be in a logical relationship. We can achieve the relationship between two tables by applying the parent table's primary key as a child table's foreign key.

How can add order by criteria in Hibernate?

Add an ordering to the result set. Join an association, assigning an alias to the joined association. Join an association using the specified join-type, assigning an alias to the joined association. Create a new Criteria, "rooted" at the associated entity.

What is Criteria query in Hibernate?

The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.


1 Answers

I figured it out, I was unaware of a createAlias function. Solution is below if anyone is wondering.

public List<UserContact> getUserContactByType(Integer userId, String contactType) {
      Session session = getSessionFactory().openSession();

      try {
           Criteria criteria = session.createCriteria(UserContact.class, "USER_CONACT");

           criteria.add(Restrictions.eq("USER_CONACT.userId, userId");

           criteria.createAlias("USER_CONACT.contact", "c");

           criteria.add(Restrictions.eq("c.contactType.contactType", contactType);

           return (List<UserContact>)criteria.list();

      }

}
like image 106
medium Avatar answered Oct 20 '22 19:10

medium