Back
Close

Hibernate Native SQL

vembubalaji
1,444 views

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.
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants