标准与标准之间的逻辑或

发布于 2025-01-01 03:45:30 字数 1497 浏览 0 评论 0原文

我有以下模型类:

@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;
}

我想编写一个相当于以下 SQL 的 Criteria 查询;

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

我尝试了以下操作:

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

如何实现上述目标?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一向肩并 2025-01-08 03:45:30
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();

像这样的东西应该可以正常工作。

在使用许多标准的 dao 上,您应该考虑使用静态导入。

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.

晒暮凉 2025-01-08 03:45:30

另一个想法

将您的标准转换为公式字段并按正常标准进行评估

将公式字段添加到映射文件,或将注释添加到您的类

<property name="titlename" type="string"
formula="(Select title.title from title 
    where title.titleID= titleID)"/>

Criteria c = session.createCriteria(Book.class)
Criteria titleCriteria = c.createCriteria("title");
titleCriteria.add(Restrictions.like("titlename", "%Java%");
Criterion edition = Restrictions.eq("edition", 4);
LogicalExpression orExp = Restrictions.or(edition, titleCriteria); //CAN< do this!!!

Another idea

Convert yout criteria to formula field and evaluate as normal criterios

Add formula field to mapping file, or annotations to your classes

<property name="titlename" type="string"
formula="(Select title.title from title 
    where title.titleID= titleID)"/>

then

Criteria c = session.createCriteria(Book.class)
Criteria titleCriteria = c.createCriteria("title");
titleCriteria.add(Restrictions.like("titlename", "%Java%");
Criterion edition = Restrictions.eq("edition", 4);
LogicalExpression orExp = Restrictions.or(edition, titleCriteria); //CAN< do this!!!
我只土不豪 2025-01-08 03:45:30

我想你想要这个。我没有测试过,所以可能会有一些小错误,但基本想法是正确的。

Criteria c = session.createCriteria(Book.class); 
Criterion titleCriterion = Restrictions.like("title.title", "%Java%"); 
Criterion edition = Restrictions.eq("edition", 4); 
c.add( Restrictions.or( edition, titleCriterion ));

I think you want this. I haven't tested it, so there may be some minor errors, but the basic idea is correct.

Criteria c = session.createCriteria(Book.class); 
Criterion titleCriterion = Restrictions.like("title.title", "%Java%"); 
Criterion edition = Restrictions.eq("edition", 4); 
c.add( Restrictions.or( edition, titleCriterion ));
堇年纸鸢 2025-01-08 03:45:30

对于超过 2 个 OR 条件,使用它更具可读性:

c.add(
   Restrictions.disjunction()
            .add(Restrictions.eq(...))
            .add(Restrictions.eq(...))
            .add(Restrictions.eq(...))
   )

For more than 2 OR conditions it's more readable to use:

c.add(
   Restrictions.disjunction()
            .add(Restrictions.eq(...))
            .add(Restrictions.eq(...))
            .add(Restrictions.eq(...))
   )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文