when querying Oracle views with Hibernate 4 I get back a list with a size bigger than 0, representing the count of elements I get when running the same query in SQL Developer for example. When I loop through the list, I however only get null values.
My hibernate.cfg.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:OUT</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.default_schema">schema</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="be/comp/model/db/VwPersoneelslid.hbm.xml"/>
</session-factory>
The VwPersoneelslid.hbm.xml looks like:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 9-aug-2013 10:13:31 by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
<class name="be.comp.model.db.VwPersoneelslid" table="VW_PERSONEELSLID">
<composite-id name="id" class="be.comp.model.db.VwPersoneelslidId">
<key-property name="pkVwPersoneelslid" type="double">
<column name="PK_VW_PERSONEELSLID" precision="126" scale="0" />
</key-property>
<key-property name="fkVwFunctie" type="double">
<column name="FK_VW_FUNCTIE" precision="126" scale="0" />
</key-property>
<key-property name="familienaam" type="string">
<column name="FAMILIENAAM" length="80" />
</key-property>
<key-property name="voornaam" type="string">
<column name="VOORNAAM" length="80" />
</key-property>
<key-property name="code" type="string">
<column name="CODE" length="10" />
</key-property>
</composite-id>
</class>
The function with the query is:
public List<VwPersoneelslid> findByCode(String code)
{
Session session = HibernateUtil.getSessionFactoryNeptunus().getCurrentSession();
session.beginTransaction();
List<VwPersoneelslid> list = new ArrayList();
Query query = session.createQuery("FROM VwPersoneelslid WHERE code = :code");
query.setParameter("code", code);
list = query.list();
session.getTransaction().commit();
return list;
}
The function transformed to native SQL:
public List<VwPersoneelslid> findByCode(String code)
{
Session session = HibernateUtil.getSessionFactoryNeptunus().getCurrentSession();
session.beginTransaction();
List<VwPersoneelslid> list = new ArrayList();
Query query = session.createSQLQuery("SELECT * FROM CIPALII.VW_PERSONEELSLID WHERE code = :code").addEntity(VwPersoneelslid.class)
.setParameter("code", code);
list = query.list();
session.getTransaction().commit();
return list;
}
If you guys need more information in order to know what might be the problem, please let me know and I add more code. The VwPersoneelslid.hbm.xml file is auto generated by Eclipse. Thanks.
I managed to create a workaround. The problem, for me at least, lies in the fact that the Hibernate Tools create composite-id 's. Which I can not get to work correctly, without jumping through several hoops. So I managed to get rid of the composite-id . In my reveng.xml file I added:
<table name="VW_PERSONEELSLID">
<primary-key>
<key-column name="PK_VW_PERSONEELSLID" />
</primary-key>
</table>
This generates a single id (PK_VW_PERSONEELSLID) and it allows me to continue to use the Hibernate Tools. I do not know if this is a dirty fix. Will probably find that out in a couple of days.
Hibernate HQL Queries deals with Entities . Views are the concept of Database or SQL Schema. When we query data using HQL we mention the Entity name and Entity Properties. So if you want to query from Views , Use Core SQL Queries with Hibernate instead Of HQL Queries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With