views:

301

answers:

2

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.

+1  A: 

I suspect you haven't overridden hashCode() and equals() in your JPA entity (e.g. MyTableObject). So Hibernate can't distinguish the returning rows. That a look here.

R. Kettelerij
Ben Poole
This is incorrect. hashCode() / equals() have ABSOLUTELY no bearing on query results.
ChssPly76
Whilst in this instance equals() and hashcode() were NOT the answer, R. Kettelerij's response got me thinking about the composite keys being built in the reveng file, and I solved my problem -- the default <primary-key> grouping was not valid (not a genuinely unique key), so Hibernate couldn't build a "proper" model. My thanks to both of you!
Ben Poole
+1  A: 

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?

ChssPly76
Your third point got me thinking about truly unique keys and such. I located the problem by identifying some additional values in the underlying table that could me build a proper unique key. I tweaked trhe default reveng file to use this, and hey presto, the Hibernate-generated data model is now working as expected!Thanks for the tips—I appreciate your taking the time to help an idiot find his way ;-)
Ben Poole