Hibernate Native SQL

vembubalaji
1,462 views
undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Native Joins

  • If we would like to get data from both User and team based on the user table, we can use native joins. In this case, we are using the left outer join
A quick sample. Check out the JAVA class and SQL file
// {...}
logger.info(dash);
List<Object[]> user = session.createNativeQuery(""
+ "select u.name, t.team_id from user u left outer join team t on u.team_id=t.team_id where u.user_id=?")
.setParameter(1, 1).list();
user.stream().forEach(objects -> {
String name = (String) objects[0];
int teamId = (int) objects[1];
if (logger.isLoggable(Level.INFO)) {
System.out.println(String.format("Info : User[ %s, %d ]", name, teamId));
}
});
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  • Notice that the returned Object List are the columns requested.
  • To ensure eager loading of associated tables we can use addJoin and addEntity.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content