﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-果酱-文章分类-SSH构架精要</title><link>http://www.blogjava.net/chen45257211/category/49090.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 23 Jul 2011 07:31:22 GMT</lastBuildDate><pubDate>Sat, 23 Jul 2011 07:31:22 GMT</pubDate><ttl>60</ttl><item><title>Hibernate上手指南（转载）</title><link>http://www.blogjava.net/chen45257211/articles/354392.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Fri, 15 Jul 2011 06:00:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/354392.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/354392.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/354392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/354392.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/354392.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;Hibernate上手&nbsp;&nbsp;&nbsp; Hibernate，很久以前我就听说过这个名词，但是似乎没什么动力让我去接近它，感觉它是一个很复杂的东西，一直没搞明白它到底是用来做什么的。直到接手了一个项目在技术选型的时候我再一次的看到了Hibernate。我尝试着去使用它，发现它并不是我想像中的那么深奥，它很易用。你并不需要了解它的内部结构，它一样能为你工作的很好，如果你...&nbsp;&nbsp;<a href='http://www.blogjava.net/chen45257211/articles/354392.html'>阅读全文</a><img src ="http://www.blogjava.net/chen45257211/aggbug/354392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2011-07-15 14:00 <a href="http://www.blogjava.net/chen45257211/articles/354392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate API基础篇 （转载）</title><link>http://www.blogjava.net/chen45257211/articles/354383.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Fri, 15 Jul 2011 03:33:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/354383.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/354383.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/354383.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/354383.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/354383.html</trackback:ping><description><![CDATA[<div><div><span style="font-size: 16px; line-height: 24px; font-family: Simsun; "><span style="font-size: 10.5pt; line-height: 21px; font-variant: normal !important; "><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; "><img alt="体系结构图" hspace="0" src="http://image.360doc.com/DownloadImg/2434/273160_1.gif" align="baseline" border="0" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; width: 340px; height: 380px; " /></div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">上面的这个图是<strong>Hiberante应用程序的结构</strong>，</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">对上图中各项的说明如下：（1）Application：应用 （2）Persistent Object：持久化对象（3）hibernate.properties：Hibernate属性文件/Hibernate配置文件 （4）XML Mapping：Hibernate映射文件 （5）Database：数据库</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">由上图可以看出，Hibernate是通过一系列的配置文件和数据库来实现持久化对象的持久化操作的。</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">为了使用Hibernate，需要创建与数据库中的表对应的持久化对象，然后再通过影射文件将持久化对象中的<strong style="color: red; ">属性与</strong>数据库表中的<strong style="color: red; ">字段对应</strong>起来。这样才能通过持久化对象完成对数据库表中的数据的增加、修改、查询和删除操作。</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">Hibernate主要包括三个组件：</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;<strong>（1）连接管理组件</strong>：它提供了高效的数据库连接管理。数据库连接是和数据库进行交互的唯一渠道，建立和关闭一个连接需要耗费大量的资源。所以现在的应用程序都采用连接池的方法来管理与数据库的连接，避免频繁的建立和关闭数据库连接</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;&nbsp;<strong>（2）事务管理组件</strong>：通过事务，一次可以执行多个SQL语句，而且能够保证这些语句执行成功或者都不成功</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;&nbsp;<strong>（3）对象/关系映射组建</strong>：对象/映射技术可以实现从对象模型到数据库中关系模型的映射工作。通过这一技术，Hibernate实现了对象的持久化操作。</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; "><strong style="font-size: 24pt; ">Hinernate的体系结构（运行时）</strong></div><div style="margin-top: 13pt; margin-right: 0cm; margin-bottom: 13pt; margin-left: 0cm; ">由于Hibernate非常灵活，而且提供了多种运行时的结构组成方案，这里介绍的时一个&#8220;最全面&#8221;的体系结构，它最大程度的完成了对持久层功能的封装，也就使得在开发中要完成的工作量最少，是在<em style="color: #000080; ">开<span style="background-color: #ffffff; ">发中最经常使用</span></em>的一种方式。这种方式把JDBC/JTA都交由Hibernate去完成，而不需要对这一部分进行任何处。<span>&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;</span><img alt="&#8220;重量级&#8221;体系结构" hspace="0" src="http://image.360doc.com/DownloadImg/2434/273160_2.gif" align="baseline" border="0" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; " /></div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;各个部分的主要功能如下&nbsp;<div>&nbsp;</div></span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; "><strong>SessionFactory</strong>：它保存了对当前数据库配置的所有映射关系，它是将某个数据库的映射关系经过编译之后全部保存在内存中的。 它还是生成Session的工厂，它在进行实例化的过程中将会用到ConnectionProvider。<span style="color: #000080; "><em>一个SessionFactory对应一个数据库连接，当数据库连接改变时需要修改SessionFactory</em></span><div><strong>Sesion</strong>：是进行持久化操作的基础，所有的持久化操作都是在Session的基础上进行的。它相当与JDBC中的Connection。它是Hibernate的持久化管理器的核心，提供了一系列的持久化操作方法。另外，它还持有一个针对持久化对象的一级缓存，在遍历持久化对象或者根据持久化标识查找对象的时候会用到。</div><div><strong>Transation</strong>：功能上和数据库中的事务完全一样，通过它实现对数据库中事务的控制。Transation对象是Session对象产生的，所以他的生命周期比Session短。一个Session的生命周期中可以有多个Transaction对象。</div><div><strong>ConnectonProvider</strong>：主要作用是生成与数据库建立了连接的JDBC对象，同时他还作为数据库连接的缓冲池。通过ConnectionProvider实现了应用程序和底层的DataSource和DriverManager的隔离。</div><div>T<strong>ransactionFactory</strong>：是生成Transaction对象的工厂，通过TransactionFactory实现了事务的封装，使其具体的实现方法与应用程序无关。</div></div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; "><span style="font-size: 24pt; ">Hibernate对象的生命周期</span></div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">一共有三种生命周期：</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">（<strong>1）瞬态</strong>：表示该实体对象在内存中是自由存在的，也就是说<span style="color: #ff0000; ">与<strong>数据库中的数据没有任何的关联</strong></span>。即，该实体从未与任何持久化上下文联系过，没有持久化标识（相当与主键）。瞬态实体的特征有：</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 与数据库中的记录没有任何关联，也就是没有与其相关联的数据库记录</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 与Session没有任何关系，也就是没有通过Session对象的实例对其进行任何持久化的操作。</div><div><strong>（2）持久态</strong>：指该实体对象处于Hibernate框架所管理的状态，也就是说这个实体对象是与Session对象的实例相关的。<span style="color: #ff0000; "><strong>处于持久态的实体对象的最大特征是对其所作的任何变更操作都将被Hibernate持久化到数据库中。</strong><span style="color: #000000; ">处于持久态的对象具有的特征为：</span><div style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 每个持久态对象都于一个Session对象关联</div><div style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 处于持久态的对象是于数据库中的记录相关联的</div><div style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hibernate会根据持久态对象的属性的变化而改变数据库中的相应记录</div></span></div><div><strong>（3）游离态</strong>：处于持久态的实体对象，当他不再与Session对象关联时，这个对象就变成了游离态。。游离态对象的特征有：</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 游离态对象一定是由持久态对象转换而来</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 游离态实体不再于Session关联</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 游离态实体对象与数据库中的数据没有直接联系，主要表现在对其进行的修改不再影响到数据库中的数据</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 游离态实体对象在数据库中有相应的数据记录（如果该记录没有被删除）</div><div>&nbsp;</div><div><strong>判断一个实体对象是否处于瞬态：</strong></div><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 该实体对象的&lt;id&gt;属性（如果存在）的值为空</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果在映射文件中为&lt;id&gt;设置了unsaved-value属性，并且实体对象的id属性的值与unsaved-value属性的值相同</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果这个实体对象配置version属性，并且version属性的值为空</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在映射文件中为version属性设置了unsaved-value属性，并且version属性的值与unsaved－value属性的值相同。</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 如果设置了interceptor，并且interceptor的isUnsaved（）方法的返回值为true</div><div>满足上述条件的实体对象就处于瞬态，否则为游离态（前提是不处于持久态）</div><div>&nbsp;</div><div>下面通过一段代码来说明状态问题：</div><div>&nbsp;</div><div>public class LifeCycle<br />{</div><div>&nbsp;public static final SessionFactory sessionFactory;<br />&nbsp;<br />&nbsp;static<br />&nbsp;{<br />&nbsp;&nbsp;sessionFactory = new Configuration().configure().buildSessionFactory();<br />&nbsp;}<br />&nbsp;<br />&nbsp;public static void main(String[] args)<br />&nbsp;{<br />&nbsp;&nbsp;LifeCycle.lifecycle();<br />&nbsp;}<br />&nbsp;<br />&nbsp;public static void lifecycle()<br />&nbsp;{<br />&nbsp;&nbsp;// 创建游离态实体对象User<br />&nbsp;&nbsp;User user = new User();<br />&nbsp;&nbsp;user.setName( "abc" + String.valueOf( Math.random() ) );<br />&nbsp;&nbsp;// 为了避免两次运行的用户名重复，所以增加了随机数<br />&nbsp;&nbsp;user.setPassword( "def" );<br />&nbsp;&nbsp;// user仍然处于游离态</div><div>&nbsp;&nbsp;Session session = sessionFactory.openSession();<br />&nbsp;&nbsp;Transaction tx = session.beginTransaction();<br />&nbsp;&nbsp;// 此时user对象仍然是Transient状态<br />&nbsp;&nbsp;session.save( user );<br />&nbsp;&nbsp;// 此时，user对象已经被纳入了Hibernate的实体管理容器中，并转变为Persistent状态<br />&nbsp;&nbsp;System.out.println( "User 1:" + user );<br />&nbsp;&nbsp;// 此时的id已经有值了。<br />&nbsp;&nbsp;get( user.getId() );<br />&nbsp;&nbsp;// 但并未真正的执行数据库的操作，所以无法得到对象的值<br />&nbsp;&nbsp;tx.commit();<br />&nbsp;&nbsp;// 事务被提交后，将向数据库的用户表中插入一条记录<br />&nbsp;&nbsp;System.out.println( "Transaction 1 commit!" );<br />&nbsp;&nbsp;get( user.getId() );<br />&nbsp;&nbsp;// 这时可以由数据库中得到刚才插入的user对象了。<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;Transaction tx2 = session.beginTransaction();<br />&nbsp;&nbsp;user.setPassword( "mmmmmmmmmmmmm" );<br />&nbsp;&nbsp;tx2.commit();<br />&nbsp;&nbsp;// 虽然这个事务中并没有调用Session的save()方法来保存user对象<br />&nbsp;&nbsp;// 但由于user对象处于Persistent状态，所以对user对象所做的任何修改都将被持久化到数据库中<br />&nbsp;&nbsp;// 那么数据库中的用户密码也应该变为了def。<br />&nbsp;&nbsp;System.out.println( "Transaction 2 commit!" );<br />&nbsp;&nbsp;session.close();</div><div>&nbsp;&nbsp;get( user.getId() );<br />&nbsp;&nbsp;// 此时密码已经变为新的值了<br />&nbsp;}<br />&nbsp;<br />&nbsp;public static User get( String id )<br />&nbsp;{<br />&nbsp;&nbsp;Session session = sessionFactory.openSession();<br />&nbsp;&nbsp;Transaction tx = session.beginTransaction();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;User user = (User)session.get( User.class, id );<br />&nbsp;&nbsp;// Hibernate在返回User对象之间会将其纳入到Hibernate的实体管理容器中<br />&nbsp;&nbsp;// 所以，这时的user对象是Persistent状态的<br />&nbsp;&nbsp;display( user );<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;tx.commit();<br />&nbsp;&nbsp;session.close();<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;return user;<br />&nbsp;}<br />&nbsp;<br />&nbsp;public static void display( Object obj )<br />&nbsp;{<br />&nbsp;&nbsp;System.out.println( obj );<br />&nbsp;}<br />}</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; ">&nbsp;</div></span><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; ">&nbsp;</div><div style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; background-color: #00ff00; "><strong>提示1：Hibernate支持四种数据库连接池的组件</strong><div>(1)C3P0。这是Hiberante自身提供的。其文档地址见：<a href="http://www.mchange.com/projects/c3p0/index.html">www.mchange.com/projects/c3p0/index.html</a>；有关Hibernte中配置c3p0的文档参考：<a href="http://www.hibernate.org/214.html">www.hibernate.org/214.html</a></div><div>&nbsp;(2)DBCP：它是Apache软件基金组织的一个开源项目。由于在Hibernte3中不再提供DBCConnectionProvider类来直接使用DBCP建立数据库的连接池，在这里提供两种方法解决这个问题。一种是仿照Hibernate2中的DBCPConnectionProvider类自己实现一个使用DBCP并实现了ConnetcitonProvider接口的连接池类。另一种是让应用服务器通过JNDI的方式来提供数据库的连接，Hibernate通过JNDI得到数据库的连接。DBCP的详细信息见：<a href="http://jakarta.apache.org/commons/dbcp/">http://jakarta.apache.org/commons/dbcp/</a>。关于自己实现ConnectionProvider接口的方法见<a href="http://wiki.apache.org/jakarta-commons/DBCP/Hibernate">http://wiki.apache.org/jakarta-commons/DBCP/Hibernate</a></div><div>(3)Proxool：也是开放源代码的提供数据库连接池的组件。其官方网站为：<a href="http://proxool.sourceforge.net/">http://proxool.sourceforge.net</a>。有关配置的详细参数见官方网站<br /><br />文章来源：<a href="http://www.360doc.com/content/06/1126/17/2434_273160.shtml">http://www.360doc.com/content/06/1126/17/2434_273160.shtml</a></div></div></span></div></div><img src ="http://www.blogjava.net/chen45257211/aggbug/354383.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2011-07-15 11:33 <a href="http://www.blogjava.net/chen45257211/articles/354383.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>