Search code examples
javasqlhibernateormcriteria

Logical OR between a Criterion and Criteria


I have the following model classes:

@Entity
@Table(name = "title")
public final class Title extends ModelData<Title>
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer titleID;

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "title")
    private Set<Book> books;
}

@Entity
@Table(name = "book")
public final class Book extends ModelData<Book>
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "bookID")
    private int bookID;

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinColumn(name = "titleID")
    private Title title;

    @Column(name = "edition")
    private Integer edition;

    @Column(name = "isbn")
    private String ISBN;
}

I want to write a Criteria query that is equivalent to the following SQL;

Select 
        t.title, b.edition 
    from 
        books b, title t
    where 
        b.titleID = t.titleID
    and 
        (b.edition=4 OR t.title LIKE '%Java%);

I tried the following:

Criteria c = session.createCriteria(Book.class);

Criteria titleCriteria = c.createCriteria("title");
titleCriteria.add(Restrictions.like("title", "%Java%");

Criterion edition = Restrictions.eq("edition", 4);

LogicalExpression orExp = Restrictions.or(edition, titleCriteria); //cannot do this

How do I achieve the above?

Thanks.


Solution

  • public class MyDTO {
      private String dtoTitle;
      private String dtoEdition;
      // + setters/getters
    }
    
    
    Criteria c = session.createCriteria(Book.class,"b");
    c.createAlias("title", "t");
    c.add(
        Restrictions.disjunction()
        .add( Restrictions.like("t.title", "%Java%") )
        .add( Restrictions.eq("b.edition", 4) )
    );
    c.setProjection( 
        Projections.projectionList()
        .add( Projections.property("t.title"), "dtoTitle" )
        .add( Projections.property("b.edition"), "dtoEdition" )
    );
    c.setResultTransformer(Transformers.aliasToBean(MyDTO.class));
    List<MyDTO> result = (List<MyDTO>)c.list();
    

    Something like this should work fine.

    On dao using many criterias you should consider using static imports.