tag:blogger.com,1999:blog-33682079848412800972024-02-18T21:47:14.778-08:00TechnofesA place where I share my findings while on study.Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.comBlogger96125tag:blogger.com,1999:blog-3368207984841280097.post-81389122319967307732018-03-17T05:54:00.000-07:002018-03-19T10:51:47.257-07:00Laravel - Setup Tips<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<h2 style="text-align: left;">
<b>Valet</b></h2>
<b><span style="color: blue;"><i>HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers:</i></span></b><br />
To resolve this error, do the following:<br />
1. valet stop<br />
2. valet uninstall<br />
3. rm -rf ~/.valet<br />
4. valet install<br />
5. valet start<br />
6. Link the applications again.<br />
<br />
<br />
<b>Restart phpfpm on homestead:</b><br />
sudo nginx -s reload<br />
sudo service php7.0-fpm restart</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-30766996056116398852018-03-17T05:53:00.000-07:002018-03-17T05:53:34.469-07:00Laravel - Code Tips<div dir="ltr" style="text-align: left;" trbidi="on">
<b><u>To Log all queries executed by Eloquent:</u></b><br />
Add the below code snippet into AppServiceProvider boot() method.<br />
<br />
<pre style="background-color: white; font-family: Menlo; font-size: 9pt;">\Event::listen('Illuminate\Database\Events\QueryExecuted', function ($query) {
\Illuminate\Support\Facades\Log::info($query->sql);
\Illuminate\Support\Facades\Log::info($query->bindings);
\Illuminate\Support\Facades\Log::info($query->time);
});</pre>
<br />
<pre style="background-color: white; font-family: Menlo; font-size: 9pt;">\DB::listen(function($sql, $bindings, $time) {
Log::info($sql);
Log::info($bindings);
Log::info($time);
});</pre>
<br />
<pre style="background-color: white; font-family: Menlo; font-size: 9pt;"></pre>
<pre style="background-color: white; font-family: Menlo; font-size: 9pt;"></pre>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-34110895096717798802017-03-08T06:42:00.000-08:002017-07-09T13:37:36.592-07:00AWS<div dir="ltr" style="text-align: left;" trbidi="on">
EC2:<br />
Login:<br />
<span style="font-family: "menlo"; font-size: 11px;">1. Copy your .pem file to ~/.ssh.</span><br />
<span style="font-family: "menlo"; font-size: 11px;">2. Change permissions to chmod 500 <your_file>.pem</span><br />
<span style="font-family: "menlo"; font-size: 11px;">3. Login to your server.</span><br />
<span style="font-family: "menlo"; font-size: 11px;"><br /></span>
<span style="font-family: "menlo"; font-size: 11px;">ssh -i /full-path-to/.pem ec2-user@PUBLIC_DNS</span><br />
<span style="font-family: "menlo"; font-size: 11px;"><br /></span>
<span style="font-family: "menlo"; font-size: 11px;"><br /></span>
<span style="font-family: "menlo";"><span style="font-size: 11px; font-variant-ligatures: no-common-ligatures;">INSTALL PHP:</span></span><br />
<pre class="lang-php prettyprint prettyprinted" style="background-color: #eff0f1; border: 0px; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; margin-bottom: 1em; max-height: 600px; overflow: auto; padding: 5px; width: auto; word-wrap: normal;"><code style="border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; margin: 0px; padding: 0px; white-space: inherit;"><span class="pln" style="border: 0px; color: #303336; margin: 0px; padding: 0px;">sudo yum install php56</span></code></pre>
<span style="font-family: "menlo";"><span style="font-size: 11px; font-variant-ligatures: no-common-ligatures;"><br /></span></span><span style="font-family: "menlo";"><span style="font-size: 11px; font-variant-ligatures: no-common-ligatures;">Free Memory Check:</span></span><style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
span.s1 {font-variant-ligatures: no-common-ligatures}
</style><br />
<span style="font-family: "menlo";"><span style="font-size: 11px; font-variant-ligatures: no-common-ligatures;">free -m (in MB)</span></span><br />
<span style="font-family: "menlo";"><span style="font-size: 11px; font-variant-ligatures: no-common-ligatures;"><br /></span></span>
<span style="font-family: menlo;"><span style="font-size: 11px;">Hard disk capacity Check:</span></span><br />
<span style="font-family: menlo;"><span style="font-size: 11px;">df -h (Human Readable)</span></span></div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-75926620866897581892017-03-08T06:41:00.000-08:002017-07-09T13:37:36.596-07:00GIT Resets<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>GIT REST HELP</b><br /><ul style="text-align: left;">
<li>Soft</li>
<ul>
<li>git reset --soft <commit id></li>
</ul>
<li>Mixed</li>
<ul>
<li>git reset --mixed <commit id></li>
</ul>
<li>Hard</li>
<ul>
<li>git reset --hard <commit id></li>
</ul>
</ul>
<div>
<br /></div>
<div>
Check git log status:</div>
<div>
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
span.s1 {font-variant-ligatures: no-common-ligatures}
</style>
<div class="p1">
<span class="s1">git log --oneline</span></div>
</div>
<div>
<b><br /></b></div>
<div>
<b>Soft:</b></div>
<div>
git reset --soft <commit id></div>
<div>
<br /></div>
<div>
Result:</div>
<div>
Brings the files changed after this commit id into staging index. </div>
<div>
i.e, as if those changed files were added using git add command.</div>
<div>
<br /></div>
<div>
<b>Mixed:</b></div>
<div>
git reset --mixed <commit id></div>
<div>
<br /></div>
<div>
<div>
Result:</div>
<div>
Brings the files changed after this commit id into Working Directory. </div>
<div>
i.e, as if those changed files were changed but not gone through git add yet.</div>
</div>
<div>
<br /></div>
<div>
<b>Hard:</b></div>
<div>
git reset --hard <commit id></div>
<div>
<br /></div>
<div>
This is the most dangerous command as it is going to completely wipe out our working directory and staging index. Any files you were tinkering with will be gone.</div>
<div>
<br /></div>
<div>
<div>
Result:</div>
<div>
Brings the files changed after this commit id into staging index. </div>
<div>
i.e, as if those changed files were added using git add command.</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<b>git reset --hard HEAD</b></div>
<div>
This will bring your code base to the commit where HEAD is pointing to.</div>
<div>
Basically this will wipe out any changes in Working Directory and Staging Index.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
TODO:</div>
<div>
Pushing reset commit to remote?</div>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-46470688751390561032017-01-20T08:17:00.001-08:002017-01-20T08:17:15.356-08:00Set Mule ESB to use TLS v1.2<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: left;"><span style="font-family: "times" , "times new roman" , serif;">Mulesoft Anypoint Studio was giving me trouble for a long time to connect to an external HTTP server that supports only TLSv1.2.</span></div><div style="text-align: left;"><span style="font-family: "times" , "times new roman" , serif;"><br />
</span></div><div style="text-align: left;"><span style="font-family: "times" , "times new roman" , serif;">Below options did not work for me:</span></div><div style="text-align: left;"><span style="font-family: "times" , "times new roman" , serif;">-Dhttps.protocols=TLSv1.2</span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;">-Djdk.tls.client.protocols=TLSv1.2</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;"><br />
</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;">Solution:</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;">I had to create a tls-default.conf file directly under src/main/resources (classpath root folder) and set</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;"><br />
</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;">enabledProtocols=TLSv1.2</span></span></div><div style="text-align: left;"><span style="background-color: white;"><span style="font-family: "times" , "times new roman" , serif;"><br />
</span></span></div><div style="text-align: left;"><br />
</div><div style="text-align: left;"><span style="font-family: "times" , "times new roman" , serif;"><span style="background-color: white;">Full Configuration:</span></span></div><div class="p1"><pre># This file allows to restrict SSL behavior in Mule. If the file doesn't exist or a property is not defined,
# default values of the current security provider will be used.
# Cipher suites that will be enabled in SSL. If this property is set, SSL sockets will
# only use cipher suites that are provided in this list and supported by the current security provider.
#enabledCipherSuites=TLS_KRB5_WITH_3DES_EDE_CBC_MD5, \
# SSL_DH_anon_WITH_DES_CBC_SHA, \
# TLS_DH_anon_WITH_AES_128_CBC_SHA, \
# TLS_DHE_RSA_WITH_AES_128_CBC_SHA, \
# SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, \
# SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, \
# TLS_DHE_RSA_WITH_AES_256_CBC_SHA, \
# TLS_KRB5_WITH_3DES_EDE_CBC_SHA, \
# TLS_KRB5_WITH_DES_CBC_MD5, \
# TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5, \
# SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, \
# SSL_DHE_DSS_WITH_DES_CBC_SHA, \
# TLS_KRB5_WITH_DES_CBC_SHA, \
# SSL_RSA_WITH_NULL_MD5, \
# TLS_DHE_DSS_WITH_AES_256_CBC_SHA, \
# SSL_DH_anon_WITH_3DES_EDE_CBC_SHA, \
# TLS_RSA_WITH_AES_128_CBC_SHA, \
# SSL_DHE_RSA_WITH_DES_CBC_SHA, \
# TLS_DH_anon_WITH_AES_256_CBC_SHA, \
# TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA, \
# SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA, \
# SSL_RSA_WITH_NULL_SHA, \
# TLS_RSA_WITH_AES_256_CBC_SHA, \
# SSL_RSA_WITH_DES_CBC_SHA, \
# TLS_EMPTY_RENEGOTIATION_INFO_SCSV, \
# SSL_RSA_EXPORT_WITH_DES40_CBC_SHA, \
# TLS_DHE_DSS_WITH_AES_128_CBC_SHA, \
# SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, \
# SSL_RSA_WITH_3DES_EDE_CBC_SHA
# Protocols that will be enabled in SSL. If this property is set, SSL sockets will only use protocols
# that are provided in this list and supported by the current security provider.
#enabledProtocols=TLSv1,TLSv1.1,TLSv1.2
enabledProtocols=TLSv1.2
</pre></div><div class="p1"><br />
</div></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-45535576739487063292013-02-18T21:21:00.001-08:002013-02-18T22:02:41.374-08:00Custom XML Generator<div dir="ltr" style="text-align: left;" trbidi="on">This is a simple XML Generator that can be used to send in any object (tested with List and custom objects) and get the XML out of it. This could be useful while building java to xml transformations for any objects.<br />
<br />
<pre class="source-code"><span style=' color: Blue;'>import</span> java.lang.reflect.InvocationTargetException;
<span style=' color: Blue;'>import</span> java.lang.reflect.Method;
<span style=' color: Blue;'>import</span> java.util.ArrayList;
<span style=' color: Blue;'>import</span> java.util.Arrays;
<span style=' color: Blue;'>import</span> java.util.HashSet;
<span style=' color: Blue;'>import</span> java.util.List;
<span style=' color: Blue;'>import</span> java.util.Set;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>class</span> XMLGeneratorImpl {
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String GET_METHOD = <span style=' color: Maroon;'>"get"</span>;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String IS_METHOD = <span style=' color: Maroon;'>"is"</span>;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String EMPTY = <span style=' color: Maroon;'>""</span>;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String START_TAG = <span style=' color: Maroon;'>"<"</span>;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String END_TAG = <span style=' color: Maroon;'>">"</span>;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> String SLASH = <span style=' color: Maroon;'>"/"</span>;
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>boolean</span> trimPackageNames;
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>static</span> <span style=' color: Blue;'>final</span> Set<Class> WRAPPER_TYPES = <span style=' color: Blue;'>new</span> HashSet(Arrays.asList(
Boolean.<span style=' color: Blue;'>class</span>, Character.<span style=' color: Blue;'>class</span>, Byte.<span style=' color: Blue;'>class</span>, Short.<span style=' color: Blue;'>class</span>,
Integer.<span style=' color: Blue;'>class</span>, Long.<span style=' color: Blue;'>class</span>, Float.<span style=' color: Blue;'>class</span>, Double.<span style=' color: Blue;'>class</span>, Void.<span style=' color: Blue;'>class</span>,
String.<span style=' color: Blue;'>class</span>));
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>enum</span> ObjectFormat {
PRIMITIVE, OBJECT, LIST, MAP, SET, ARRAY;
}
<span style=' color: Blue;'>public</span> String toXML(Object input) {
ObjectFormat format = getFormat(input);
<span style=' color: Blue;'>return</span> convertToXml(input, format);
}
<span style=' color: Blue;'>private</span> String convertToXml(Object input, ObjectFormat format) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
String objectName = getObjectName(input);
<span style=' color: Blue;'>switch</span> (format) {
<span style=' color: Blue;'>case</span> OBJECT:
xml.append(START_TAG).append(objectName).append(END_TAG);
xml.append(getXMLFromObject(input));
xml.append(START_TAG).append(SLASH).append(objectName)
.append(END_TAG);
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>case</span> ARRAY:
xml.append(getXMLFromArray(input));
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>case</span> LIST:
xml.append(getXMLFromList(input));
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>case</span> PRIMITIVE:
xml.append(getXMLFromPrimitive(<span style=' color: Blue;'>null</span>, input));
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>default</span>:
<span style=' color: Blue;'>throw</span> <span style=' color: Blue;'>new</span> IllegalArgumentException(<span style=' color: Maroon;'>"Unsupported request object"</span>);
}
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> String getXMLFromPrimitive(String key, Object input) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
String xmlTag = key == <span style=' color: Blue;'>null</span> ? getObjectName(input) : key;
xml.append(START_TAG).append(xmlTag).append(END_TAG)
.append(input.toString()).append(START_TAG).append(SLASH)
.append(xmlTag).append(END_TAG);
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> String getXMLFromObject(Object input) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
List<Method> methods = getAllowedMethods(input);
<span style=' color: Blue;'>for</span> (Method method : methods) {
xml.append(getXMLValue(input, method));
}
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> String getXMLFromArray(Object input) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
Object[] objects = (Object[]) input;
<span style=' color: Blue;'>for</span> (Object object : objects) {
ObjectFormat of = getFormat(object);
xml.append(convertToXml(object, of));
}
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> String getXMLFromList(Object input) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
List objects = (List) input;
<span style=' color: Blue;'>for</span> (Object object : objects) {
ObjectFormat of = getFormat(object);
xml.append(convertToXml(object, of));
}
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> List<Method> getAllowedMethods(Object input) {
List<Method> getters = <span style=' color: Blue;'>new</span> ArrayList<Method>();
Method[] methods = input.getClass().getDeclaredMethods();
<span style=' color: Blue;'>for</span> (Method method : methods) {
<span style=' color: Blue;'>if</span> (isGetterMethod(method)) {
getters.add(method);
}
}
<span style=' color: Blue;'>return</span> getters;
}
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>boolean</span> isGetterMethod(Method method) {
<span style=' color: Blue;'>boolean</span> getter = <span style=' color: Maroon;'>false</span>;
<span style=' color: Blue;'>if</span> (method.getName().startsWith(GET_METHOD)
|| method.getName().startsWith(IS_METHOD)) {
getter = <span style=' color: Maroon;'>true</span>;
}
<span style=' color: Blue;'>return</span> getter;
}
<span style=' color: Blue;'>private</span> String getXMLValue(Object input, Method method) {
StringBuilder xml = <span style=' color: Blue;'>new</span> StringBuilder();
<span style=' color: Blue;'>try</span> {
Object value = method.invoke(input, <span style=' color: Blue;'>null</span>);
String xmlTagName = getObjectName(<span style=' color: Blue;'>null</span>, method);
<span style=' color: Blue;'>if</span> (value != <span style=' color: Blue;'>null</span>) {
ObjectFormat of = getFormat(value);
<span style=' color: Blue;'>switch</span> (of) {
<span style=' color: Blue;'>case</span> PRIMITIVE:
xml.append(START_TAG).append(xmlTagName).append(END_TAG)
.append(value).append(START_TAG).append(SLASH)
.append(xmlTagName).append(END_TAG);
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>case</span> OBJECT:
xml.append(convertToXml(value, of));
<span style=' color: Blue;'>break</span>;
<span style=' color: Blue;'>case</span> LIST:
<span style=' color: Blue;'>case</span> MAP:
<span style=' color: Blue;'>case</span> SET:
<span style=' color: Blue;'>case</span> ARRAY:
xml.append(START_TAG).append(xmlTagName).append(END_TAG)
.append(convertToXml(value, of)).append(START_TAG)
.append(SLASH).append(xmlTagName).append(END_TAG);
<span style=' color: Blue;'>break</span>;
}
}
} <span style=' color: Blue;'>catch</span> (IllegalArgumentException e) {
<span style=' color: Green;'>// TODO: handle exception</span>
} <span style=' color: Blue;'>catch</span> (IllegalAccessException e) {
<span style=' color: Green;'>// TODO Auto-generated catch block</span>
e.printStackTrace();
} <span style=' color: Blue;'>catch</span> (InvocationTargetException e) {
<span style=' color: Green;'>// TODO Auto-generated catch block</span>
e.printStackTrace();
}
<span style=' color: Blue;'>return</span> xml.toString();
}
<span style=' color: Blue;'>private</span> ObjectFormat getFormat(Object input) {
ObjectFormat format = ObjectFormat.OBJECT;
Class clazz = input.getClass();
<span style=' color: Blue;'>if</span> (isWrapperType(clazz)) {
format = ObjectFormat.PRIMITIVE;
} <span style=' color: Blue;'>else</span> <span style=' color: Blue;'>if</span> (clazz.getName().contains(<span style=' color: Maroon;'>"List"</span>)) {
format = ObjectFormat.LIST;
}
<span style=' color: Blue;'>return</span> format;
}
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>boolean</span> isWrapperType(Class clazz) {
<span style=' color: Blue;'>return</span> WRAPPER_TYPES.contains(clazz);
}
<span style=' color: Blue;'>private</span> String getXMLTagName(Method method) {
String xmlTagName = <span style=' color: Blue;'>null</span>;
<span style=' color: Blue;'>if</span> (method.getName().startsWith(GET_METHOD)) {
xmlTagName = method.getName().substring(<span style=' color: Maroon;'>3</span>);
} <span style=' color: Blue;'>else</span> {
xmlTagName = method.getName().substring(<span style=' color: Maroon;'>2</span>);
}
<span style=' color: Blue;'>return</span> xmlTagName;
}
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>boolean</span> isTrimPackageNames() {
<span style=' color: Blue;'>return</span> trimPackageNames;
}
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>void</span> setTrimPackageNames(<span style=' color: Blue;'>boolean</span> trimPackageNames) {
<span style=' color: Blue;'>this</span>.trimPackageNames = trimPackageNames;
}
<span style=' color: Blue;'>private</span> String getObjectName(Object input) {
<span style=' color: Blue;'>return</span> getObjectName(input, <span style=' color: Blue;'>null</span>);
}
<span style=' color: Blue;'>private</span> String getObjectName(Object input, Method method) {
String xmlTagName = EMPTY;
<span style=' color: Blue;'>if</span> (input != <span style=' color: Blue;'>null</span>) {
xmlTagName = input.getClass().getName();
<span style=' color: Blue;'>if</span> (trimPackageNames && xmlTagName.indexOf(<span style=' color: Maroon;'>"."</span>) != -<span style=' color: Maroon;'>1</span>) {
xmlTagName = xmlTagName
.substring(xmlTagName.lastIndexOf(<span style=' color: Maroon;'>"."</span>) + <span style=' color: Maroon;'>1</span>);
}
} <span style=' color: Blue;'>else</span> <span style=' color: Blue;'>if</span> (method != <span style=' color: Blue;'>null</span>) {
xmlTagName = getXMLTagName(method);
<span style=' color: Blue;'>if</span> (trimPackageNames && xmlTagName.indexOf(<span style=' color: Maroon;'>"."</span>) != -<span style=' color: Maroon;'>1</span>) {
xmlTagName = xmlTagName
.substring(xmlTagName.lastIndexOf(<span style=' color: Maroon;'>"."</span>) + <span style=' color: Maroon;'>1</span>);
}
}
<span style=' color: Blue;'>return</span> xmlTagName;
}
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-2071098145998067632013-02-09T06:27:00.000-08:002013-02-09T06:30:24.047-08:00@Controller and @RequestMapping<div dir="ltr" style="text-align: left;" trbidi="on">Since Spring 2.5 onwards, we can configure controllers through Spring Annotations which helps us in many ways.<br />
<br />
<ol style="text-align: left;"><li>Spring bean definition files to be of minimal size.</li>
<li>No need to extend any spring controllers and easy to create form and multi-action controller</li>
</ol><div>@Controller:</div><div>Tells the spring framework that this class is used as a controller and should be considered by Dispatcher Servlet for delegating the requests to this controller.</div><div><br />
</div><div>@RequestMapping:</div><div>Tells the spring framework what are all the URLs that would be handled by this controller.</div><div><br />
</div><div><pre class="source-code">com.controller.test;
<strong><span style="color: #7f0055;">import</span></strong> org.springframework.stereotype.Controller;
<strong><span style="color: #7f0055;">import</span></strong> org.springframework.web.bind.annotation.RequestMapping;
<strong><span style="color: #7f0055;">import</span></strong> org.springframework.web.bind.annotation.RequestMethod;
<strong><span style="color: #7f0055;">import</span></strong> org.springframework.web.servlet.ModelAndView;
<span style="color: #646464;">@Controller</span>(<span style="color: #2a00ff;">"HelloWordController"</span>)
<span style="color: #646464;">@RequestMapping</span>(<span style="color: #2a00ff;">"/hello"</span>)
<strong><span style="color: #7f0055;">public</span></strong> <strong><span style="color: #7f0055;">class</span></strong> HelloWorldController {
<span style="color: #646464;">@RequestMapping</span>(value = <span style="color: #2a00ff;">"/world"</span>, method = RequestMethod.GET)
<strong><span style="color: #7f0055;">public</span></strong> ModelAndView helloWord() {
<strong><span style="color: #7f0055;">return</span></strong> <strong><span style="color: #7f0055;">new</span></strong> ModelAndView(<span style="color: #2a00ff;">"HelloWorld"</span>);
}
}
</pre></div><div>Now tell spring framework to scan your package for annotations. and define the HelloWorld webpage as per your requirement.<br />
<br />
<context:component-scan base-package="com.controller.test" /></div><br />
Thats it. Now deploy & start your application. Hit the below URL and see the actions.<br />
<br />
<a href="http://localhost:8080/ches-swes-admin/hello/world">http://localhost:8080/yourproject/hello/world</a></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-31766960490272596802013-02-03T07:30:00.001-08:002013-02-09T05:57:28.255-08:00Generating POJOs from Database Scema (Used HSQL)<div dir="ltr" style="text-align: left;" trbidi="on">
Ok, Now in a project we have the tables created in a database server. How can i leverage those tables to generate my hibernate pojos or domain objects easily?<br />
<br />
<br />
a. Install Hibernate Tools.<br />
b. Go to Hibernate Perspective (Window -> Open Perspective -> Other -> Hibernate)<br />
c. In the Hibernate Configurations View, Right click -> Add Configuration<br />
d. Under Main Tab,<br />
* Select the project where the POJOS need to be generated.<br />
* Select the database connection as Hibernate Configured Connection.<br />
* Create the hibernate Configuration file (Properties file is not mandatory) with the following values.<br />
<?xml version="1.0" encoding="UTF-8"?><br />
<!DOCTYPE hibernate-configuration PUBLIC<br />
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br />
<hibernate-configuration><br />
<session-factory name="TechFes"><br />
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property><br />
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:9001/TechFes</property><br />
<property name="hibernate.connection.username">SA</property><br />
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property><br />
</session-factory><br />
</hibernate-configuration><br />
e. Now go to Run icon (the run icon in Hibernate perspective) and click on Hibernate Code Generation Configurations.<br />
f. Under Main Tab,<br />
* Select the output directory where the files has to get generated.<br />
* Select the "Reverse Engineer from JDBC Connection" checkbox.<br />
<div style="text-align: left;">
* Provide the package name under which the domain java objects need to be created. <br />
* Uncheck "Detect many-to-many" associations. This will help in generating the pojo objects for the tables that are maintained only for many-many association.</div>
g. Under Exporters Tab,<br />
* Check the Domain Code, hibernate cfg file and if required annotations, etc.<br />
<br />
h. Click on Run and your domain code should have got generated.</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-73789108452709958122013-02-03T06:39:00.002-08:002013-02-03T06:39:30.863-08:00Standalone HSQL DB Setup<div dir="ltr" style="text-align: left;" trbidi="on">
I have a project and have the schemas available. But I don't have an actual database setup yet. Now run HSQLDB in standalone mode from command line and load the tables and get going :-) What are the steps to do that?<br />
<br /> a. Download latest version of HSQLDB from <a href="http://hsqldb.org/">http://hsqldb.org</a>.<br />
<br /> b. Goto hsql db folder and create a file named server.properties with the following entries.<br /> server.database.0=file:hsqldb/TechFes<br /> server.dbname.0=TechFes<br /> c. Once the JAVA_HOME and PATH variables are updated with the correct java location,<br /> execute this command from hsqldb folder where server.properties file is created<br /> <br /> >java -classpath lib/hsqldb.jar org.hsqldb.server.Server<br /> <br /> NOTE: This command would create a folder hsqldb and create the files for TechFes database under that folder.<br /> <br /> d. Now we need to open the database with additional params to the above command.<br /> <br /> >java -classpath lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:hsqldb/TechFes --dbname.0 TechFes<br /> <br /> NOTE: This would open the database so clients can connect to it.<br /> <br /> e. Now we can use the below command to launch the UI for this database and make changes to the schema.<br /> <br /> >java -cp lib/hsqldb.jar org.hsqldb.util.DatabaseManagerSwing</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-40208831944086877422012-05-21T23:38:00.001-07:002012-05-21T23:38:18.273-07:00Hibernate Connection Release Modes<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
<b>Hibernate Connection Release Modes:</b></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The different release modes that are supported by Hibernate are as follows:</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
1.<code> </code><b>on_close: </b>The
Hibernate session obtains a connection when it first needs to perform some JDBC access
and maintains that connection until the session is closed.
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
2.<b> after_transaction: </b>uses ConnectionReleaseMode.AFTER_TRANSACTION.
This setting should not be used in JTA environments. Also note that with
ConnectionReleaseMode.AFTER_TRANSACTION, if a session is considered to be in auto-commit
mode, connections will be released as if the release mode were AFTER_STATEMENT.
</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
3. <b>after_statement:</b> uses ConnectionReleaseMode.AFTER_STATEMENT. Additionally,
the configured <code class="literal">ConnectionProvider</code> is consulted to see if it supports this
setting (<code class="literal">supportsAggressiveRelease()</code>). If not, the release mode is reset
to ConnectionReleaseMode.AFTER_TRANSACTION. This setting is only safe in environments where
we can either re-acquire the same underlying JDBC connection each time you make a call into
<code class="literal">ConnectionProvider.getConnection()</code> or in auto-commit environments where
it does not matter if we re-establish the same connection</div>
</div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-40942848264068000682012-05-10T07:52:00.002-07:002012-05-11T11:36:11.427-07:00Hibernate Logging<div dir="ltr" style="text-align: left;" trbidi="on">We can use two ways of hibernate configuration for debugging the sql queries generated by hibernate framework.<br />
<br />
<b>1. Using log4j.properties</b><br />
<br />
# Hibernate logging options (INFO only shows startup messages)<br />
Log4j.logger.org.hiernate=INFO<br />
<br />
Log4j.rootLogger=INFO, console<br />
<br />
#The below logger statement is equivalent to hibernate.show_sql=true<br />
log4j.logger.org.hibernate.SQL = DEBUG<br />
<br />
# Log JDBC bind parameter runtime arguments. <br />
# The below logger prints the values binded to sql query and the response from the query.<br />
log4j.logger.org.hibernate.type = TRACE<br />
<br />
log4j.appender.console=org.apache.log4j.ConsoleAppender<br />
log4j.appender.console.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.console.layout.ConversionPattern=IPDMS[%d{yyyy-MM-dd HH:mm:ss}] %5p (%F:%L) %m%n<br />
<br />
<b>2. Hibernate configuration</b><br />
<br />
<pre class="source-code"><span style=' color: Blue;'><</span><span style=' color: Maroon;'>property</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>show_sql</span>"<span style=' color: Blue;'>></span>true<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>property</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>property</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>format_sql</span>"<span style=' color: Blue;'>></span>true<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>property</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>property</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>use_sql_comments</span>"<span style=' color: Blue;'>></span>true<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>property</span><span style=' color: Blue;'>></span>
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-14574980942572431312011-10-06T20:45:00.000-07:002011-10-06T20:49:38.567-07:00org.jasypt.exceptions.EncryptionOperationNotPossibleException<div dir="ltr" style="text-align: left;" trbidi="on">
Many of us have come across this exception while using encryption in our functionality.<br />
<br />
<div style="text-align: justify;">
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><span class="Apple-style-span" style="color: blue;"><strong style="background-attachment: initial; background-clip: initial; background-color: transparent; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-width: 0px; border-color: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-size: 14px; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">org.jasypt.exceptions.EncryptionOperationNotPossibleException</strong>:</span> Encryption raised an excep tion. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine</span><br />
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><b><span class="Apple-style-span" style="color: #cc0000;"><br /></span></b></span><br />
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><b><span class="Apple-style-span" style="color: #cc0000;">Resolution:</span></b></span><br />
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span><br />
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;">As the exception indicates one need to install the latest JCE Policy Jar Files into your JAVA setup.</span><br />
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;">1. Download the JCE Policy jar files from the below location:</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><a href="http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html">http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html</a></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;">2. The zip file would contain two jar files (local_policy.jar and US_export_policy.jar). </span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;">3. These jar files need to be placed under the following location:</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;">%JAVA_HOME%\jre\lib\security\</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><br /></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><b><span class="Apple-style-span" style="color: #cc0000;">Reference Link:</span></b></span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 14px; line-height: 17px;"><a href="http://download.oracle.com/javase/1,5.0/docs/guide/security/jce/JCERefGuide.html#InstallProvider">http://download.oracle.com/javase/1,5.0/docs/guide/security/jce/JCERefGuide.html#InstallProvider</a></span></div>
</div>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com2tag:blogger.com,1999:blog-3368207984841280097.post-45524095507985931402011-10-03T20:25:00.000-07:002011-10-03T20:25:47.717-07:00Mule - Expression Splitter Router<div dir="ltr" style="text-align: left;" trbidi="on">
The following example is used to simulate the functionality of the expression-splitter-router in mule.<br />
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">Java Classes:</span></b><br />
<br />
<b><span class="Apple-style-span" style="color: blue;">Fruit:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> Fruit {
<span style="color: blue;">private</span> String shape;
<span style="color: blue;">public</span> String getShape() {
<span style="color: blue;">return</span> shape;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setShape(String shape) {
<span style="color: blue;">this</span>.shape = shape;
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">Apple:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Apple <span style="color: blue;">extends</span> Fruit {
<span style="color: blue;">private</span> String type;
<span style="color: blue;">public</span> String getType() {
<span style="color: blue;">return</span> type;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setType(String type) {
<span style="color: blue;">this</span>.type = type;
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">Banana:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Banana <span style="color: blue;">extends</span> Fruit {
<span style="color: blue;">private</span> String form;
<span style="color: blue;">public</span> String getForm() {
<span style="color: blue;">return</span> form;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setForm(String form) {
<span style="color: blue;">this</span>.form = form;
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">FruitBowl:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">import</span> java.util.ArrayList;
<span style="color: blue;">import</span> java.util.List;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> FruitBowl {
<span style="color: blue;">private</span> List<Fruit> fruit;
<span style="color: blue;">public</span> List<Fruit> getFruit() {
<span style="color: blue;">return</span> fruit;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setFruit(List<Fruit> fruit) {
<span style="color: blue;">this</span>.fruit = fruit;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> addFruit(Fruit fruit){
<span style="color: blue;">if</span> (<span style="color: blue;">this</span>.fruit == <span style="color: blue;">null</span>) {
<span style="color: blue;">this</span>.fruit = <span style="color: blue;">new</span> ArrayList<Fruit>();
}
<span style="color: blue;">this</span>.fruit.add(fruit);
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">CreateFruitComponent:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> CreateFruitComponent {
<span style="color: blue;">public</span> FruitBowl createFruit(String start) {
FruitBowl bowl = <span style="color: blue;">new</span> FruitBowl();
Apple apple = <span style="color: blue;">new</span> Apple();
apple.setShape(<span style="color: maroon;">"Round"</span>);
apple.setType(<span style="color: maroon;">"Green"</span>);
Banana banana = <span style="color: blue;">new</span> Banana();
banana.setShape(<span style="color: maroon;">"Lengthy"</span>);
banana.setForm(<span style="color: maroon;">"Yellow"</span>);
bowl.addFruit(apple);
bowl.addFruit(banana);
apple = <span style="color: blue;">new</span> Apple();
apple.setShape(<span style="color: maroon;">"Rectangular Square"</span>);
apple.setType(<span style="color: maroon;">"Red"</span>);
bowl.addFruit(apple);
<span style="color: blue;">return</span> bowl;
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">AppleComponent:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> AppleComponent {
<span style="color: blue;">public</span> <span style="color: blue;">void</span> displayApple(Apple apple) {
System.out.println(<span style="color: maroon;">"Apple Component"</span>);
System.out.println(apple.getShape() + <span style="color: maroon;">","</span> + apple.getType());
}
}
</pre>
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: blue;">BananaComponent:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressionsplitterrouter;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> BananaComponent {
<span style="color: blue;">public</span> <span style="color: blue;">void</span> displayBanana(Banana banana) {
System.out.println(<span style="color: maroon;">"BananaComponent"</span>);
System.out.println(banana.getShape() + <span style="color: maroon;">","</span> + banana.getForm());
}
}
</pre>
<b><span class="Apple-style-span" style="color: #cc0000;"><br /></span></b><br />
<b><span class="Apple-style-span" style="color: #cc0000;">Mule-config.xml:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span>="<span style="color: blue;">UTF-8</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span><span style="color: maroon;">mule</span> <span style="color: red;">xmlns</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/core/2.2</span>"
<span style="color: red;">xmlns:xsi</span>="<span style="color: blue;">http://www.w3.org/2001/XMLSchema-instance</span>" <span style="color: red;">xmlns:stdio</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/stdio/2.2</span>"
<span style="color: red;">xmlns:vm</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/vm/2.2</span>"
<span style="color: red;">xsi:schemaLocation</span>="
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:connector</span> <span style="color: red;">name</span>="<span style="color: blue;">stdioIN</span>" <span style="color: red;">promptMessage</span>="<span style="color: blue;">Press any key to continue</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">model</span> <span style="color: red;">name</span>="<span style="color: blue;">ExpressionSplitterRouterModel</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">CreateMsgService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:inbound-endpoint</span>
<span style="color: red;">connector-ref</span>="<span style="color: blue;">stdioIN</span>" <span style="color: red;">system</span>="<span style="color: blue;">IN</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span>
<span style="color: red;">class</span>="<span style="color: blue;">com.expressionsplitterrouter.CreateFruitComponent</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">expression-splitter-router</span>
<span style="color: red;">expression</span>="<span style="color: blue;">fruit</span>" <span style="color: red;">evaluator</span>="<span style="color: blue;">bean</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:outbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">banana-channel</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">payload-type-filter</span>
<span style="color: red;">expectedType</span>="<span style="color: blue;">com.expressionsplitterrouter.Banana</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">vm:outbound-endpoint</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:outbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">apple-channel</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">payload-type-filter</span>
<span style="color: red;">expectedType</span>="<span style="color: blue;">com.expressionsplitterrouter.Apple</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">vm:outbound-endpoint</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">expression-splitter-router</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">BananaService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:inbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">banana-channel</span>"/<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span> <span style="color: red;">class</span>="<span style="color: blue;">com.expressionsplitterrouter.BananaComponent</span>"/<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">AppleService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:inbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">apple-channel</span>"/<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span> <span style="color: red;">class</span>="<span style="color: blue;">com.expressionsplitterrouter.AppleComponent</span>"/<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">model</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">mule</span><span style="color: blue;">></span>
</pre>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-80201600344505526742011-10-03T09:10:00.000-07:002011-10-03T09:10:04.057-07:00Mule - Expression recipient list router<div dir="ltr" style="text-align: left;" trbidi="on">
The following example is on the usage of the <b><span class="Apple-style-span" style="color: blue;">expression-recipient-list-router</span></b>. This router can be used to extract the endpoints from the message and route it to the corresponding component.<br />
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">Java Classes:</span></b><br />
<br />
<b><span class="Apple-style-span" style="color: blue;">CreateXmlMessageComponent:</span></b><br />
<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressrecipienttransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> CreateXmlMessageComponent {
<span style="color: blue;">public</span> String createMessage(String msg) {
StringBuilder sb = <span style="color: blue;">new</span> StringBuilder();
sb.append(<span style="color: maroon;">"<message>"</span>);
sb.append(<span style="color: maroon;">"<recipientList>"</span>);
sb.append(<span style="color: maroon;">"<recipient>vm://display-message-channel</recipient>"</span>);
sb.append(<span style="color: maroon;">"<recipient>vm://compute-message-channel</recipient>"</span>);
sb.append(<span style="color: maroon;">"</recipientList>"</span>);
sb.append(<span style="color: maroon;">"<info>"</span>);
sb.append(<span style="color: maroon;">"Hi there"</span>);
sb.append(<span style="color: maroon;">"</info>"</span>);
sb.append(<span style="color: maroon;">"</message>"</span>);
<span style="color: blue;">return</span> sb.toString();
}
}
</pre>
<br />
<b><span class="Apple-style-span" style="color: blue;">DisplayMessageComponent:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressrecipienttransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> DisplayMessageComponent {
<span style="color: blue;">public</span> <span style="color: blue;">void</span> displayMessage(String msg) {
System.out.println(<span style="color: maroon;">"Into displaymessage"</span>);
System.out.println(msg);
}
}
</pre>
<br />
<b><span class="Apple-style-span" style="color: blue;">ComputeMsgComponent:
</span></b><br />
<b><span class="Apple-style-span" style="color: blue;"><br /></span></b><br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressrecipienttransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> ComputeMsgComponent {
<span style="color: blue;">public</span> <span style="color: blue;">void</span> compute(String msg) {
System.out.println(<span style="color: maroon;">"Into compute component"</span>);
System.out.println(msg.length());
}
}
</pre>
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">mule-config.xml:
<br />
</span></b><br />
<b><span class="Apple-style-span" style="color: #cc0000;"><br /></span></b><br />
<pre class="source-code"><b><span class="Apple-style-span" style="color: #cc0000;"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span>="<span style="color: blue;">UTF-8</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span><span style="color: maroon;">mule</span> <span style="color: red;">xmlns</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/core/2.2</span>"
<span style="color: red;">xmlns:xsi</span>="<span style="color: blue;">http://www.w3.org/2001/XMLSchema-instance</span>" <span style="color: red;">xmlns:stdio</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/stdio/2.2</span>"
<span style="color: red;">xmlns:vm</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/vm/2.2</span>"
<span style="color: red;">xsi:schemaLocation</span>="
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:connector</span> <span style="color: red;">name</span>="<span style="color: blue;">stdioIN</span>" <span style="color: red;">promptMessage</span>="<span style="color: blue;">Press any key to continue</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">model</span> <span style="color: red;">name</span>="<span style="color: blue;">expression-recipient-list-model</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">expression-recipient-list-serviec</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:inbound-endpoint</span>
<span style="color: red;">connector-ref</span>="<span style="color: blue;">stdioIN</span>" <span style="color: red;">system</span>="<span style="color: blue;">IN</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span>
<span style="color: red;">class</span>="<span style="color: blue;">com.expressrecipienttransformers.CreateXmlMessageComponent</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">component</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<u><span style="color: blue;"><</span><span style="color: maroon;">expression-recipient-list-router</span></u>
<u><span style="color: red;">expression</span>="<span style="color: blue;">/message/recipientList/recipient</span>"</u>
<u><span style="color: red;">evaluator</span>="<span style="color: blue;">xpath</span>"</u> /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">DisplayService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:inbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">display-message-channel</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span>
<span style="color: red;">class</span>="<span style="color: blue;">com.expressrecipienttransformers.DisplayMessageComponent</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">ComputeService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:inbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">compute-message-channel</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span>
<span style="color: red;">class</span>="<span style="color: blue;">com.expressrecipienttransformers.ComputeMsgComponent</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">model</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">mule</span>
</span></b></pre>
<b><span class="Apple-style-span" style="color: #cc0000;">
</span></b></div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-78356809543922535312011-09-30T10:31:00.000-07:002011-09-30T10:32:33.293-07:00Mule Expression Transformers<div dir="ltr" style="text-align: left;" trbidi="on">
<b><i><span class="Apple-style-span" style="color: blue;">Java Classes:</span></i></b><br />
<br />
<span class="Apple-style-span" style="color: #cc0000;"><b>GameData:</b> </span><br />
<br />
A simple POJO object.
<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressiontransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> GameData {
<span style="color: blue;">private</span> String gameName;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> year;
<span style="color: blue;">public</span> String getGameName() {
<span style="color: blue;">return</span> gameName;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setGameName(String gameName) {
<span style="color: blue;">this</span>.gameName = gameName;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getYear() {
<span style="color: blue;">return</span> year;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setYear(<span style="color: blue;">int</span> year) {
<span style="color: blue;">this</span>.year = year;
}
}
</pre>
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">GameInfo:</span></b><br />
<br />
Another simple POJO that contains the GameData Object.<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressiontransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> GamingInfo {
<span style="color: blue;">private</span> <span style="color: blue;">int</span> gameId;
<span style="color: blue;">private</span> GameData gameData;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getGameId() {
<span style="color: blue;">return</span> gameId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setGameId(<span style="color: blue;">int</span> gameId) {
<span style="color: blue;">this</span>.gameId = gameId;
}
<span style="color: blue;">public</span> GameData getGameData() {
<span style="color: blue;">return</span> gameData;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setGameData(GameData gameData) {
<span style="color: blue;">this</span>.gameData = gameData;
}
}
</pre>
<br />
<br />
Here we will be using two component classes. One is used to generate the GameInfo Object and the other is used to print the values.
<br />
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">CreateGamingInfoComponent:</span></b>
<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressiontransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> CreateGamingInfoComponent {
<span style="color: blue;">public</span> GamingInfo create(String msg) {
GamingInfo info = <span style="color: blue;">new</span> GamingInfo();
info.setGameId(<span style="color: maroon;">1</span>);
GameData data = <span style="color: blue;">new</span> GameData();
data.setGameName(<span style="color: maroon;">"Cricket"</span>);
data.setYear(<span style="color: maroon;">2010</span>);
info.setGameData(data);
<span style="color: blue;">return</span> info;
}
}
</pre>
<br />
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">GamingComponent:</span></b>
<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> com.expressiontransformers;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> GamingComponent {
<span style="color: blue;">public</span> <span style="color: blue;">void</span> addGame(Integer gameId, GameData data) {
System.out.println(gameId);
System.out.println(data.getGameName());
System.out.println(data.getYear());
}
}
</pre>
<br />
<b><span class="Apple-style-span" style="color: #cc0000;">Mule-config.xml:</span></b>
<br />
<br />
The mule-config.xml uses the expression-transformer in the inbound-endpoint to convert the input GameInfo object to the required values of the GamingComponent.
<br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span>="<span style="color: blue;">UTF-8</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span><span style="color: maroon;">mule</span> <span style="color: red;">xmlns</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/core/2.2</span>"
<span style="color: red;">xmlns:xsi</span>="<span style="color: blue;">http://www.w3.org/2001/XMLSchema-instance</span>" <span style="color: red;">xmlns:stdio</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/stdio/2.2</span>"
<span style="color: red;">xmlns:vm</span>="<span style="color: blue;">http://www.mulesource.org/schema/mule/vm/2.2</span>"
<span style="color: red;">xsi:schemaLocation</span>="
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:connector</span> <span style="color: red;">name</span>="<span style="color: blue;">stdioIN</span>" <span style="color: red;">promptMessage</span>="<span style="color: blue;">Press any key to Continue</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">model</span> <span style="color: red;">name</span>="<span style="color: blue;">RestaurantServiceE</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">ExpressionFilterService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">stdio:inbound-endpoint</span> <span style="color: red;">system</span>="<span style="color: blue;">IN</span>"
<span style="color: red;">connector-ref</span>="<span style="color: blue;">stdioIN</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span>
<span style="color: red;">class</span>="<span style="color: blue;">com.expressiontransformers.CreateGamingInfoComponent</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">pass-through-router</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:outbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">displayGamingInfo-channel</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">pass-through-router</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">outbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">service</span> <span style="color: red;">name</span>="<span style="color: blue;">DisplayInfoService</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">vm:inbound-endpoint</span> <span style="color: red;">path</span>="<span style="color: blue;">displayGamingInfo-channel</span>"<span style="color: blue;">></span>
<b><span style="color: blue;"><</span><span style="color: maroon;"><u>expression-transformer</u></span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">return-argument</span> <span style="color: red;">expression</span>="<span style="color: blue;">gameId</span>"
<span style="color: red;">evaluator</span>="<span style="color: blue;">bean</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">return-argument</span> <span style="color: red;">expression</span>="<span style="color: blue;">gameData</span>"
<span style="color: red;">evaluator</span>="<span style="color: blue;">bean</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;"><u>expression-transformer</u></span><span style="color: blue;">></span></b>
<span style="color: blue;"><</span>/<span style="color: maroon;">vm:inbound-endpoint</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">inbound</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">component</span> <span style="color: red;">class</span>="<span style="color: blue;">com.expressiontransformers.GamingComponent</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">service</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">model</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">mule</span><span style="color: blue;">></span>
</pre>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-38197119337348124872011-09-26T13:20:00.001-07:002011-09-26T13:20:54.080-07:00Log4j, Mx4j and JMX JConsole Configuration in Mule
Sample Configuration File:
<pre class="source-code">
<span style=' color: Blue;'><?</span><span style=' color: Maroon;'>xml</span> <span style=' color: Red;'>version</span>="<span style=' color: Blue;'>1.0</span>" <span style=' color: Red;'>encoding</span>="<span style=' color: Blue;'>UTF-8</span>"<span style=' color: Blue;'>?></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule</span> <span style=' color: Red;'>xmlns</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/core/2.2</span>"
<span style=' color: Red;'>xmlns:xsi</span>="<span style=' color: Blue;'>http://www.w3.org/2001/XMLSchema-instance</span>" <span style=' color: Red;'>xmlns:http</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/http/2.2</span>"
<span style=' color: Red;'>xmlns:https</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/https/2.2</span>"
<span style=' color: Red;'>xmlns:saaj</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/saaj/2.2</span>"
<span style=' color: Red;'>xmlns:vm</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/vm/2.2</span>"
<span style=' color: Red;'>xmlns:mule-xml</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/xml/2.2</span>"
<span style=' color: Red;'>xmlns:management</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/management/2.2</span>"
<span style=' color: Red;'>xmlns:spring</span>="<span style=' color: Blue;'>http://www.springframework.org/schema/beans</span>"
<span style=' color: Red;'>xsi:schemaLocation</span>="
http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
http://www.mulesource.org/schema/mule/https/2.2 http://www.mulesource.org/schema/mule/https/2.2/mule-https.xsd
http://www.mulesource.org/schema/mule/saaj/2.2 http://www.mulesource.org/schema/mule/saaj/2.2/mule-saaj.xsd
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd
http://www.mulesource.org/schema/mule/management/2.2 http://www.mulesource.org/schema/mule/management/2.2/mule-management.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!--
Mule Management Console Configurations START............
Register the Mx4j Console...............................
Register the JMX Console................................
Register Log4j Console..................................
--<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:jmx-default-config</span> <span style=' color: Red;'>port</span>="<span style=' color: Blue;'>1098</span>"
<span style=' color: Red;'>registerMx4jAdapter</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:credentials</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>spring:entry</span> <span style=' color: Red;'>key</span>="<span style=' color: Blue;'>vijay</span>" <span style=' color: Red;'>value</span>="<span style=' color: Blue;'>vijay123</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>management:credentials</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>management:jmx-default-config</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:jmx-log4j</span> /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!--
Mule Management Console Configurations END.
--<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>mule</span><span style=' color: Blue;'>></span>
</pre>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-57623845955995779062011-09-26T13:17:00.000-07:002011-09-26T13:17:56.029-07:00Handling SOAP Fault with Http Web Service<div dir="ltr" style="text-align: left;" trbidi="on">
The sample XML configuration file:<br />
<br />
<pre class="source-code">
<span style=' color: Blue;'><?</span><span style=' color: Maroon;'>xml</span> <span style=' color: Red;'>version</span>="<span style=' color: Blue;'>1.0</span>" <span style=' color: Red;'>encoding</span>="<span style=' color: Blue;'>UTF-8</span>"<span style=' color: Blue;'>?></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule</span> <span style=' color: Red;'>xmlns</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/core/2.2</span>"
<span style=' color: Red;'>xmlns:xsi</span>="<span style=' color: Blue;'>http://www.w3.org/2001/XMLSchema-instance</span>" <span style=' color: Red;'>xmlns:http</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/http/2.2</span>"
<span style=' color: Red;'>xmlns:https</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/https/2.2</span>"
<span style=' color: Red;'>xmlns:saaj</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/saaj/2.2</span>"
<span style=' color: Red;'>xmlns:vm</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/vm/2.2</span>"
<span style=' color: Red;'>xmlns:mule-xml</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/xml/2.2</span>"
<span style=' color: Red;'>xmlns:management</span>="<span style=' color: Blue;'>http://www.mulesource.org/schema/mule/management/2.2</span>"
<span style=' color: Red;'>xmlns:spring</span>="<span style=' color: Blue;'>http://www.springframework.org/schema/beans</span>"
<span style=' color: Red;'>xsi:schemaLocation</span>="
http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
http://www.mulesource.org/schema/mule/https/2.2 http://www.mulesource.org/schema/mule/https/2.2/mule-https.xsd
http://www.mulesource.org/schema/mule/saaj/2.2 http://www.mulesource.org/schema/mule/saaj/2.2/mule-saaj.xsd
http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
http://www.mulesource.org/schema/mule/xml/2.2 http://www.mulesource.org/schema/mule/xml/2.2/mule-xml.xsd
http://www.mulesource.org/schema/mule/management/2.2 http://www.mulesource.org/schema/mule/management/2.2/mule-management.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!--
Mule Management Console Configurations START............
Register the Mx4j Console...............................
Register the JMX Console................................
Register Log4j Console..................................
--<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:jmx-default-config</span> <span style=' color: Red;'>port</span>="<span style=' color: Blue;'>1098</span>"
<span style=' color: Red;'>registerMx4jAdapter</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:credentials</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>spring:entry</span> <span style=' color: Red;'>key</span>="<span style=' color: Blue;'>vijay</span>" <span style=' color: Red;'>value</span>="<span style=' color: Blue;'>vijay123</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>management:credentials</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>management:jmx-default-config</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>management:jmx-log4j</span> /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!--
Mule Management Console Configurations END.
--<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>spring:beans</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>spring:import</span> <span style=' color: Red;'>resource</span>="<span style=' color: Blue;'>transformers.xml</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>spring:beans</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>custom-transformer</span>
<span style=' color: Red;'>class</span>="<span style=' color: Blue;'>com.restaurantsrevice.transformers.DocumentToSOAPFaultTransformer</span>"
<span style=' color: Red;'>name</span>="<span style=' color: Blue;'>faultTransformer</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>spring:property</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>propagateHeaders</span>" <span style=' color: Red;'>value</span>="<span style=' color: Blue;'>false</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>custom-transformer</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>model</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>RestaurantServiceModel</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Default Service Exception Strategy --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>default-service-exception-strategy</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>exception-channel</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>default-service-exception-strategy</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>service</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>RestaurantService</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>http:inbound-endpoint</span>
<span style=' color: Red;'>address</span>="<span style=' color: Blue;'>http://localhost:8080/RestaurantService</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform Incoming Soap Message to XML String --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>saaj:soap-message-to-document-transformer</span> /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule-xml:dom-to-xml-transformer</span>
<span style=' color: Red;'>returnClass</span>="<span style=' color: Blue;'>java.lang.String</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>http:inbound-endpoint</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>pass-through-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>router-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>pass-through-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>async-reply</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:inbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>success-response-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:inbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>exception-response-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>single-async-reply-router</span> /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>async-reply</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>service</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>service</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>RouterService</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:inbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>router-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>log-component</span> /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>request-processing-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span> <span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>addFoodRequestTransformer</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>vm:outbound-endpoint</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>wildcard-filter</span> <span style=' color: Red;'>pattern</span>="<span style=' color: Blue;'>*AddFoodRequest*</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>service</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>service</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>RequestProcessingService</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:inbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>request-processing-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>component</span>
<span style=' color: Red;'>class</span>="<span style=' color: Blue;'>org.restaurantservice.RestaurantService</span>"<span style=' color: Blue;'>></span><span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>component</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>success-response-channel</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span> <span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>addFoodResponseTransformer</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform XML to SOAP response --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule-xml:xml-to-dom-transformer</span>
<span style=' color: Red;'>returnClass</span>="<span style=' color: Blue;'>org.w3c.dom.Document</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>saaj:document-to-soap-message-transformer</span>
<span style=' color: Red;'>propagateHeaders</span>="<span style=' color: Blue;'>false</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>vm:outbound-endpoint</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>payload-type-filter</span>
<span style=' color: Red;'>expectedType</span>="<span style=' color: Blue;'>com.services.restaurantservice.AddFoodResponse</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>service</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>service</span> <span style=' color: Red;'>name</span>="<span style=' color: Blue;'>ExceptionService</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:inbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>exception-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>inbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>component</span>
<span style=' color: Red;'>class</span>="<span style=' color: Blue;'>com.services.restaurantservice.components.ExceptionHandler</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>exception-response-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span>
<span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>FoodAdditionFailedExceptionMap</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform to Document Object --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule-xml:xml-to-dom-transformer</span>
<span style=' color: Red;'>returnClass</span>="<span style=' color: Blue;'>org.w3c.dom.Document</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform to SOAP Fault --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span> <span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>faultTransformer</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>vm:outbound-endpoint</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>payload-type-filter</span>
<span style=' color: Red;'>expectedType</span>="<span style=' color: Blue;'>com.services.restaurantservice.webservice.FoodAdditionFailed</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>vm:outbound-endpoint</span> <span style=' color: Red;'>path</span>="<span style=' color: Blue;'>exception-response-channel</span>"
<span style=' color: Red;'>synchronous</span>="<span style=' color: Blue;'>true</span>"<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span> <span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>IncompleteRequestExceptionMap</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform to Document Object --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>mule-xml:xml-to-dom-transformer</span>
<span style=' color: Red;'>returnClass</span>="<span style=' color: Blue;'>org.w3c.dom.Document</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>!-- Transform to SOAP Fault --<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>transformer</span> <span style=' color: Red;'>ref</span>="<span style=' color: Blue;'>faultTransformer</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>transformers</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>vm:outbound-endpoint</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span><span style=' color: Maroon;'>payload-type-filter</span>
<span style=' color: Red;'>expectedType</span>="<span style=' color: Blue;'>com.services.restaurantservice.webservice.IncompleteRequest</span>" /<span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>filtering-router</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>outbound</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>service</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>model</span><span style=' color: Blue;'>></span>
<span style=' color: Blue;'><</span>/<span style=' color: Maroon;'>mule</span><span style=' color: Blue;'>></span>
</pre>
<b>Java Code to transform to Soap Fault:</b>
<pre class="source-code">
<span style=' color: Blue;'>package</span> com.restaurantsrevice.transformers;
<span style=' color: Blue;'>import</span> javax.xml.namespace.QName;
<span style=' color: Blue;'>import</span> javax.xml.soap.Detail;
<span style=' color: Blue;'>import</span> javax.xml.soap.MessageFactory;
<span style=' color: Blue;'>import</span> javax.xml.soap.Name;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPBody;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPException;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPFactory;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPFault;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPHeader;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPHeaderElement;
<span style=' color: Blue;'>import</span> javax.xml.soap.SOAPMessage;
<span style=' color: Blue;'>import</span> org.mule.api.MuleMessage;
<span style=' color: Blue;'>import</span> org.mule.api.transformer.TransformerException;
<span style=' color: Blue;'>import</span> org.mule.module.saaj.SaajUtils;
<span style=' color: Blue;'>import</span> org.mule.module.saaj.i18n.SaajMessages;
<span style=' color: Blue;'>import</span> org.mule.transformer.AbstractMessageAwareTransformer;
<span style=' color: Blue;'>import</span> org.w3c.dom.Document;
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>class</span> DocumentToSOAPFaultTransformer <span style=' color: Blue;'>extends</span>
AbstractMessageAwareTransformer {
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>boolean</span> propagateHeaders = <span style=' color: Maroon;'>true</span>;
<span style=' color: Blue;'>private</span> String headerURI = <span style=' color: Maroon;'>"http://www.mulesource.org/schema/mule/saaj/2.2"</span>;
<span style=' color: Blue;'>private</span> String headerPrefix = <span style=' color: Maroon;'>"mule-saaj"</span>;
<span style=' color: Blue;'>private</span> SOAPFactory soapFactory;
<span style=' color: Blue;'>private</span> MessageFactory messageFactory;
<span style=' color: Blue;'>public</span> DocumentToSOAPFaultTransformer() <span style=' color: Blue;'>throws</span> Exception {
soapFactory = SOAPFactory.newInstance();
messageFactory = MessageFactory.newInstance();
}
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>void</span> setPropagateHeaders(<span style=' color: Blue;'>boolean</span> propagateHeaders) {
<span style=' color: Blue;'>this</span>.propagateHeaders = propagateHeaders;
}
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>void</span> setHeaderURI(String headerURI) {
<span style=' color: Blue;'>this</span>.headerURI = headerURI;
}
<span style=' color: Blue;'>public</span> <span style=' color: Blue;'>void</span> setHeaderPrefix(String headerPrefix) {
<span style=' color: Blue;'>this</span>.headerPrefix = headerPrefix;
}
<span style=' color: Blue;'>public</span> Object transform(MuleMessage muleMessage, String s)
<span style=' color: Blue;'>throws</span> TransformerException {
Document document = (Document) muleMessage.getPayload();
SOAPMessage soapMessage;
<span style=' color: Blue;'>try</span> {
soapMessage = messageFactory.createMessage();
SOAPBody body = soapMessage.getSOAPBody();
addFault(body, document);
<span style=' color: Blue;'>if</span> (propagateHeaders) {
propagateHeaders(muleMessage, soapMessage);
}
soapMessage.saveChanges();
} <span style=' color: Blue;'>catch</span> (SOAPException ex) {
<span style=' color: Blue;'>throw</span> <span style=' color: Blue;'>new</span> TransformerException(SaajMessages
.failedToBuildSOAPMessage());
}
<span style=' color: Blue;'>if</span> (logger.isDebugEnabled()) {
logger.debug(<span style=' color: Maroon;'>"Transformation result: "</span>
+ SaajUtils.getSOAPMessageAsString(soapMessage));
}
<span style=' color: Blue;'>return</span> SaajUtils.getSOAPMessageAsBytes(soapMessage);
}
<span style=' color: Blue;'>void</span> propagateHeaders(MuleMessage muleMessage, SOAPMessage soapMessage)
<span style=' color: Blue;'>throws</span> SOAPException {
<span style=' color: Blue;'>for</span> (Object n : muleMessage.getPropertyNames()) {
String propertyName = (String) n;
SOAPHeader header = soapMessage.getSOAPHeader();
Name name = soapFactory.createName(propertyName, headerPrefix,
headerURI);
SOAPHeaderElement headerElement = header.addHeaderElement(name);
headerElement.addTextNode(muleMessage.getProperty(propertyName)
.toString());
}
}
<span style=' color: Blue;'>private</span> <span style=' color: Blue;'>void</span> addFault(SOAPBody soapBody, Document document)
<span style=' color: Blue;'>throws</span> SOAPException {
org.w3c.dom.Element docElement = document.getDocumentElement();
QName qName = <span style=' color: Blue;'>new</span> QName(docElement.getLocalName());
SOAPFault soapFault = soapBody.addFault(qName, docElement
.getTextContent());
Detail detail = soapFault.addDetail();
detail.addChildElement(soapFactory.createElement(docElement));
}
}
</pre>
</div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-25673079598604910702011-09-25T20:07:00.000-07:002011-09-25T20:07:22.390-07:00Customization of WSDL using bindings.xml<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span>="<span style="color: blue;">UTF-8</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span><span style="color: maroon;">jaxws:bindings</span> <span style="color: red;">wsdlLocation</span>="<span style="color: blue;">RestaurantService.wsdl</span>"
<span style="color: red;">xmlns:jaxws</span>="<span style="color: blue;">http://java.sun.com/xml/ns/jaxws</span>" <span style="color: red;">xmlns:xsd</span>="<span style="color: blue;">http://www.w3.org/2001/XMLSchema</span>"
<span style="color: red;">xmlns:jaxb</span>="<span style="color: blue;">http://java.sun.com/xml/ns/jaxb</span>" <span style="color: red;">xmlns:wsdl</span>="<span style="color: blue;">http://schemas.xmlsoap.org/wsdl/</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">jaxws:bindings</span> <span style="color: red;">node</span>="<span style="color: blue;">wsdl:definitions/wsdl:types/xsd:schema</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">jaxb:globalBindings</span> <span style="color: red;">choiceContentProperty</span>="<span style="color: blue;">true</span>"
<span style="color: red;">collectionType</span>="<span style="color: blue;">java.util.Set</span>"
<span style="color: red;">generateIsSetMethod</span>="<span style="color: blue;">true</span>"
<span style="color: red;">enableJavaNamingConventions</span>="<span style="color: blue;">true</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span>!--
<span style="color: blue;"><</span><span style="color: maroon;">jaxb:javaType</span> <span style="color: red;">name</span>="<span style="color: blue;">java.util.Date</span>"
<span style="color: red;">xmlType</span>="<span style="color: blue;">xs:dateTime</span>"
<span style="color: red;">parseMethod</span>="<span style="color: blue;">org.apache.cxf.tools.common.DataTypeAdapter.parseDateTime</span>"
<span style="color: red;">printMethod</span>="<span style="color: blue;">org.apache.cxf.tools.common.DataTypeAdapter.printDateTime</span>"/<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">jaxb:javaType</span> <span style="color: red;">name</span>="<span style="color: blue;">java.util.Date</span>" <span style="color: red;">xmlType</span>="<span style="color: blue;">xs:date</span>"
<span style="color: red;">parseMethod</span>="<span style="color: blue;">org.apache.cxf.tools.common.DataTypeAdapter.parseDate</span>"
<span style="color: red;">printMethod</span>="<span style="color: blue;">org.apache.cxf.tools.common.DataTypeAdapter.printDate</span>"/<span style="color: blue;">></span>
--<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">jaxb:globalBindings</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">jaxws:bindings</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">jaxws:bindings</span><span style="color: blue;">></span>
</pre>
<br />
<br />
<a href="http://confluence.highsource.org/display/HJ3/Apache+CXF+Tutorial+-+Building+JAX-WS,+JAXB+and+JPA-based+web+service+with+Apache+CXF,+Spring+and+Hyperjaxb3">Reference Link</a><br />
<br /></div>
Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-68349954006115525752011-08-29T07:24:00.000-07:002011-08-29T07:24:41.642-07:00MULE HTTP Web Service - Step2A. Creating the client jars from the WSDL with Options<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">This step focuses on creating the client jars from the WSDL created in the previous step. This serves two purposes.</span></div><ol style="text-align: left;"><li style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">To verify that the WSDL and the XSD was created without any errors.</span></li>
<li style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">To get the jars ready so that we will have the class files generated for the objects defined in XSD. These classes will be used in our Mule web service.</span></li>
</ol><div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">This example focuses on using the Maven cxf-codegen-plugin to generate the classes.</span></div></div><div><br />
</div><div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Since we are going to develop a Mule Web Service using maven, we can easily import the client jar in your pom.xml</span></div></div><div><br />
</div><div><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>1.</b> Create a simple maven project in Eclipse.</span></div><div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn4AJ9t8nmUbiuP0CmoikOfmVM21mJwGAXUlIYGEt-cNOXxL7DTXDQGC4VWo3y_lMyaZKXefhmIBvY8nRWf5iOPlSM6PwN6RvtqykpdcAThUd1uorkWAiWTHiFiNV6fawG256BSA44IhA/s1600/ShippingServiceClient.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="574" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn4AJ9t8nmUbiuP0CmoikOfmVM21mJwGAXUlIYGEt-cNOXxL7DTXDQGC4VWo3y_lMyaZKXefhmIBvY8nRWf5iOPlSM6PwN6RvtqykpdcAThUd1uorkWAiWTHiFiNV6fawG256BSA44IhA/s640/ShippingServiceClient.jpg" width="640" /></a></div><br />
</div><div class="separator" style="clear: both; text-align: center;"></div><div><br />
</div><div><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>2.</b> Add the cxf-codegen-plugin to your pom.xml</span></div></div><br />
<pre class="source-code"><span style="color: blue;"><</span><span style="color: maroon;">project</span> <span style="color: red;">xmlns</span>="<span style="color: blue;">http://maven.apache.org/POM/4.0.0</span>" <span style="color: red;">xmlns:xsi</span>="<span style="color: blue;">http://www.w3.org/2001/XMLSchema-instance</span>"
<span style="color: red;">xsi:schemaLocation</span>="<span style="color: blue;">http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">modelVersion</span><span style="color: blue;">></span>4.0.0<span style="color: blue;"><</span>/<span style="color: maroon;">modelVersion</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">groupId</span><span style="color: blue;">></span>org.hello<span style="color: blue;"><</span>/<span style="color: maroon;">groupId</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">artifactId</span><span style="color: blue;">></span>HelloServiceClient<span style="color: blue;"><</span>/<span style="color: maroon;">artifactId</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">version</span><span style="color: blue;">></span>1.0-SNAPSHOT<span style="color: blue;"><</span>/<span style="color: maroon;">version</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">packaging</span><span style="color: blue;">></span>jar<span style="color: blue;"><</span>/<span style="color: maroon;">packaging</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">build</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">plugins</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">plugin</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">groupId</span><span style="color: blue;">></span>org.apache.cxf<span style="color: blue;"><</span>/<span style="color: maroon;">groupId</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">artifactId</span><span style="color: blue;">></span>cxf-codegen-plugin<span style="color: blue;"><</span>/<span style="color: maroon;">artifactId</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">version</span><span style="color: blue;">></span>2.4.0<span style="color: blue;"><</span>/<span style="color: maroon;">version</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">executions</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">execution</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span><span style="color: blue;">></span>generate-sources<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">phase</span><span style="color: blue;">></span>generate-sources<span style="color: blue;"><</span>/<span style="color: maroon;">phase</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">configuration</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">sourceRoot</span><span style="color: blue;">></span>${project.build.directory}/generated/cxf<span style="color: blue;"><</span>/<span style="color: maroon;">sourceRoot</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">wsdlOptions</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">wsdlOption</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">extraargs</span><span style="color: blue;">></span>
<b><i><span style="color: blue;"><</span><span style="color: maroon;">extraarg</span><span style="color: blue;">></span>-p<span style="color: blue;"><</span>/<span style="color: maroon;">extraarg</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">extraarg</span><span style="color: blue;">></span>http://hello.services.com=com.services.hello.helloservice<span style="color: blue;"><</span>/<span style="color: maroon;">extraarg</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">extraarg</span><span style="color: blue;">></span>-p<span style="color: blue;"><</span>/<span style="color: maroon;">extraarg</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">extraarg</span><span style="color: blue;">></span>http://ws.hello.services.com=com.services.hello.helloservice.ws<span style="color: blue;"><</span>/<span style="color: maroon;">extraarg</span><span style="color: blue;">></span></i></b>
<span style="color: blue;"><</span>/<span style="color: maroon;">extraargs</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">wsdl</span><span style="color: blue;">></span>${basedir}/src/main/resources/Hello.wsdl<span style="color: blue;"><</span>/<span style="color: maroon;">wsdl</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">bindingFiles</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">bindingFile</span><span style="color: blue;">></span>${basedir}/src/main/resources/bindings.xml<span style="color: blue;"><</span>/<span style="color: maroon;">bindingFile</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">bindingFiles</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">wsdlOption</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">wsdlOptions</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">configuration</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">goals</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">goal</span><span style="color: blue;">></span>wsdl2java<span style="color: blue;"><</span>/<span style="color: maroon;">goal</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">goals</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">execution</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">executions</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">plugin</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">plugins</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">build</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">project</span><span style="color: blue;">></span>
</pre><br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>3.</b> Move the WSDL and XSD created in the previous section under src/main/resources</span></div><br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>4.</b> Make sure that you have JDK in your project classpath and not JRE. This plugin needs to compile and generate the class files and it requires a JDK to do it.</span></div><br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;"><b>5.</b> Right click on the project, go to Run As and select Maven Package.</span></div><br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">With the above steps the client jar would get generated successfully and the client jar should contain the following structure.</span></div><br />
<div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUksPebTMCdRWzbQN3pNl2gr7KSQoezKkbt-AFSHMpZlbLCN7uwWOwTiEg2mRscn_98aLVDY1U4SS39iMnczFoOaGNoBCvuFZDUmfI8iV_NYyXW_VCJl7uYXbta6NhpXPHIKdvrVkfAok/s1600/ShippingServiceClient1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="348" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUksPebTMCdRWzbQN3pNl2gr7KSQoezKkbt-AFSHMpZlbLCN7uwWOwTiEg2mRscn_98aLVDY1U4SS39iMnczFoOaGNoBCvuFZDUmfI8iV_NYyXW_VCJl7uYXbta6NhpXPHIKdvrVkfAok/s640/ShippingServiceClient1.jpg" width="640" /></a></div><br />
</div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-17401373296212962372011-08-14T20:10:00.000-07:002011-08-14T20:10:38.802-07:00HIBERNATE - Bidirectional Many-to-Many Association<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the bidirectional Many-to-Many association using Hibernate. We will be using the Student and Professor table where a student will have one or more professors. The same professor will have one or more students.</span></div><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Schema Creation:</b></span><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Since many-to-many is not allowed on the database level (Normalization), we will change this to add a junction-entity table in the middle. I am using the name of the junction-entity table to have the combination of both the tables.</span></div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">The entities will be translated as follows:</span></div><br />
<table border="1"><tbody>
<tr> <th>STUDENT</th> <th>STUDPROF</th> <th>PROFESSOR</th> </tr>
<tr> <td>ID</td> <th>SID</th><td>ID</td> </tr>
<tr> <td>NAME</td> <th>PID</th><td>NAME</td> </tr>
<tr> <td>PHNO</td> <th><br />
</th><td>PHNO</td> </tr>
<tr> <td>ADDRESS</td> <th><br />
</th><td>DOB</td> </tr>
</tbody></table><br />
<pre class="source-code"><span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> STUDENT (
ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
ADDRESS <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
PHNO <span class="Apple-style-span" style="color: blue;">VARCHAR(10)</span> NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE </span>PROFESSOR (
ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
PHNO <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
DOB <span class="Apple-style-span" style="color: blue;">DATE </span>NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> STUDPROF (
SID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL DEFAULT 1,
PID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL DEFAULT 1,
<span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span>(SID, PID),
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (SID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>STUDENT (ID),
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (PID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>PROFESSOR (ID)
);
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">HBM Files Creation:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Professor.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Professor</span>" <span style="color: red;">table</span>="<span style="color: blue;">PROFESSOR</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PHNO</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">dob</span>" <span style="color: red;">column</span>="<span style="color: blue;">DOB</span>" <span style="color: red;">type</span>="<span style="color: blue;">date</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">set</span> <span style="color: red;">name</span>="<span style="color: blue;">students</span>" <span style="color: red;">table</span>="<span style="color: blue;">STUDPROF</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>" <span style="color: red;">lazy</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">key</span> <span style="color: red;">column</span>="<span style="color: blue;">PID</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">key</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">many-to-many</span> <span style="color: red;">column</span>="<span style="color: blue;">SID</span>" <span style="color: red;">class</span>="<span style="color: blue;">Student</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">many-to-many</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">set</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Student.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Student</span>" <span style="color: red;">table</span>="<span style="color: blue;">STUDENT</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">ID</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"
<span style="color: red;">update</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">address</span>" <span style="color: red;">column</span>="<span style="color: blue;">ADDRESS</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"
<span style="color: red;">update</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PHNO</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"
<span style="color: red;">update</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">set</span> <span style="color: red;">name</span>="<span style="color: blue;">professors</span>" <span style="color: red;">table</span>="<span style="color: blue;">STUDPROF</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>" <span style="color: red;">lazy</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">key</span> <span style="color: red;">column</span>="<span style="color: blue;">SID</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">key</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">many-to-many</span> <span style="color: red;">column</span>="<span style="color: blue;">PID</span>" <span style="color: red;">class</span>="<span style="color: blue;">Professor</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">many-to-many</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">set</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Java Files:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Professor.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">import</span> java.util.Date;
<span style="color: blue;">import</span> java.util.Set;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Professor <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = <span style="color: maroon;">2286787316913029844L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> String phNo;
<span style="color: blue;">private</span> Date dob;
<span style="color: blue;">private</span> Set<Student> students;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> String getPhNo() {
<span style="color: blue;">return</span> phNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhNo(String phNo) {
<span style="color: blue;">this</span>.phNo = phNo;
}
<span style="color: blue;">public</span> Date getDob() {
<span style="color: blue;">return</span> dob;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setDob(Date dob) {
<span style="color: blue;">this</span>.dob = dob;
}
<span style="color: blue;">public</span> Set<Student> getStudents() {
<span style="color: blue;">return</span> students;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setStudents(Set<Student> students) {
<span style="color: blue;">this</span>.students = students;
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Student.java:</span><br />
<br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.util.Set;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Student {
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> String address;
<span style="color: blue;">private</span> String phNo;
<span style="color: blue;">private</span> Set<Professor> professors;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> String getAddress() {
<span style="color: blue;">return</span> address;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddress(String address) {
<span style="color: blue;">this</span>.address = address;
}
<span style="color: blue;">public</span> String getPhNo() {
<span style="color: blue;">return</span> phNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhNo(String phNo) {
<span style="color: blue;">this</span>.phNo = phNo;
}
<span style="color: blue;">public</span> Set<Professor> getProfessors() {
<span style="color: blue;">return</span> professors;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setProfessors(Set<Professor> professors) {
<span style="color: blue;">this</span>.professors = professors;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Create Student and Professor:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createProfessorStudent() {
Professor professor = <span style="color: blue;">new</span> Professor();
professor.setName(<span style="color: maroon;">"Gautham"</span>);
professor.setPhNo(<span style="color: maroon;">"3144143141"</span>);
professor.setDob(<span style="color: blue;">new</span> Date());
Set<Student> students = <span style="color: blue;">new</span> HashSet<Student>();
Student student = <span style="color: blue;">new</span> Student();
student.setId(<span style="color: maroon;">5</span>);
students.add(student);
student = <span style="color: blue;">new</span> Student();
student.setName(<span style="color: maroon;">"Johny"</span>);
student.setAddress(<span style="color: maroon;">"Santa Clara"</span>);
student.setPhNo(<span style="color: maroon;">"3144443141"</span>);
students.add(student);
professor.setStudents(students);
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session session = ht.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
<span style="color: blue;">try</span> {
session.saveOrUpdate(professor);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
session.close();
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Retrieve Student with Professor:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> getProfessorStudent() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(Professor.<span style="color: blue;">class</span>,
<span style="color: maroon;">"prof"</span>);
criteria.add(Restrictions.eq(<span style="color: maroon;">"prof.id"</span>, <span style="color: maroon;">4</span>));
List<Professor> professors = ht.findByCriteria(criteria);
<span style="color: blue;">for</span> (Professor prof : professors) {
System.out.println(<span style="color: maroon;">"PROFESSOR: "</span> + prof.getName() + <span style="color: maroon;">":"</span>
+ prof.getPhNo() + <span style="color: maroon;">":"</span> + prof.getDob());
<span style="color: blue;">for</span> (Student stud : prof.getStudents()) {
System.out.println(<span style="color: maroon;">"STUDENT: "</span> + stud.getName() + <span style="color: maroon;">":"</span>
+ stud.getPhNo());
}
}
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-83357057733735265732011-08-12T10:18:00.000-07:002011-08-12T10:18:48.785-07:00HIBERNATE - Unidirectional Many-to-Many Association<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the unidirectional Many-to-Many association using Hibernate. We will be using the Student and Professor table where a student will have one or more professors. The same professor will have lots of students.</span></div><div style="text-align: justify;"><br />
</div><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Schema Creation:</b></span><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">Since many-to-many is not allowed on the database level (Normalization), we will change this to add a junction-entity table in the middle. I am using the name of the junction-entity table to have the combination of both the tables.</span></div><div style="text-align: justify;"><br />
</div><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">The entities will be translated as follows:</span></div><br />
<table border="1"><tbody>
<tr> <th>STUDENT</th> <th>STUDPROF</th> <th>PROFESSOR</th> </tr>
<tr> <td>ID</td> <th>SID</th><td>ID</td> </tr>
<tr> <td>NAME</td> <th>PID</th><td>NAME</td> </tr>
<tr> <td>PHNO</td> <th><br />
</th><td>PHNO</td> </tr>
<tr> <td>ADDRESS</td> <th><br />
</th><td>DOB</td> </tr>
</tbody></table></div><br />
<pre class="source-code"><span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> STUDENT (
ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
ADDRESS <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
PHNO <span class="Apple-style-span" style="color: blue;">VARCHAR(10)</span> NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE </span>PROFESSOR (
ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
PHNO <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
DOB <span class="Apple-style-span" style="color: blue;">DATE </span>NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> STUDPROF (
SID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL DEFAULT 1,
PID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL DEFAULT 1,
<span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span>(SID, PID),
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (SID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>STUDENT (ID),
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (PID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>PROFESSOR (ID)
);
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">HBM Files Creation:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Professor.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Professor</span>" <span style="color: red;">table</span>="<span style="color: blue;">PROFESSOR</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PHNO</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">dob</span>" <span style="color: red;">column</span>="<span style="color: blue;">DOB</span>" <span style="color: red;">type</span>="<span style="color: blue;">date</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Student.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Student</span>" <span style="color: red;">table</span>="<span style="color: blue;">STUDENT</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">ID</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">address</span>" <span style="color: red;">column</span>="<span style="color: blue;">ADDRESS</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PHNO</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">set</span> <span style="color: red;">name</span>="<span style="color: blue;">professors</span>" <span style="color: red;">table</span>="<span style="color: blue;">STUDPROF</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>" <span style="color: red;">lazy</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">key</span> <span style="color: red;">column</span>="<span style="color: blue;">SID</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">key</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">many-to-many</span> <span style="color: red;">column</span>="<span style="color: blue;">PID</span>" <span style="color: red;">class</span>="<span style="color: blue;">Professor</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">many-to-many</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">set</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Java Files:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Professor.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">import</span> java.util.Date;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Professor <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = <span style="color: maroon;">2286787316913029844L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> String phNo;
<span style="color: blue;">private</span> Date dob;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> String getPhNo() {
<span style="color: blue;">return</span> phNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhNo(String phNo) {
<span style="color: blue;">this</span>.phNo = phNo;
}
<span style="color: blue;">public</span> Date getDob() {
<span style="color: blue;">return</span> dob;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setDob(Date dob) {
<span style="color: blue;">this</span>.dob = dob;
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Student.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.util.Set;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Student {
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> String address;
<span style="color: blue;">private</span> String phNo;
<span style="color: blue;">private</span> Set<Professor> professors;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> String getAddress() {
<span style="color: blue;">return</span> address;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddress(String address) {
<span style="color: blue;">this</span>.address = address;
}
<span style="color: blue;">public</span> String getPhNo() {
<span style="color: blue;">return</span> phNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhNo(String phNo) {
<span style="color: blue;">this</span>.phNo = phNo;
}
<span style="color: blue;">public</span> Set<Professor> getProfessors() {
<span style="color: blue;">return</span> professors;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setProfessors(Set<Professor> professors) {
<span style="color: blue;">this</span>.professors = professors;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Create Student and Professor:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createStudentProfessor() {
Student student1 = <span style="color: blue;">new</span> Student();
Student student2 = <span style="color: blue;">new</span> Student();
student1.setName(<span style="color: maroon;">"Vijay"</span>);
student1.setPhNo(<span style="color: maroon;">"3312241124"</span>);
student1.setAddress(<span style="color: maroon;">"St Louis"</span>);
student2.setName(<span style="color: maroon;">"Lakshmi"</span>);
student2.setPhNo(<span style="color: maroon;">"2211221212"</span>);
student2.setAddress(<span style="color: maroon;">"Indiana"</span>);
Professor professor1 = <span style="color: blue;">new</span> Professor();
Professor professor2 = <span style="color: blue;">new</span> Professor();
professor1.setName(<span style="color: maroon;">"Mike"</span>);
professor1.setPhNo(<span style="color: maroon;">"5551115551"</span>);
professor1.setDob(<span style="color: blue;">new</span> Date());
professor2.setName(<span style="color: maroon;">"Jake"</span>);
professor2.setPhNo(<span style="color: maroon;">"4411221144"</span>);
professor2.setDob(<span style="color: blue;">new</span> Date());
Set<Professor> professors = <span style="color: blue;">new</span> HashSet<Professor>();
professors.add(professor1);
professors.add(professor2);
student1.setProfessors(professors);
student2.setProfessors(professors);
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
s.save(student1);
s.save(student2);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Retrieve Student with Professor:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> getStudentProfessor() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(Student.<span style="color: blue;">class</span>,
<span style="color: maroon;">"stud"</span>);
criteria.add(Restrictions.eq(<span style="color: maroon;">"stud.id"</span>, <span style="color: maroon;">1</span>));
List<Student> students = ht.findByCriteria(criteria);
<span style="color: blue;">for</span> (Student student : students) {
System.out.println(<span style="color: maroon;">"STUDENT: "</span> + student.getName() + <span style="color: maroon;">":"</span>
+ student.getAddress() + <span style="color: maroon;">":"</span> + student.getPhNo());
<span style="color: blue;">for</span> (Professor professor : student.getProfessors()) {
System.out.println(<span style="color: maroon;">"PROFESSOR: "</span> + professor.getName() + <span style="color: maroon;">":"</span>
+ professor.getPhNo());
}
}
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com1tag:blogger.com,1999:blog-3368207984841280097.post-7012564451723564172011-08-11T11:43:00.000-07:002011-08-11T11:43:16.057-07:00HIBERNATE - Bi-directional One-to-Many Association<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the bi-directional One-to-Many association using Hibernate. We will be using the Employee and PhoneNumbers table where each Employee will have one or more phone numbers associated with him/her.</span></div><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Schema Creation:</span></b><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">We will be creating two tables Employee and PhoneNumbers. The PhoneNumbers table has a foreign key reference to the Employee table.</span></div><div style="text-align: justify;"><br />
</div><pre class="source-code"><span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> EMPLOYEE (
EMP_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
EMP_NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
EMP_DOB <span class="Apple-style-span" style="color: blue;">DATE </span>NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> PHONENUMBERS (
PH_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5) </span>PRIMARY KEY DEFAULT 1,
PH_NUMBER <span class="Apple-style-span" style="color: blue;">VARCHAR(10)</span> NOT NULL,
EMP_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL,
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (EMP_ID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES</span> EMPLOYEE (EMP_ID)
);
</pre><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>HBM Files Creation:</b></span><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Employee.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Employee</span>" <span style="color: red;">table</span>="<span style="color: blue;">EMPLOYEE</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">empId</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">empName</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_NAME</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">empDOB</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_DOB</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">date</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">set</span> <span style="color: red;">name</span>="<span style="color: blue;">phoneNumbers</span>" <span style="color: red;">table</span>="<span style="color: blue;">PHONENUMBERS</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>"
<span style="color: red;">lazy</span>="<span style="color: blue;">false</span>" <span style="color: red;">inverse</span>="<span style="color: blue;">true</span>" <span style="color: red;">order-by</span>="<span style="color: blue;">PH_ID ASC</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">key</span> <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">key</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">one-to-many</span> <span style="color: red;">class</span>="<span style="color: blue;">PhoneNumbers</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">set</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">PhoneNumbers.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PhoneNumbers</span>" <span style="color: red;">table</span>="<span style="color: blue;">PHONENUMBERS</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">phId</span>" <span style="color: red;">column</span>="<span style="color: blue;">PH_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phoneNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PH_NUMBER</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">many-to-one</span> <span style="color: red;">name</span>="<span style="color: blue;">employee</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>"
<span style="color: red;">not-null</span>="<span style="color: blue;">true</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>" <span style="color: red;">class</span>="<span style="color: blue;">Employee</span>" <span style="color: red;">lazy</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">many-to-one</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Java Files:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Employee.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">import</span> java.util.Date;
<span style="color: blue;">import</span> java.util.LinkedHashSet;
<span style="color: blue;">import</span> java.util.Set;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Employee <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = <span style="color: maroon;">4451804446997564426L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> empId;
<span style="color: blue;">private</span> String empName;
<span style="color: blue;">private</span> Date empDOB;
<span style="color: green;">// Hibernate expects interfaces like Set, Map, List to be the declaration</span>
<span style="color: green;">// because while doing retrieve operations Hibernate uses its own Set, etc</span>
<span style="color: green;">// to fill up the contents in this variable</span>
<span style="color: blue;">private</span> Set<PhoneNumbers> phoneNumbers;
<span style="color: blue;">public</span> Employee() {
phoneNumbers = <span style="color: blue;">new</span> LinkedHashSet<PhoneNumbers>();
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getEmpId() {
<span style="color: blue;">return</span> empId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpId(<span style="color: blue;">int</span> empId) {
<span style="color: blue;">this</span>.empId = empId;
}
<span style="color: blue;">public</span> String getEmpName() {
<span style="color: blue;">return</span> empName;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpName(String empName) {
<span style="color: blue;">this</span>.empName = empName;
}
<span style="color: blue;">public</span> Date getEmpDOB() {
<span style="color: blue;">return</span> empDOB;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpDOB(Date empDOB) {
<span style="color: blue;">this</span>.empDOB = empDOB;
}
<span style="color: blue;">public</span> Set<PhoneNumbers> getPhoneNumbers() {
<span style="color: blue;">return</span> phoneNumbers;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhoneNumbers(Set<PhoneNumbers> phoneNumbers) {
<span style="color: blue;">this</span>.phoneNumbers = phoneNumbers;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> addPhoneNumber(PhoneNumbers phoneNumber) {
<span style="color: blue;">this</span>.phoneNumbers.add(phoneNumber);
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">PhoneNumbers.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PhoneNumbers <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">3505306499633395745L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> phId;
<span style="color: blue;">private</span> String phoneNo;
<span style="color: green;">// Adding Bi-directionality.</span>
<span style="color: blue;">private</span> Employee employee;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getPhId() {
<span style="color: blue;">return</span> phId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhId(<span style="color: blue;">int</span> phId) {
<span style="color: blue;">this</span>.phId = phId;
}
<span style="color: blue;">public</span> String getPhoneNo() {
<span style="color: blue;">return</span> phoneNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhoneNo(String phoneNo) {
<span style="color: blue;">this</span>.phoneNo = phoneNo;
}
<span style="color: blue;">public</span> Employee getEmployee() {
<span style="color: blue;">return</span> employee;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmployee(Employee employee) {
<span style="color: blue;">this</span>.employee = employee;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Create Employee:</span><br />
<br />
<span class="Apple-style-span" style="color: purple; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><i><u>Approach 1:</u></i></span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createEmployeeBidirApproach1() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
<span style="color: green;">// Create an employee</span>
Employee emp = <span style="color: blue;">new</span> Employee();
emp.setEmpName(<span style="color: maroon;">"Peter"</span>);
emp.setEmpDOB(Calendar.getInstance().getTime());
<span style="color: green;">// Save the Employee and get the Primary Key</span>
Integer key = (Integer) s.save(emp);
PhoneNumbers phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"8765432190"</span>);
phNo.setEmployee(emp); <span style="color: green;">// Set the foreign key.</span>
<span style="color: green;">// Save the phone number</span>
s.save(phNo);
phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"9988776655"</span>);
phNo.setEmployee(emp);<span style="color: green;">// Save the phone number</span>
<span style="color: green;">// Save the phone number</span>
s.save(phNo);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<u><i><span class="Apple-style-span" style="color: purple; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Approach 2:</span></i></u><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createEmployeeBidirApproach2() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
Employee emp = <span style="color: blue;">new</span> Employee();
emp.setEmpName(<span style="color: maroon;">"Jake"</span>);
emp.setEmpDOB(Calendar.getInstance().getTime());
<span style="color: green;">// Save Employee and get the key</span>
Integer key = (Integer) s.save(emp);
PhoneNumbers phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"7865765412"</span>);
phNo.setEmployee(emp);
emp.addPhoneNumber(phNo);
phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"7766776677"</span>);
phNo.setEmployee(emp);
emp.addPhoneNumber(phNo);
<span style="color: green;">// Save the employee again with the phone numbers added</span>
s.save(emp);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Retrieve Employee:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> getAllEmployees() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.<span style="color: blue;">class</span>,
<span style="color: maroon;">"emp"</span>);
criteria.add(Restrictions.le(<span style="color: maroon;">"emp.empId"</span>, <span style="color: maroon;">10</span>));
<span style="color: green;">// NOTE: LEFT_JOIN eliminates the selection of all phone numbers from</span>
<span style="color: green;">// the phonenumbers table. This is necessary when we want to filter out</span>
<span style="color: green;">// the result set from the dependent entity</span>
DetachedCriteria child = criteria.createCriteria(<span style="color: maroon;">"emp.phoneNumbers"</span>,
<span style="color: maroon;">"ph"</span>, CriteriaSpecification.LEFT_JOIN);
child.add(Restrictions.ilike(<span style="color: maroon;">"ph.phoneNo"</span>, <span style="color: maroon;">"2"</span>, MatchMode.END));
child.add(Restrictions.le(<span style="color: maroon;">"ph.phId"</span>, <span style="color: maroon;">10</span>));
child.addOrder(Order.desc(<span style="color: maroon;">"ph.phId"</span>));
<span style="color: green;">// NOTE: The result transformer ensures that the result contains only</span>
<span style="color: green;">// one Employee object with set of phone numbers. If this transformer is</span>
<span style="color: green;">// not used, then the result set would be repeated by the number of</span>
<span style="color: green;">// phone numbers present in the set.</span>
criteria
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<Employee> employees = ht.findByCriteria(criteria);
<span style="color: blue;">int</span> count = <span style="color: maroon;">1</span>;
<span style="color: blue;">for</span> (Employee employee : employees) {
System.out.println(<span style="color: maroon;">"Employee Index : "</span> + count++);
System.out.println(<span style="color: maroon;">"----------------------------"</span>);
printEmployee(employee);
}
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> getPhoneNumbersForEmployee() {
Integer empId = <span style="color: maroon;">5</span>;
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(
PhoneNumbers.<span style="color: blue;">class</span>, <span style="color: maroon;">"ph"</span>);
criteria.createAlias(<span style="color: maroon;">"ph.employee"</span>, <span style="color: maroon;">"emp"</span>);
criteria.add(Restrictions.eq(<span style="color: maroon;">"emp.empId"</span>, <span style="color: maroon;">5</span>));
List<PhoneNumbers> phonenumbers = ht.findByCriteria(criteria);
<span style="color: blue;">for</span> (PhoneNumbers phNo : phonenumbers) {
System.out.println(phNo.getPhId() + <span style="color: maroon;">":"</span> + phNo.getPhoneNo() + <span style="color: maroon;">":"</span>
+ phNo.getEmployee().getEmpName());
System.out.println();
}
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printEmployee(Employee employee) {
System.out.println(<span style="color: maroon;">"Employee Id : "</span> + employee.getEmpId());
System.out.println(<span style="color: maroon;">"Employee Name : "</span> + employee.getEmpName());
System.out.println(<span style="color: maroon;">"Employee DOB : "</span> + employee.getEmpDOB());
Set<PhoneNumbers> phNos = employee.getPhoneNumbers();
System.out.println(phNos.getClass());
<span style="color: blue;">if</span> (phNos != <span style="color: blue;">null</span>) {
<span style="color: blue;">for</span> (PhoneNumbers phNo : phNos) {
System.out.println(<span style="color: maroon;">"PhNo : "</span> + phNo.getPhId() + <span style="color: maroon;">", "</span>
+ phNo.getPhoneNo());
}
}
System.out.println(<span style="color: maroon;">"---------------------------"</span>
+ <span style="color: maroon;">"--------------------------"</span>);
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-40027100462844876212011-08-11T09:00:00.000-07:002011-08-11T09:00:53.249-07:00HIBERNATE - Unidirectional One-to-Many Association<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the Unidirectional One-to-Many association using Hibernate. We will be using the Employee and PhoneNumbers table where each Employee will have one or more phone numbers associated with him/her.</span></div><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Schema Creation:</b></span><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">We will be creating two tables Employee and PhoneNumbers. The PhoneNumbers table has a foreign key reference to the Employee table.</span></div><br />
<pre class="source-code"><span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> EMPLOYEE (
EMP_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
EMP_NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
EMP_DOB <span class="Apple-style-span" style="color: blue;">DATE </span>NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> PHONENUMBERS (
PH_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5) </span>PRIMARY KEY DEFAULT 1,
PH_NUMBER <span class="Apple-style-span" style="color: blue;">VARCHAR(10)</span> NOT NULL,
EMP_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> NOT NULL,
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span> (EMP_ID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES</span> EMPLOYEE (EMP_ID)
);
</pre><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>HBM Files Creation:</b></span><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Employee.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">Employee</span>" <span style="color: red;">table</span>="<span style="color: blue;">EMPLOYEE</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">empId</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">empName</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_NAME</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">empDOB</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_DOB</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">date</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">set</span> <span style="color: red;">name</span>="<span style="color: blue;">phoneNumbers</span>" <span style="color: red;">table</span>="<span style="color: blue;">PHONENUMBERS</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>"
<span style="color: red;">lazy</span>="<span style="color: blue;">false</span>" <span style="color: red;">inverse</span>="<span style="color: blue;">true</span>" <span style="color: red;">order-by</span>="<span style="color: blue;">PH_ID ASC</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">key</span> <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">key</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">one-to-many</span> <span style="color: red;">class</span>="<span style="color: blue;">PhoneNumbers</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">set</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">PhoneNumbers.hbm.xml:</span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PhoneNumbers</span>" <span style="color: red;">table</span>="<span style="color: blue;">PHONENUMBERS</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">phId</span>" <span style="color: red;">column</span>="<span style="color: blue;">PH_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">generator</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">phoneNo</span>" <span style="color: red;">column</span>="<span style="color: blue;">PH_NUMBER</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">empId</span>" <span style="color: red;">column</span>="<span style="color: blue;">EMP_ID</span>" <span style="color: red;">not-null</span>="<span style="color: blue;">true</span>"
<span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">property</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Java Files:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Employee.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">import</span> java.util.Date;
<span style="color: blue;">import</span> java.util.LinkedHashSet;
<span style="color: blue;">import</span> java.util.Set;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> Employee <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = <span style="color: maroon;">4451804446997564426L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> empId;
<span style="color: blue;">private</span> String empName;
<span style="color: blue;">private</span> Date empDOB;
<span style="color: green;">// Hibernate expects interfaces like Set, Map, List to be the declaration</span>
<span style="color: green;">// because while doing retrieve operations Hibernate uses its own Set, etc</span>
<span style="color: green;">// to fill up the contents in this variable</span>
<span style="color: blue;">private</span> Set<PhoneNumbers> phoneNumbers;
<span style="color: blue;">public</span> Employee() {
phoneNumbers = <span style="color: blue;">new</span> LinkedHashSet<PhoneNumbers>();
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getEmpId() {
<span style="color: blue;">return</span> empId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpId(<span style="color: blue;">int</span> empId) {
<span style="color: blue;">this</span>.empId = empId;
}
<span style="color: blue;">public</span> String getEmpName() {
<span style="color: blue;">return</span> empName;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpName(String empName) {
<span style="color: blue;">this</span>.empName = empName;
}
<span style="color: blue;">public</span> Date getEmpDOB() {
<span style="color: blue;">return</span> empDOB;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpDOB(Date empDOB) {
<span style="color: blue;">this</span>.empDOB = empDOB;
}
<span style="color: blue;">public</span> Set<PhoneNumbers> getPhoneNumbers() {
<span style="color: blue;">return</span> phoneNumbers;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhoneNumbers(Set<PhoneNumbers> phoneNumbers) {
<span style="color: blue;">this</span>.phoneNumbers = phoneNumbers;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> addPhoneNumber(PhoneNumbers phoneNumber) {
<span style="color: blue;">this</span>.phoneNumbers.add(phoneNumber);
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">PhoneNumbers.java:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PhoneNumbers <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">3505306499633395745L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> phId;
<span style="color: blue;">private</span> String phoneNo;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> empId;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getPhId() {
<span style="color: blue;">return</span> phId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhId(<span style="color: blue;">int</span> phId) {
<span style="color: blue;">this</span>.phId = phId;
}
<span style="color: blue;">public</span> String getPhoneNo() {
<span style="color: blue;">return</span> phoneNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPhoneNo(String phoneNo) {
<span style="color: blue;">this</span>.phoneNo = phoneNo;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getEmpId() {
<span style="color: blue;">return</span> empId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setEmpId(<span style="color: blue;">int</span> empId) {
<span style="color: blue;">this</span>.empId = empId;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Create Employee:</span><br />
<br />
<span class="Apple-style-span" style="color: purple; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><i><u>Approach 1:</u></i></span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createEmployeeApproach1() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
<span style="color: green;">// Create an employee</span>
Employee emp = <span style="color: blue;">new</span> Employee();
emp.setEmpName(<span style="color: maroon;">"Vijay"</span>);
emp.setEmpDOB(Calendar.getInstance().getTime());
<span style="color: green;">// Save the Employee and get the Primary Key</span>
Integer key = (Integer) s.save(emp);
PhoneNumbers phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"3143143142"</span>);
phNo.setEmpId(key); <span style="color: green;">// Set the foreign key.</span>
<span style="color: green;">// Save the phone number</span>
s.save(phNo);
phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"3221322132"</span>);
phNo.setEmpId(key);<span style="color: green;">// Save the phone number</span>
<span style="color: green;">// Save the phone number</span>
s.save(phNo);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<u><i><span class="Apple-style-span" style="color: purple; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Approach 2:</span></i></u><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> createEmployeeApproach2() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
Employee emp = <span style="color: blue;">new</span> Employee();
emp.setEmpName(<span style="color: maroon;">"Steve"</span>);
emp.setEmpDOB(Calendar.getInstance().getTime());
<span style="color: green;">// Save Employee and get the key</span>
Integer key = (Integer) s.save(emp);
PhoneNumbers phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"1221221221"</span>);
phNo.setEmpId(key);
emp.addPhoneNumber(phNo);
phNo = <span style="color: blue;">new</span> PhoneNumbers();
phNo.setPhoneNo(<span style="color: maroon;">"2212212212"</span>);
phNo.setEmpId(key);
emp.addPhoneNumber(phNo);
<span style="color: green;">// Save the employee again with the phone numbers added</span>
s.save(emp);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Retrieve Employee:</span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> getAllEmployees() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.<span style="color: blue;">class</span>,
<span style="color: maroon;">"emp"</span>);
criteria.add(Restrictions.le(<span style="color: maroon;">"emp.empId"</span>, <span style="color: maroon;">5</span>));
<span style="color: green;">// NOTE: LEFT_JOIN eliminates the selection of all phone numbers from</span>
<span style="color: green;">// the phonenumbers table. This is necessary when we want to filter out</span>
<span style="color: green;">// the result set from the dependent entity</span>
DetachedCriteria child = criteria.createCriteria(<span style="color: maroon;">"emp.phoneNumbers"</span>,
<span style="color: maroon;">"ph"</span>, CriteriaSpecification.LEFT_JOIN);
child.add(Restrictions.ilike(<span style="color: maroon;">"ph.phoneNo"</span>, <span style="color: maroon;">"2"</span>, MatchMode.END));
child.add(Restrictions.le(<span style="color: maroon;">"ph.phId"</span>, <span style="color: maroon;">10</span>));
child.addOrder(Order.desc(<span style="color: maroon;">"ph.phId"</span>));
<span style="color: green;">// NOTE: The result transformer ensures that the result contains only</span>
<span style="color: green;">// one Employee object with set of phone numbers. If this transformer is</span>
<span style="color: green;">// not used, then the result set would be repeated by the number of</span>
<span style="color: green;">// phone numbers present in the set.</span>
criteria
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<Employee> employees = ht.findByCriteria(criteria);
<span style="color: blue;">int</span> count = <span style="color: maroon;">1</span>;
<span style="color: blue;">for</span> (Employee employee : employees) {
System.out.println(<span style="color: maroon;">"Employee Index : "</span> + count++);
System.out.println(<span style="color: maroon;">"----------------------------"</span>);
printEmployee(employee);
}
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printEmployee(Employee employee) {
System.out.println(<span style="color: maroon;">"Employee Id : "</span> + employee.getEmpId());
System.out.println(<span style="color: maroon;">"Employee Name : "</span> + employee.getEmpName());
System.out.println(<span style="color: maroon;">"Employee DOB : "</span> + employee.getEmpDOB());
Set<PhoneNumbers> phNos = employee.getPhoneNumbers();
System.out.println(phNos.getClass());
<span style="color: blue;">if</span> (phNos != <span style="color: blue;">null</span>) {
<span style="color: blue;">for</span> (PhoneNumbers phNo : phNos) {
System.out.println(<span style="color: maroon;">"PhNo : "</span> + phNo.getPhId() + <span style="color: maroon;">", "</span>
+ phNo.getPhoneNo());
}
}
System.out.println(<span style="color: maroon;">"---------------------------"</span>
+ <span style="color: maroon;">"--------------------------"</span>);
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com4tag:blogger.com,1999:blog-3368207984841280097.post-34269090451093115442011-08-08T10:54:00.000-07:002011-08-08T10:56:35.102-07:00HIBERNATE - Bi-directional One-to-One Association<div dir="ltr" style="text-align: left;" trbidi="on"><div dir="ltr" style="text-align: left;" trbidi="on"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the Bidirectional One-to-One association using Hibernate. We will be using the Person and Address table where one person will have only one address and an address can belong to only one person. The one-to-one association is achieved using the many-to-one concept in hibernate that is explained in the below section.</span><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Schema Creation:</b></span></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><br />
</div><div style="text-align: justify;"><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">We will be creating two tables Person and Address. Each Person will have an Address associated with him/her and the one-to-one relationship is maintained using the UNIQUE constraint on the foreign key column on the Address table.</span></div></div><br />
<pre class="source-code"><span class="Apple-style-span" style="color: blue;">DROP TABLE</span> ADDRESS_O;
<span class="Apple-style-span" style="color: blue;">DROP TABLE</span> PERSONS_O;
<span class="Apple-style-span" style="color: blue;">CREATE TABLE </span>PERSONS_O (
P_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
AGE <span class="Apple-style-span" style="color: blue;">INTEGER(2) </span>NOT NULL
);
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> ADDRESS_O (
A_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5) </span>PRIMARY KEY DEFAULT 1,
ADDRESS_LINE_1 <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
CITY <span class="Apple-style-span" style="color: blue;">VARCHAR(20)</span>NOT NULL,
STATE <span class="Apple-style-span" style="color: blue;">VARCHAR(2)</span>NOT NULL,
ZIPCODE <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span>NOT NULL,
P_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> UNIQUE,
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span>(P_ID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>PERSONS_O(P_ID)
);
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: 'Trebuchet MS', sans-serif;">HBM Files Creation:</span></b><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">I used the terminology OTO_B to indicate that its a one-to-one bi-directional association.</span></div><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonOTO_B.hbm.xml:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PersonOTO_B</span>" <span style="color: red;">table</span>="<span style="color: blue;">persons_o</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">P_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">update</span>="<span style="color: blue;">false</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">age</span>" <span style="color: red;">column</span>="<span style="color: blue;">AGE</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">one-to-one</span> <span style="color: red;">name</span>="<span style="color: blue;">address</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">one-to-one</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<b><span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">PersonAddressOTO_B.hbm.xml:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PersonAddressOTO_B</span>" <span style="color: red;">table</span>="<span style="color: blue;">address_o</span>"
<span style="color: red;">dynamic-insert</span>="<span style="color: blue;">true</span>" <span style="color: red;">dynamic-update</span>="<span style="color: blue;">true</span>"
<span style="color: red;">select-before-update</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">A_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">addressLine1</span>" <span style="color: red;">column</span>="<span style="color: blue;">ADDRESS_LINE_1</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">city</span>" <span style="color: red;">column</span>="<span style="color: blue;">CITY</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">state</span>" <span style="color: red;">column</span>="<span style="color: blue;">STATE</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">zipCode</span>" <span style="color: red;">column</span>="<span style="color: blue;">ZIPCODE</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>!-- In One-to-one we cannot specify the foreign key column
that has to be filled up
<span style="color: blue;"><</span><span style="color: maroon;">one-to-one</span> <span style="color: red;">name</span>="<span style="color: blue;">person</span>" <span style="color: red;">class</span>="<span style="color: blue;">PersonOTO_B</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>"
<span style="color: red;">constrained</span>="<span style="color: blue;">true</span>"<span style="color: blue;">></span> <span style="color: blue;"><</span>/<span style="color: maroon;">one-to-one</span><span style="color: blue;">></span>
--<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">many-to-one</span> <span style="color: red;">name</span>="<span style="color: blue;">person</span>" <span style="color: red;">column</span>="<span style="color: blue;">P_ID</span>" <span style="color: red;">unique</span>="<span style="color: blue;">true</span>"
<span style="color: red;">not-null</span>="<span style="color: blue;">true</span>" <span style="color: red;">lazy</span>="<span style="color: blue;">false</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span></pre></div><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: 'Trebuchet MS', sans-serif;"><b>Java Files:</b></span><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonOTO_B.java:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PersonOTO_B <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">9127358545321739524L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> age;
<span style="color: blue;">private</span> PersonAddressOTO_B address;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getAge() {
<span style="color: blue;">return</span> age;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAge(<span style="color: blue;">int</span> age) {
<span style="color: blue;">this</span>.age = age;
}
<span style="color: blue;">public</span> PersonAddressOTO_B getAddress() {
<span style="color: blue;">return</span> address;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddress(PersonAddressOTO_B address) {
<span style="color: blue;">this</span>.address = address;
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonAddressOTO_B.java:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PersonAddressOTO_B <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">9127358545321739523L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String addressLine1;
<span style="color: blue;">private</span> String city;
<span style="color: blue;">private</span> String state;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> zipCode;
<span style="color: blue;">private</span> PersonOTO_B person;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getAddressLine1() {
<span style="color: blue;">return</span> addressLine1;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddressLine1(String addressLine1) {
<span style="color: blue;">this</span>.addressLine1 = addressLine1;
}
<span style="color: blue;">public</span> String getCity() {
<span style="color: blue;">return</span> city;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setCity(String city) {
<span style="color: blue;">this</span>.city = city;
}
<span style="color: blue;">public</span> String getState() {
<span style="color: blue;">return</span> state;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setState(String state) {
<span style="color: blue;">this</span>.state = state;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getZipCode() {
<span style="color: blue;">return</span> zipCode;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setZipCode(<span style="color: blue;">int</span> zipCode) {
<span style="color: blue;">this</span>.zipCode = zipCode;
}
<span style="color: blue;">public</span> PersonOTO_B getPerson() {
<span style="color: blue;">return</span> person;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPerson(PersonOTO_B person) {
<span style="color: blue;">this</span>.person = person;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<b><span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">AddPerson:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> addPersonOTO_B() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
<span style="color: green;">// Create a person</span>
PersonOTO_B person = <span style="color: blue;">new</span> PersonOTO_B();
person.setName(<span style="color: maroon;">"Luna"</span>);
person.setAge(<span style="color: maroon;">33</span>);
Integer key = (Integer) s.save(person);
System.out.println(<span style="color: maroon;">"Primary Key : "</span> + key);
person.setId(key);
<span style="color: green;">// Create the address for the person</span>
PersonAddressOTO_B address = <span style="color: blue;">new</span> PersonAddressOTO_B();
address.setAddressLine1(<span style="color: maroon;">"Lunaris"</span>);
address.setCity(<span style="color: maroon;">"Twinkle"</span>);
address.setState(<span style="color: maroon;">"MA"</span>);
address.setZipCode(<span style="color: maroon;">10308</span>);
address.setPerson(person);
person.setAddress(address);
key = (Integer) s.save(address);
System.out.println(<span style="color: maroon;">"Primary Key again : "</span> + key);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;">Retrieve Address & Persons:</span></b><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> <span class="Apple-style-span" style="color: #cc0000;">getAddress</span>() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
PersonAddressOTO_B address = (PersonAddressOTO_B) ht.get(
PersonAddressOTO_B.<span style="color: blue;">class</span>, <span style="color: maroon;">1</span>);
printAddress(address);
PersonOTO_B person = (PersonOTO_B) ht.get(PersonOTO_B.<span style="color: blue;">class</span>, address
.getPerson().getId());
printPerson(person);
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> <span class="Apple-style-span" style="color: #cc0000;">getAddressWithRestrictions</span>() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(
PersonAddressOTO_B.<span style="color: blue;">class</span>, <span style="color: maroon;">"a"</span>);
criteria.createAlias(<span style="color: maroon;">"person"</span>, <span style="color: maroon;">"p"</span>);
criteria.add(Restrictions.ilike(<span style="color: maroon;">"p.name"</span>, <span style="color: maroon;">"L"</span>, MatchMode.START));
criteria.add(Restrictions.eq(<span style="color: maroon;">"a.zipCode"</span>, <span style="color: maroon;">50102</span>));
List<PersonAddressOTO_B> address = ht.findByCriteria(criteria);
printAddress(address);
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printAddress(List<PersonAddressOTO_B> address) {
<span style="color: blue;">for</span> (PersonAddressOTO_B add : address) {
printAddress(add);
}
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printAddress(PersonAddressOTO_B address) {
System.out.println(<span style="color: maroon;">"-----------------------------------"</span>);
System.out.println(address.getId());
System.out.println(address.getAddressLine1());
System.out.println(address.getCity() + <span style="color: maroon;">","</span> + address.getState() + <span style="color: maroon;">","</span>
+ address.getZipCode());
System.out.println(address.getPerson().getId() + <span style="color: maroon;">","</span>
+ address.getPerson().getName() + <span style="color: maroon;">","</span>
+ address.getPerson().getAge());
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printPerson(PersonOTO_B person) {
System.out.println(<span style="color: maroon;">"-----------------------------------"</span>);
System.out.println(person.getId() + <span style="color: maroon;">","</span> + person.getName() + <span style="color: maroon;">","</span>
+ person.getAge());
System.out.println(person.getAddress().getId() + <span style="color: maroon;">","</span>
+ person.getAddress().getAddressLine1() + <span style="color: maroon;">","</span>
+ person.getAddress().getCity() + <span style="color: maroon;">","</span>
+ person.getAddress().getState() + <span style="color: maroon;">","</span>
+ person.getAddress().getZipCode());
}
</pre><br />
</div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com0tag:blogger.com,1999:blog-3368207984841280097.post-10983403356370978552011-08-05T08:37:00.000-07:002011-08-05T08:37:43.522-07:00HIBERNATE - Unidirectional One-to-One Association<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">In the following example, we will be going through the Unidirectional One-to-One association using Hibernate. We will be using the Person and Address table where one person will have only one address and an address can belong to only one person.</span></div><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Schema Creation:</b></span><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">We will be creating two tables Person and Address. Each Person will have an Address associated with him/her and the one-to-one relationship is maintained using the UNIQUE constraint on the foreign key column on the Address table. </span></div><br />
<pre class="source-code"><span class="Apple-style-span" style="color: blue;">DROP TABLE</span> PERSONS_O;
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> PERSONS_O (
P_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
NAME <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
AGE <span class="Apple-style-span" style="color: blue;">INTEGER(2)</span> NOT NULL
);
<span class="Apple-style-span" style="color: blue;">DROP TABLE</span> ADDRESS_O;
<span class="Apple-style-span" style="color: blue;">CREATE TABLE</span> ADDRESS_O (
A_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span> <span class="Apple-style-span" style="color: #cc0000;">PRIMARY KEY</span> DEFAULT 1,
ADDRESS_LINE_1 <span class="Apple-style-span" style="color: blue;">VARCHAR(30)</span> NOT NULL,
CITY <span class="Apple-style-span" style="color: blue;">VARCHAR(20)</span>NOT NULL,
STATE <span class="Apple-style-span" style="color: blue;">VARCHAR(2)</span>NOT NULL,
ZIPCODE <span class="Apple-style-span" style="color: blue;">INTEGER(5)</span>NOT NULL,
P_ID <span class="Apple-style-span" style="color: blue;">INTEGER(5) </span>UNIQUE,
<span class="Apple-style-span" style="color: #cc0000;">FOREIGN KEY</span>(P_ID) <span class="Apple-style-span" style="color: #cc0000;">REFERENCES </span>PERSONS_O(P_ID)
);
</pre><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>HBM Files Creation:</b></span><br />
<br />
<div style="text-align: justify;"><span class="Apple-style-span" style="font-family: 'Trebuchet MS', sans-serif;">I used the as xxxO to indicate that its a one-to-one association.</span></div><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonO.hbm.xml:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PersonO</span>" <span style="color: red;">table</span>="<span style="color: blue;">persons_o</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">P_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">name</span>" <span style="color: red;">column</span>="<span style="color: blue;">NAME</span>" <span style="color: red;">update</span>="<span style="color: blue;">false</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">age</span>" <span style="color: red;">column</span>="<span style="color: blue;">AGE</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">one-to-one</span> <span style="color: red;">name</span>="<span style="color: blue;">address</span>" <span style="color: red;">cascade</span>="<span style="color: blue;">all</span>"<span style="color: blue;">></span><span style="color: blue;"><</span>/<span style="color: maroon;">one-to-one</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonAddressO.hbm.xml:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;"><?</span><span style="color: maroon;">xml</span> <span style="color: red;">version</span>="<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span>
<span style="color: blue;"><</span>!DOCTYPE hibernate-mapping PUBLIC
"<span style="color: blue;">-//Hibernate/Hibernate Mapping DTD 3.0//EN</span>"
"<span style="color: blue;">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">hibernate-mapping</span> <span style="color: red;">package</span>="<span style="color: blue;">org.mybusiness.pojos</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">class</span> <span style="color: red;">name</span>="<span style="color: blue;">PersonAddressO</span>" <span style="color: red;">table</span>="<span style="color: blue;">address_o</span>" <span style="color: red;">dynamic-insert</span>="<span style="color: blue;">true</span>"
<span style="color: red;">dynamic-update</span>="<span style="color: blue;">true</span>" <span style="color: red;">select-before-update</span>="<span style="color: blue;">false</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">id</span> <span style="color: red;">name</span>="<span style="color: blue;">id</span>" <span style="color: red;">column</span>="<span style="color: blue;">A_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>"<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">generator</span> <span style="color: red;">class</span>="<span style="color: blue;">increment</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">id</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">addressLine1</span>" <span style="color: red;">column</span>="<span style="color: blue;">ADDRESS_LINE_1</span>"
<span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">city</span>" <span style="color: red;">column</span>="<span style="color: blue;">CITY</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">state</span>" <span style="color: red;">column</span>="<span style="color: blue;">STATE</span>" <span style="color: red;">type</span>="<span style="color: blue;">string</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">zipCode</span>" <span style="color: red;">column</span>="<span style="color: blue;">ZIPCODE</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: maroon;">property</span> <span style="color: red;">name</span>="<span style="color: blue;">personId</span>" <span style="color: red;">column</span>="<span style="color: blue;">P_ID</span>" <span style="color: red;">type</span>="<span style="color: blue;">integer</span>" /<span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">class</span><span style="color: blue;">></span>
<span style="color: blue;"><</span>/<span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">></span>
</pre><br />
<span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;"><b>Java Files:</b></span><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonO.java:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PersonO <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">9127358545321739524L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String name;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> age;
<span style="color: blue;">private</span> PersonAddressO address;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getName() {
<span style="color: blue;">return</span> name;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setName(String name) {
<span style="color: blue;">this</span>.name = name;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getAge() {
<span style="color: blue;">return</span> age;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAge(<span style="color: blue;">int</span> age) {
<span style="color: blue;">this</span>.age = age;
}
<span style="color: blue;">public</span> PersonAddressO getAddress() {
<span style="color: blue;">return</span> address;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddress(PersonAddressO address) {
<span style="color: blue;">this</span>.address = address;
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>PersonAddressO.java:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">package</span> org.mybusiness.pojos;
<span style="color: blue;">import</span> java.io.Serializable;
<span style="color: blue;">public</span> <span style="color: blue;">class</span> PersonAddressO <span style="color: blue;">implements</span> Serializable {
<span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">final</span> <span style="color: blue;">long</span> serialVersionUID = -<span style="color: maroon;">9127358545321739523L</span>;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> id;
<span style="color: blue;">private</span> String addressLine1;
<span style="color: blue;">private</span> String city;
<span style="color: blue;">private</span> String state;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> zipCode;
<span style="color: blue;">private</span> <span style="color: blue;">int</span> personId;
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getId() {
<span style="color: blue;">return</span> id;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setId(<span style="color: blue;">int</span> id) {
<span style="color: blue;">this</span>.id = id;
}
<span style="color: blue;">public</span> String getAddressLine1() {
<span style="color: blue;">return</span> addressLine1;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setAddressLine1(String addressLine1) {
<span style="color: blue;">this</span>.addressLine1 = addressLine1;
}
<span style="color: blue;">public</span> String getCity() {
<span style="color: blue;">return</span> city;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setCity(String city) {
<span style="color: blue;">this</span>.city = city;
}
<span style="color: blue;">public</span> String getState() {
<span style="color: blue;">return</span> state;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setState(String state) {
<span style="color: blue;">this</span>.state = state;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getZipCode() {
<span style="color: blue;">return</span> zipCode;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setZipCode(<span style="color: blue;">int</span> zipCode) {
<span style="color: blue;">this</span>.zipCode = zipCode;
}
<span style="color: blue;">public</span> <span style="color: blue;">int</span> getPersonId() {
<span style="color: blue;">return</span> personId;
}
<span style="color: blue;">public</span> <span style="color: blue;">void</span> setPersonId(<span style="color: blue;">int</span> personId) {
<span style="color: blue;">this</span>.personId = personId;
}
}
</pre><br />
<b><span class="Apple-style-span" style="color: #cc0000; font-family: Georgia, 'Times New Roman', serif;">Functionalities:</span></b><br />
<br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>AddPerson:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> addPerson() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
Session s = ht.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
<span style="color: blue;">try</span> {
<span style="color: green;">// Create a person</span>
PersonO person = <span style="color: blue;">new</span> PersonO();
person.setName(<span style="color: maroon;">"Luke"</span>);
person.setAge(<span style="color: maroon;">18</span>);
Integer key = (Integer) s.save(person);
System.out.println(<span style="color: maroon;">"Primary Key : "</span> + key);
<span style="color: green;">// Create the address for the person</span>
PersonAddressO address = <span style="color: blue;">new</span> PersonAddressO();
address.setAddressLine1(<span style="color: maroon;">"Shines"</span>);
address.setCity(<span style="color: maroon;">"Caramel"</span>);
address.setState(<span style="color: maroon;">"UT"</span>);
address.setZipCode(<span style="color: maroon;">80708</span>);
address.setPersonId(key);
person.setAddress(address);
key = (Integer) s.save(address);
System.out.println(<span style="color: maroon;">"Primary Key again : "</span> + key);
tx.commit();
} <span style="color: blue;">catch</span> (Exception e) {
e.printStackTrace();
tx.rollback();
} <span style="color: blue;">finally</span> {
s.close();
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>RetrievePersons:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> retrieveAllPersons() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(PersonO.<span style="color: blue;">class</span>,
<span style="color: maroon;">"person"</span>);
List<PersonO> persons = ht.findByCriteria(criteria);
printPerson(persons);
}
<span style="color: blue;">private</span> <span style="color: blue;">void</span> printPerson(List<PersonO> persons) {
<span style="color: blue;">for</span> (PersonO p : persons) {
System.out
.println(<span style="color: maroon;">"---------------------------------------------------"</span>);
System.out
.println(p.getId() + <span style="color: maroon;">","</span> + p.getName() + <span style="color: maroon;">","</span> + p.getAge());
System.out.println(p.getAddress().getAddressLine1() + <span style="color: maroon;">","</span>
+ p.getAddress().getCity() + <span style="color: maroon;">","</span>
+ p.getAddress().getState() + <span style="color: maroon;">","</span>
+ p.getAddress().getZipCode());
System.out
.println(<span style="color: maroon;">"---------------------------------------------------"</span>);
}
}
</pre><br />
<span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"><b>Retrieve Using Restrictions:</b></span><br />
<br />
<pre class="source-code"><span style="color: blue;">public</span> <span style="color: blue;">void</span> retrievePersonsWithRestrictions() {
HibernateTemplate ht = <span style="color: blue;">new</span> HibernateTemplate(sessionFactory);
DetachedCriteria criteria = DetachedCriteria.forClass(PersonO.<span style="color: blue;">class</span>,
<span style="color: maroon;">"p"</span>);
criteria.createAlias(<span style="color: maroon;">"address"</span>, <span style="color: maroon;">"a"</span>);
criteria.add(Restrictions.eq(<span style="color: maroon;">"p.age"</span>, <span style="color: maroon;">26</span>));
criteria.add(Restrictions.ilike(<span style="color: maroon;">"a.state"</span>, <span style="color: maroon;">"M"</span>, MatchMode.ANYWHERE));
criteria.add(Restrictions.sqlRestriction(<span style="color: maroon;">"ZIPCODE > 50000"</span>));
criteria.addOrder(Order.desc(<span style="color: maroon;">"p.name"</span>));
criteria.addOrder(Order.asc(<span style="color: maroon;">"a.zipCode"</span>));
List<PersonO> persons = ht.findByCriteria(criteria);
printPerson(persons);
}
</pre></div>Vijihttp://www.blogger.com/profile/17888152743660300893noreply@blogger.com1