标准与标准之间的逻辑或
我有以下模型类:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像这样的东西应该可以正常工作。
在使用许多标准的 dao 上,您应该考虑使用静态导入。
Something like this should work fine.
On dao using many criterias you should consider using static imports.
另一个想法
将您的标准转换为公式字段并按正常标准进行评估
将公式字段添加到映射文件,或将注释添加到您的类
中
Another idea
Convert yout criteria to formula field and evaluate as normal criterios
Add formula field to mapping file, or annotations to your classes
then
我想你想要这个。我没有测试过,所以可能会有一些小错误,但基本想法是正确的。
I think you want this. I haven't tested it, so there may be some minor errors, but the basic idea is correct.
对于超过 2 个 OR 条件,使用它更具可读性:
For more than 2 OR conditions it's more readable to use: