简体   繁体   English

是否有可能覆盖getter?

[英]Is it possible to override getter?

Here is the problem I'm struggling with (pretty new to Java & Hibernate, so the answer might be obvious, but I really can't figure it out): 这是我正在努力解决的问题(对于Java和Hibernate来说是相当新的问题,因此答案可能很明显,但我真的无法弄清楚):

I have a table with non-unique ID column. 我有一个带有非唯一ID列的表。 Pairs of rows of the table look like this: 成对的表行看起来像这样:

ID|NAME|CODE|START_DATE|END_DATE
--------------------------------
1 |name|1234|1900-01-02|2013-01-01
--------------------------------
1 |name|1234|2013-01-01|4999-01-01
--------------------------------
2 |name1|123|1900-01-02|2013-01-01
--------------------------------
2 |name1|123|2013-01-01|4999-01-01
--------------------------------

The only thing that is different between two rows in each pair of rows is a time period ( START_DATE and END_DATE ) 每对行中的两行之间唯一不同的是时间段( START_DATEEND_DATE

Is there any way to override getter, to get ID s only of the rows, where START_DATE is 2013-01-01 有什么方法可以覆盖getter,仅获取行的ID ,其中START_DATE2013-01-01

In hibernate, most would suggest to make a domain access object (DAO) to perform the specialized query and throw a service layer on top of that that your controller would call. 在休眠状态下,大多数人建议创建一个域访问对象(DAO)以执行专门的查询,并在控制器将调用的服务层之上放置一个服务层。

I'm not sure if you are using hibernate or JPA straight with which version, otherwise I might try to offer a code solution. 我不确定您是直接在哪个版本上使用hibernate或JPA,否则我可能会尝试提供代码解决方案。 For the most part though, one-to-many relationships do in fact need their own DAO method call in spring and hibernate. 尽管在大多数情况下,一对多关系实际上确实需要在Spring和Hibernate中调用它们自己的DAO方法。

Edit: Try something like this then for JPA 2 编辑:尝试这样,然后为JPA 2

public List<Row> findByStartDate(Date date) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Row> criteria = cb.createQuery(Row.class);
    Root<Row> r = criteria.from(Row.class);
    Predicate dateEquals = cb.equal(r.get("startDate"), date);
    criteria.select(r).where(dateEquals);
    return entityManager.createQuery(criteria).getResultList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM