0

Suppose I have UserDetails table with feilds:

UserId, RoleId, UserName, Password, Name, Mobile

and UserRole whit:

RoleId, RoleName, UserPage

I want to get certain information by joining two(or multiple tables) tables. I have query as:

SELECT ud.UserId,
    ud.RoleId,
    ud.UserName,
    ud.Password,
    ud.Name,
    ud.Mobile,
    ud.DateOfBirth,
    ur.RoleName,
    ur.UserPage    
FROM UserRole ur join UserDetails ud on ur.RoleId =ud.RoleId and UserName='user1';

With JdbcTemplate in spring I am able to get but With hibernate how to get returned values?

2 Answers 2

1

I think your problem can be solved by using criteria in hibernate or use native SQL query in hibernate to fetch all the data from database.

Sign up to request clarification or add additional context in comments.

2 Comments

can u give an example?
String sql = "SELECT * FROM TESTEMPLOYEE"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(TestEmployee.class); List results = query.list(); But its is not recommended to use native sql in hibernate. instead you can use HQL . and Hibernate is ORM tool. its help in mapping java object to database and viceversa.. so you need to create pojo and map them properly
1

Both the ways you can use.

This is sql query:

String userName = 'user1'

createSQLQuery("SELECT ud.UserId,
    ud.RoleId,
    ud.UserName,
    ud.Password,
    ud.Name,
    ud.Mobile,
    ud.DateOfBirth,
    ur.RoleName,
    ur.UserPage    
FROM UserRole ur join UserDetails ud on ur.RoleId =ud.RoleId and UserName="+userName)

This is hql query:

FROM UserRole ur join fetch UserDetails ud  where ud.UserName='user1';

use pojo class names as table names and variables in pojo classes as column names

3 Comments

that means i need to create pojo classes for all tables(which i think resource consuming). I can create pojo of returning value pojo but creating all is not good. Any other solution?
Without creating pojo classes,how hibernate will works?
Actually I thought like jdbctemplate we can marshal it, so no need of pojo!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.