views:

226

answers:

4

Hi,

We have a DB table that is mapped into a hibernate entity. So far everything goes well...

However what we want is to only map enentitys that satisty a specific criteria, like ' distinct(fieldA,fieldB) '...

Is it possible to map with hibernate and hibernate annotations? How can we do it? With @Filter?

A: 
  1. You could create a view and then map that view to entity:

    create view my_data as
    select ... from ...
    
    
    @Entity(table="my_data")
    public class MyData { ... }
    
  2. One option is to map the table normally, then you could fetch your always entities through a query or a filter.

  3. You could also make a native SQL query and map the entity on the results:

    Query q = sess.createSQLQuery("SELECT DISTINCT fieldA, fieldB FROM some_table")
              .addEntity(MyEntity.class);
    List<MyEntity> cats = q.list();
    
  4. It might be also possible to add DISTINCT to this type of HQL query:

    select new Family(mother, mate, offspr)
    from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr
    

Methods 1, 3 and 4 will make a read-only mapping.

Could you be more specific about the criteria you are using? The view approach is more generic since you can't do everything with a hibernate query or filter.

Juha Syrjälä
the criteria we want to apply is a "distinct" over a couple of columns.This is a legacy application, which we would prefer not mess with, so an approach that is only on the hibernate side is best... :\
A: 

perhaps you could create a new Pojo that encapsulates the fields and the condition that they should statisy . And then then make that class a 'custom user defined type', such that Hibernate will have to use the mapping class that you provide, for mapping that 'type'..

rjk2008
A: 

In addition to the options mentioned by Juha, you can also create an object directly out of a SQL query using the NamedNativeQuery and SqlResultSetMapping annotations.

@Entity
@SqlResultSetMapping(name = "compositekey", entities = 
  @EntityResult(entityClass = MiniBar.class, 
    fields = { @FieldResult(name = "miniBar", column = "BAR_ID"), })
)
@NamedNativeQuery(name = "compositekey", 
   query = "select BAR_ID from BAR", resultSetMapping = "compositekey")
@Table(name = "BAR")
public class Bar {

Flavor the SQL query to your taste

Jherico
A: 

I would recommend that you use @Where annotation. This annotation can be used on the element Entity or target entity of a collection. You provide a clause attribute written in sql that will be applied to any select that hibernate performs on that entity. It is very easy to use and very readable.

Here is an example.

@Entity
//I am only interested in Donuts that have NOT been eaten
@Where(clause = "EATEN_YN = 'N'")
public class Donut {

  @Column(name = "FILLING")
  private String filling;

  @Column(name = "GLAZED")
  private boolean glazed = true;

  @Column(name = "EATEN_YN")
  private boolean eaten = false;

...

}
mR_fr0g