facebook

JPA get a findBy method that uses part of primary key

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #277949 Reply

    Crasht51
    Member

    This message has not been recovered.

    #277957 Reply

    Brian Fernandes
    Moderator

    This message has not been recovered.

    #284571 Reply

    LSICIM
    Member

    Brian 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

    #284575 Reply

    LSICIM
    Member

    Uh, 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

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: JPA get a findBy method that uses part of primary key

You must be logged in to post in the forum log in