Home >> Hibernate
>> Hibernate-named-query-markup
Hibernate Query with characters conflicting with Markup.
Some of the characters or symbols, those if used in an XML
file will cause XML parser to malfunction, thus causing
program non functioning.
Similarly a case study discussed here:
If >, <,>=, <= etc are some of the notations,
those are used in SQL query to represent conditionality,
suchh as greater than, lesser than, greater than and
equals to, lesser than and equals to some value.
Suppose need arises to use these notations/operators along
with Hibernate named query in HBM XML file. In that case
one has to use CDATA along with the named query in HBM file.
Using an example you might have seen already in one of the
examples on Hibernate:
Employee and Department sharing many to one type of association
mapping with one another.
This example uses following software environment:
1. JDK 5.0
2. Eclipse 3.2
3. Hibernate 3.2
4. HSQLDB 1.8.0
Table creational DDL is as follows:
---------------------------------------------------------
create table Address
(address_id varchar(10) not null, addr_line_1 varchar(50) not null,
addr_line_2 varchar(50) not null,addr_line_3 varchar(50) not null,
city varchar(20) not null, state varchar(20) not null, pin integer,
primary key(address_id));
---------------------------------------------------------
Java POJO for this example is Address.java
Address.java
---------------------------------------------------------
package demo.profile;
import java.io.Serializable;
public class Address extends Contact implements Serializable {
private String addressId;
private String addrLine1;
private String addrLine2;
private String addrLine3;
private String city;
private String state;
private int pin;
public String getAddrLine1() {
return addrLine1;
}
public void setAddrLine1(String addrLine1) {
this.addrLine1 = addrLine1;
}
public String getAddrLine2() {
return addrLine2;
}
public void setAddrLine2(String addrLine2) {
this.addrLine2 = addrLine2;
}
public String getAddrLine3() {
return addrLine3;
}
public void setAddrLine3(String addrLine3) {
this.addrLine3 = addrLine3;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getAddressId() {
return addressId;
}
public void setAddressId(String addressId) {
this.addressId = addressId;
}
}
---------------------------------------------------------
My Example configuration file for creating Hibernate SessionFactory
is as follows:
hibernate.cfg.xml
---------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="connection.url">
jdbc:hsqldb:hsql://localhost/
</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.HSQLDialect
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="demo/profile/address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
---------------------------------------------------------
The named query is defined in the address.hbm.xml file:
address.hbm.xml
---------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="demo.profile">
<class name="Address" table="Address">
<id name="addressId" access="property" column="address_id"/>
<property name="addrLine1" column="addr_line_1"/>
<property name="addrLine2" column="addr_line_2"/>
<property name="addrLine3" column="addr_line_3"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="pin" column="pin"/>
</class>
<query name="findaddress">
<![CDATA[
from Address a where a.pin > ?
]]>
</query>
</hibernate-mapping>
---------------------------------------------------------
The section in Bold green color (as marked above) is showing
a way to mention CDATA content from within the query tag
defined in the HBM configuration file.
Testing client program for this example is very simple,
it just gets the named query from the HBM file, and then
sets appropriate parameter value to the query, starting
with the parameter index as '0'.
Final result is the number of the address records fetched from
database, in this example it is '1'.
AddressClient.java
---------------------------------------------------------
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class AddressClient {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public AddressClient() {
Session session = sessionFactory.openSession();
session.beginTransaction();
List listAddress = session.getNamedQuery("findaddress") .setParameter(0, 753012).list();
System.out.println(listAddress.size());
}
/**
* @param args
*/
public static void main(String[] args) {
new AddressClient();
}
}
---------------------------------------------------------
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/Hibernate-named-query-markup.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com