robinmag
robinmag

Reputation: 18100

hibernate - projection a component

I have a Address bean that have a LatLng component. Please tell me how can i project that component and transform it in to a new class.

Thank you.

Upvotes: 0

Views: 390

Answers (2)

Moesio
Moesio

Reputation: 3178

As an example, you can use criteria as follows:

Criteria criteria = session.createCriteria(Cat.class);
criteria.add(Restrictions.like(“description”, “Pap”)
  .addOrder(Order.asc(“description”);

Criteria subCriteria = criteria.createCriteria("kind", "kind");
subCriteria.add(Restrictions.eq("description", "persa"));

Criteria anotherSubCriteria = subCriteria.createCriteria("anAssociation","anAssociation");
anotherSubCriteria.add(Restrictions.eq("attribute", "anything"));

criteria.setResultTransformer(new AliasToBeanResultTransformer(Cat.class));

criteria.crateAlias(“kind.anAssociation”, “kind_anAssociation”);

criteria.setProjections(Projections.projectionList()
  .add(Projections.alias(Projections.property(“id”), “id”))
  .add(Projections.alias(Projections.property(“kind.id”, “kind.id”))
  .add(Projections.alias(Projections.property(“kind.anAssocation.attribute”, “kind.anAssociation.attribute”))

List cats = criteria.list();

But if you want save some code, you can use Seimos and code just

Filters filters = new Filters();
filters.add(new Filters(“description”, “Pap”)
  .add(new Filter(“description”))
  .add(new Filter("kind.description", "persa"))
  .add(new Filter("kind.anAssociation.attribute", "anything"));
List<Cat> cats = dao.find(filters);

So, consider use http://github.com/moesio/seimos

Upvotes: 0

Adeel Ansari
Adeel Ansari

Reputation: 39887

As far as I understand your concern, you want to create a separate class for LatLng, something like below.

public class LatLng {
}

And then want to include that in your entity Address bean, like below.

public class Address {
   private LatLng latLng;
}

Now, have a look at Custom value types in Hibernate and Mapping custom type in Hibernate.

Upvotes: 2

Related Questions