Reputation: 934
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.
Upvotes: 4
Views: 9863
Reputation: 6140
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
Upvotes: 5