- This topic has 3 replies, 3 voices, and was last updated 16 years, 8 months ago by LSICIM.
-
AuthorPosts
-
Crasht51MemberThis message has not been recovered.
Brian FernandesModeratorThis message has not been recovered.
LSICIMMemberBrian or JP,
Background:
I am getting started with Hibernate & JPA & had a very similar issue. I am configured to use Hibernate as my persistence unit & I am also using Swing 2. I have a legacy table with a composite key, say columns A, B, & C. After reverse engineering the table I have classes MyTableId which contains properties A, B & C and class MyTable which has property Id. I need to fetch the data based on the contents of column A or column A & B respectively.Problem:
When I attempt to perform a findByProperty on column A from MyTable I get the error “org.hibernate.QueryException: could not resolve property:”. Alright, that makes sense because as noted above that property does not exist in the class MyTable. I then modified teh findByProperty in the MyTableDAO to reference MyTableId since the property exists in that class. When I tried that I get the error message “MyTableId is not mapped” even though this class is references in the persistence.xml.Question:
How to I query based on properties (columns) that exist in the MyTableId class?Below are snippets from my persistence.xml & application-context.xml
persistence.xml snippet:
<persistence-unit name=”OnPartConfigHdbxPU”
transaction-type=”RESOURCE_LOCAL”>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.stu.example.MyTable</class>
<class>com.stu.example.MyTableId</class>
<properties>
<property name=”hibernate.connection.driver_class”
value=”oracle.jdbc.driver.OracleDriver” />
<property name=”hibernate.dialect”
value=”org.hibernate.dialect.Oracle9Dialect”/>
<property name=”hibernate.connection.url”
value=”jdbc:oracle:thin:@asdf:1522:asdf” />
<property name=”hibernate.connection.username”
value=”asdf” />
<property name=”hibernate.connection.password”
value=”asdf” />
</properties>
</persistence-unit>application-context.xml snippet:
<bean id=”entityManagerFactory”
class=”org.springframework.orm.jpa.LocalEntityManagerFactoryBean”>
<property name=”persistenceUnitName” value=”OnPartConfigHdbxPU” />
</bean>
<bean id=”transactionManager”
class=”org.springframework.orm.jpa.JpaTransactionManager”>
<property name=”entityManagerFactory”
ref=”entityManagerFactory” />
</bean>
<tx:annotation-driven transaction-manager=”transactionManager” /><bean
id=”LsiConstantsTableDAO”
class=”com.stu.example.MyTableDAO”>
<property name=”entityManagerFactory”
ref=”entityManagerFactory” />
</bean>Thanks for any help,
Stu
LSICIMMemberUh, ok, I have had my coffee now. As is reasonably obvious you cannot run a findByProperty(“a”,”myvalue”) when property “a” is not a property in the table. However as noted in the original post since a is part of the composite key you will find property “a” is a property on class MyTableId. This class is exposed from MyTable.getId(). Therefore when using the findByProperty make sure you prefix “a” with the “id.” prefix to indicate it is off of the id. Therefore the correct syntax is findByProperty(“id.a”,”myvalue”).
This really was not rocket science, but perhaps a single paragraph in the JPA tutorial on dealing with legacy tables might save some folks a little time.
Stu
-
AuthorPosts