I do have a java web application, which does use Hibernate ORM for database connectivity. There are a lot of database tables, mainly configured as hibernate xml mapping files , i.e hbm.xml where each file is a table in the database.
Now the question is, - there are a view created with postgreSQl in the database and I want to use that view in java, how to do it? If I create another hbm.xml file that will be a table created in database. I've read over the internet there are examples on how to use it with annotations, but I dont have it configured in my web application.
Update1:
After some research and recommendation on below answer, this is what I have atm:
I created xml mapping file and a java class with getters/setters for each property:
<?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"
>
<hibernate-mapping>
<class name="org.xxx.model.SearchView" table="search" mutable="false">
<id name="uuId" column="uuid">
</id>
<property name="typeOfCRA" column="typeofcra" />
<property name="birthDate" column="birthDate" />
<property name="lastName" column="lastname" />
<property name="firstName" column="firstname" />
<property name="middleName" column="middlename" />
<property name="recordId" column="recordid" />
<property name="registrationDate" column="registrationdate" />
<property name="state" column="state" />
<property name="organisationunitId" column="organisationunitid" />
<property name="version" column="version" />
<property name="gender" column="gender" />
</class>
</hibernate-mapping>
This is how I call it in java to get a list of results from database:
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(SearchView.class);
criteria.add(Restrictions.like("typeofcra", "birth"));
List<SearchView> tmp =criteria.list();
It does'nt give any errors nothing. It keeps returning 0 records based on my criteria. But when I filter it out directly in Postgresql with the same criteria it returns almost 600k results.
During compilation or running this code snippet there are also 0 errors. Any help is appreciated.
Update2:
Hibernate conf file :
connection.dialect = org.hibernate.dialect.PostgreSQLDialect
connection.driver_class = org.postgresql.Driver
connection.url = jdbc:postgresql://localhost:5432/finalDb
connection.username = postgres
connection.password = root
connection.schema = update
Updated code based on answer below:
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(SearchView.class);
criteria.add(Restrictions.like("typeOfCRA", "%birth%"));
List<SearchView> tmp =criteria.list();
Direct call within the PgAdmin works:
SELECT typeofcra, dateofbirth, lastname, firstname, middlename, recordid,
registrationdate, state, organisationunitid, uuid, version, gender
FROM public.search where typeofcra like '%birth%';
But from java it doesnt work.