Search code examples
javajpadaomyeclipse

Wizard-generated JPA DAO method doesn't return an iterable List


I must be really stupid, but I'm at my wits' end with a JPA issue, using MyEclipse 7.5.

I am accessing a DB2 database (on an AS400) via JPA. I have reverse-engineered a simple table to provide a DAO with some precision "find" methods. So far so good.

If I run a SELECT statement over the table thus, I get 4 rows:

SELECT * FROM MyTable WHERE MyValue = '1234'

However, if I try to access these same 4 records via JPA, I get a list that's the right size (4), but which contains 4 objects which are all the same, all copies of the first object found:

List <MyTableObject> objects = dao.findByMyValue("1234");

It's almost as if the internal Query object that the DAO class creates can't iterate through the rows of data. I've tweaked the reveng.xml file myriad ways, and I've tinkered with the generated DAO, but I'm getting nowhere. Am I missing something really obvious here? I just want to get a list of objects in the same way that the conventional SELECT statement returns a resultset!

(This is MyEclipse 7.5, using Hibernate 3.2 and its associated JPA library).

UPDATE: here's the generated code that findByMyValue() passes over to (loggin / try-catch removed for clarity):

    @SuppressWarnings("unchecked")
public List<PolicyStatFile> findByProperty(String propertyName, final Object value)
{
    final String queryString = "select model from MyTableObject model where model." + propertyName + "= :propertyValue";
    Query query = getEntityManager().createQuery(queryString);
    query.setParameter("propertyValue", value);
    return query.getResultList();
}

FINAL UPDATE It was all about the model: see comments to this post. Essentially, the model generated from the reverse engineering file was invalid because I didn't have a truly unique key. Once I resolved this (spurred by comments here), all was well.


Solution

  • Method you've posted looks correct (although it seems rather pointless to generate this for all properties). Couple things to check:

    1. Is MyValue property you've mentioned mapped directly on your entity (e.g. to the column on the same table; no associations are involved)?
    2. Can you enable Hibernate SQL debug (set 'hibernate.show_sql' property to true in your configuration) and check what the generated query looks like?
    3. Are 4 objects returned actually the same (e.g. are '==' to each other) or are they copies of each other (e.g. have the same property values)?

    Can you post your mapping for the entity in question and generated SQL from #2 above?