English 中文(简体)
自愿多功能处理
原标题:Hibernate multi-table query handling
  • 时间:2011-11-23 12:26:38
  •  标签:
  • hibernate

我有4个表格

  1. Titles having the following fields, id (PK), Name, AuthorId(FK) & PublishedId(FK)
  2. Author having id(FK), Author Name & Contact Details
  3. Publisher having, id(PK), Publisher Name & RegionId(FK)
  4. Region having, id(PK), Region Name, Published Date & Published Cost.

现在我认为,我接受。

  1. Author Name, and my code looks like

    SearchTemplate _tempTemplate = new SearchTemplate(); Criterion criterion = null;

        if (rcValue.getOperator().equalsIgnoreCase("like")) {
                        criterion = Restrictions.like(rcValue.getDisplayName(), rcValue.getOperandValue());
                    } else if (rcValue.getOperator().equalsIgnoreCase("is")) {
                        criterion = Restrictions.eq(rcValue.getDisplayName(), rcValue.getOperandValue());
                    } else if (rcValue.getOperator().equalsIgnoreCase("not")) {
                        criterion = Restrictions.ne(rcValue.getDisplayName(), rcValue.getOperandValue());
                    }
    
    _tempTemplate.addCriterion(criterion);
    

When I search for Name, I get the right results, but when I search for my Author Name, what do I need to change in my above code?

2.When I search of Published Date, what do I need to change, because its a multi-table search, if its SQL I can easily write it, but using Hibernate now and with an implementation of 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:
*/

谁能帮助?

最佳回答

第一问题:

你们需要加入(假定实体是所有权):

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));

海事组织认为,这一搜索引擎确实是坏的。 a. 没有明确的合同,而不是有明确的界定方法:

List<Title> findTitlesWithName(String name)
List<Title> findTitlesPublishedBy(Long publisherId)
Title findLatestTitleFromAuthor(String authorId)

页: 1

List<Title> findTitles(SearchTemplate criteria)

你们必须采用什么标准? 标准类型如何? 这种方法与这些标准有什么关系? 将适用哪种命令? 哪些相关实体将重新安排或装满负荷?

寻找方法的“探索”是一件好事,因为每种方法都有自己的合同、优化和目标。 还可以进行单位测试。 这符合农业部的目标。 如果没有这些方法,你也可能有独特的方法,例如,

List find(String hql, Object[] arguments)

但我看不出这样一个小岛的目标。

问题回答

Hibernate is an ORM technology: Object/relationalmap. 我在您的提问中看到关系表,但没有提到物体。

我在你的模型中至少看到5个物体:

  1. Title
  2. Author
  3. Publisher
  4. Region
  5. ContactDetails

思考这些物体之间的单向和多对手关系。 例如,作者可能写好几本书;所有权可能有几个作者。 象与我有着相互的多对手关系。

因此,目标可能照此办理:

public class Author {
    private List<Title> titles;  // parent
}

public class Title {
    private List<Author> authors; // child
}

将那些在Hibernate的1:m关系划为1:m关系,这有利于为你们创造食堂。 你们几乎像你认为的那样,赢得了“未来”的需求。

另一种方式是“重新”这种关系成为单独的目标。 你们的设计选择取决于哪些工作做得更好。 如果您的属性最好分开,那就是一个好的想法。 例如学生和课程(学生学习许多课程;许多学生学习课程)。 把这重新纳入“格拉德”报告或某种其他物体可能具有意义。





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签