Hibernate Native SQL

vembubalaji
13.6K views

Open Source Your Knowledge, Become a Contributor

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

Create Content

Mapping Non-managed entities via result transformer

  • Until now, we have been mapping result sets to specific entities, what if we want to map the result set directly onto a JavaBean rather than an entity. For this, we can us the Result transformer
  • Here again, we have implicit transformer, explicit transformers.
  • For Implicit transformation, we would use the Transformers.aliasToBean method. Here the understanding is that the columns returned from the result set have the same naming convention with that of the mapped JavaBean. In this case, the CustomDTO’s attributes.
  • In case the columns returned from the result set names does not match the names on the mapped JavaBean attributes, and then we opt for the explicit transformation.
A quick sample. Check out the JAVA class and SQL file
// {...}
List<CustomDTO> custom = session.createNamedQuery("3tableJoin")
.setResultTransformer(Transformers.aliasToBean(CustomDTO.class)).setParameter(1, 1).list();
CustomDTO dto = custom.get(0);
System.out.println("Info: Domain Name from default Result Transformer " + dto.getDomainName());
System.out.println("Info: User Name from default Result Transformer " + dto.getUserName());
System.out.println(dash);
List<MoreCustomDTO> moreCustom = session.createNamedQuery("3tableJoin")
.setResultTransformer(new CustomResultTransformer()).setParameter(1, 2).list();
MoreCustomDTO dto1 = moreCustom.get(0);
System.out.println("Info: Domain Name from custom Result Transformer " + dto1.getdName());
System.out.println("Info: User Name from custom Result Transformer " + dto1.getName());
System.out.println(dash);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content