Tech I Enjoy Logo
Custom Search
   Log In    OR    Register  


  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
Some of the other Articles you may would like to read :
Hibernate Many to Many Mapping Example :
Many to many mapping example using Hibernate
Framework and a simple to follow steps.
Class Hierarchy Mapping Example :
class hierarchy mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Hibernate Interview Questions :
Interview Questions on Hibernate with answer.
Hibernate Component Property :
Hibernate Example on Component 
with source code explained.
Hibernate Join Example :
Using Table join explained with an example
while using Hibernate Framework.
Hibernate Example on Filter Criteria :
Example on using Filter Criteria
using Hibernate Framework to work with.
Hibernate Example on composite Primary key :
Example on using Hibernate Framework
to work with mapping using composite
Primary key.
Hibernate Interceptor Example :
Example on using Interceptor using Hibernate Framework
with source code explained.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Example on persisting Class Hierarchy :
Example on using Hibernate Framework
to persist Class Hierarchy into database.
Hibernate class heirarchy mapping :
Hibernate Example on mapping
class hierarchy using various ways
of persisting into database
tables.
Hibernate Bag Mapping Example :
class mapping using Bag Tag example using Hibernate
Framework and a simple to follow steps.
List of Examples on Hibernate :
List of example using Hibernate.
Hibernate Insert Update control :
Hibernate Example on controlling
insert and update attributes
Hibernate Property Formula :
Hibernate Example on Property
Tag with ease to do code walk-through
Hibernate Transaction on JBoss :
Explaining Transaction using Hibernate
on JBoss Application Server.
Hibernate one to one mapping Example :
one to one mapping explained using an example
and Hibernate Framework.
Hibernate Example on Filter :
Example on using Filter using Hibernate Framework
to work with.
Hibernate Named Query Example :
Named Query markup using an example
and Hibernate Framework.


References :
Tags: Hibernate Class Hierarchy Persist example
Tags: Hibernate composite primary key
Tags: Hibernate criteria filter example
Tags: Hibernate Filter Example
Tags: Hibernate Interceptor example
Tags: Hibernate Interview Questions
Tags: Hibernate join example
Tags: Hibernate Many to Many Mapping Example
Tags: Hibernate mapping bag
Tags: Hibernate mapping class hierarchy table per subclass
Tags: Hibernate named query markup
Tags: Hibernate One to Many mapping example
Tags: Hibernate One To One Mapping Example
Tags: Hibernate scenario one to many
Tags: Hibernate transaction jboss
Tags: Hibernate


For any of the content, if you would like to bring it to notice for removal from this web site, please write to this web site administrator @ EMAIL-ID,
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern, we would take appropriate action and 
remove disputed content from this web site within 24 hours starting from the time it has brought to our notice.


The content provided in this page is not warranted and/or guaranteed by techienjoy.com. techienjoy.com is not liable for any negative 
consequences that may result/arise from implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis, without WARRANTIES or conditions of any kind, either express
or implied, including, without limitation, merchantability, or fitness for a particular purpose. You are solely responsible for determining 
the appropriateness of using or refering this and assume any risks associated with this.

This web site is optimized for learning and training. Examples might be simplefied to improve reading and basic understanding only. 
This web site content are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. 
In spite of all precautions taken to avoid any typo in these pages, there might be some issues like grammatical mistakes and typos 
being observed in these pages, techienjoy.com extends sincerest apologies to all our visitors for the same.

While using this web site, you agree to have read and accepted our terms of use and privacy policy.


Android Examples || Android Training

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions