4

I want to retrieve data from the database (Oracle) using Hibernate.

I want to select all columns from a view. The view has no primary key, so I used composite key in the Hibernate-mapping.

Firstly my class:

public class MyBean implements Serializable {
    private MyBeanId compId;
    private String col1;
    private String col2;
    // getters and setters
}

Where the MyBeanId class:

public class MyBeanId implements Serializable {
    private int id1;
    private int id2;
    // getters and setters, hashCode and equals
}

The Hibernate mapping:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mypackage">
<class name="MyBean" table="MY_TABLE">
    <composite-id name="compId" class="MyBeanId ">
        <key-property column="COL_ID1" name="id1" type="int"/>
        <key-property column="COL_ID2" name="id2" type="int"/>

    </composite-id>
    <property name="col1" type="string">
        <column name="COL1" />
    </property>
    <property name="col2" type="string">
        <column name="COL2" />
    </property>
</class>
</hibernate-mapping>

My DAO (MyBeanManagerImpl):

public List<MyBean> getMyBeans(Session session) {

    try {
        Criteria criteria = session.createCriteria(MyBean.class);
        List<MyBean> list = criteria.list();
        System.out.println(list.toString());
        return list;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

And my table MY_TABLE:

ID1,ID2,COL1,COL2
1,2,"a","b"
3,2,"c","d"

The result is an empty list. I've verified there's data in my table. When I use other managers in my service, there's no problem getting the result so there's no problem with the session.

There's no Exception thrown at all, so it is strange it doesn't get any result.

7
  • How does your db look like? Commented Nov 18, 2013 at 16:26
  • Why do you have 'cutter' <property name="cutter" type="string"> <column name="COL2" /> </property> for the property col2? Commented Nov 18, 2013 at 16:35
  • @ŁukaszRzeszotarski Sorry, I've forgotten to change that when I paste my code Commented Nov 18, 2013 at 16:40
  • Do you use any framework like spring where you configure session factory for instance? Commented Nov 18, 2013 at 16:46
  • Alternatively did you added mapping-resource entry for the new class/mapping to hibernate-configuration? Commented Nov 18, 2013 at 16:54

1 Answer 1

5

Add mapping-resource entry for the new mapping in yours hibernate-configuration -> session factory configuration like you have for other already mapped classes.

See the link https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/Hibernate_Core_Reference_Guide/tutorial.html#tutorial-firstapp-configuration Chapter 'Hibernate configuration' there you have example of hibernate.cfg.xml. You need to add <mapping-resouce ... entry

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.