我有四张桌子
具有以下字段的PublishedId(FK)
在我看来,我正在接受
SearchTemplate _tempTemplate =新的SearchTemplate();判据= null;
若(rcValue.getOperator().equalsIgnoreCase("like")) {rcValue.getDisplayName= Restrictions.like(rcValue.getDisplayName(),rcValue.getOperandValue());}Restrictions.like{rcValue.getDisplayName= Restrictions.eq(rcValue.getDisplayName(),rcValue.getOperandValue());}如果(rcValue.getOperator().equalsIgnoreCase("not")) {rcValue.getDisplayName= Restrictions.ne(rcValue.getDisplayName(),rcValue.getOperandValue());} _tempTemplate.addCriterion(criterion);
当我搜索名字时,我得到了正确的结果,但是当我搜索我的作者名字时,我需要在上面的代码中更改什么呢?
2.当我搜索发布日期时,我需要更改什么,因为它是一个多表搜索,如果它的SQL我可以轻松地编写它,但是现在使用Hibernate,并使用'SearchTemplate‘的实现。
/*The SearchTemplate is used to pass search parameters to the DAO layer.
*
* By having all the search parameters in one place (the SearchTemplate),
* we prevents combinatory explosion of 'find' method signatures in the DAO/Service layer.
* This leads to more stable DAO/Service interfaces as you can add search parameters to the
* SearchTemplate without adding new methods to your interfaces.
*
* A SearchTemplate helps you drive your search in the following areas:
*/有人能帮忙吗?
发布于 2011-11-23 12:43:26
第一个问题:
您需要进行连接(假设根实体是title):
criteria.createAlias("author", "author");
criteria.add(Restrictions.eq("author.name", authorName);第二个问题:
同样的答案:你需要加入:
criteria.createAlias("publisher", "publisher");
criteria.createAlias("publisher.region", "region");
criteria.add(Restrictions.eq("region.publishedDate", someDate));这个SearchTemplate真是个坏主意,海事组织。而不是有明确定义的方法和明确的契约,如:
List<Title> findTitlesWithName(String name)
List<Title> findTitlesPublishedBy(Long publisherId)
Title findLatestTitleFromAuthor(String authorId)你会有一个方法
List<Title> findTitles(SearchTemplate criteria)你必须在标准中写些什么?标准的类型是什么?如何处理这些标准呢?将采用哪种订购方式?哪些相关实体将被获取或延迟加载?
查找方法的“爆炸”是一件好事,因为每个find方法都有自己的契约、优化和目标。每个单元也可以进行单元测试。这就是刀的目标。如果没有这些方法,您最好有一个唯一的方法,例如
List find(String hql, Object[] arguments)但我看不出这道刀的目的。
发布于 2011-11-23 12:43:31
Hibernate是一种ORM技术:对象/关系映射。我在您的问题中看到了关系表,但没有提到对象。
我在你的模型中至少看到了五个物体,也许更多:
想想这些对象之间的一对多和多对多的关系。例如,一个作者可能写几个标题;一个标题可能有几个作者。听起来像是一对一对多的关系。
因此,这些对象看起来可能如下:
public class Author {
private List<Title> titles; // parent
}
public class Title {
private List<Author> authors; // child
}将它们映射为Hibernate中的1:m关系,它将有助于为您生成SQL。你不会像你想象的那么需要HQL。
另一种实现这种多对多关系的方法是将其“具体化”成一个单独的对象。你的设计选择,取决于哪个更好。如果您的属性是最好分开的,那么这是个好主意。例如,学生和课程(学生参加许多课程;一门课程由许多学生参加)。将其还原为具有品位的ReportCard或其他类型的对象可能是有意义的。
https://stackoverflow.com/questions/8242180
复制相似问题