Skip to content

Hibernate enhanced AliasToBeanResultTransformer that supports projecting one-to-one and many-to-one associations

Notifications You must be signed in to change notification settings

samiandoni/AliasToBeanNestedResultTransformer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 

Repository files navigation

AliasToBeanNestedResultTransformer

You can use this class as criteria trnsformer in Hibernate to bind associated beans to the target bean.

Usage Example:

class Person {
  private Long id;
  private String name;
  private Car car;
  // getters and setters
}

class Car {
  private Long id;
  private String color;
  // getters and setters
}

List<Person> getPeople() {
  ProjectionList projections = Projections.projectionList()
	.add(Projections.id().as("id"))
	.add(Projections.property("name").as("name"))
	.add(Projections.property("c.id").as("car.id"))
	.add(Projections.property("c.color").as("car.color"));

  Criteria criteria = getCurrentSession().createCriteria(Person.class)
    .createAlias("car", "c")
    .setProjection(projections)
    .setResultTransformer(new AliasToBeanNestedResultTransformer(Person.class));

  return (List<Person>) criteria.list();
}

// each car of Person will be populated

This is working for one-to-one and many-to-one associations (or beans).

We can develop it to include collections and multi-level nested associations.

About

Hibernate enhanced AliasToBeanResultTransformer that supports projecting one-to-one and many-to-one associations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published