简体   繁体   中英

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

I have a table with non-unique ID column. 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 )

Is there any way to override getter, to get ID s only of the rows, where START_DATE is 2013-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.

I'm not sure if you are using hibernate or JPA straight with which version, otherwise I might try to offer a code solution. For the most part though, one-to-many relationships do in fact need their own DAO method call in spring and hibernate.

Edit: Try something like this then for 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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