Hibernation request to join criteria

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

USER_CONTACT stores all contacts that the user has tables filled with dummy data, the following

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 | test@oracle.com | ------------------------------------------------------------- 

CONTACT_TYPE

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

What I'm trying to do is create a query that will return a list containing only CONACT_TYPE PHONE, here is my hibernation 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. Important class information is as follows.

Contact.java

Contains @ManyToOne relation to ContactType.java class

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

UserContact.java

Contains @ManyToOne relation to the Contact.java class and @ManyToOne in the User.java class

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

All classes have standard getters and seters for all column attributes for the tables above.

I keep getting an error message indicating that it cannot resolve the contact.contactType property of my UserContact class. Does anyone know how to properly do something like this in sleep mode?

+6
hibernate
source share
2 answers

I realized this, I did not know about the createAlias โ€‹โ€‹function. The solution is below if someone 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(); } } 
+7
source share

Now you can use the use of Spring -Data-JPA with QueryDSL support!

0
source share

All Articles