<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>拈花微笑 &#187; hibernate</title>
	<atom:link href="http://www.jayxu.com/tags/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jayxu.com</link>
	<description>晶 · 彦</description>
	<lastBuildDate>Thu, 02 Feb 2012 09:24:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
<link>http://www.jayxu.com</link>
<url>http://www.jayxu.com/wp-content/cbnet-favicon/1289928181_Black_Cat.png</url>
<title>拈花微笑</title>
</image>
		<item>
		<title>About save/persist, update/merge in JPA/Hibernate</title>
		<link>http://www.jayxu.com/2011/12/15/13164/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=about-savepersist-updatemerge-in-jpahibernate</link>
		<comments>http://www.jayxu.com/2011/12/15/13164/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 14:55:56 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://www.jayxu.com/?p=13164</guid>
		<description><![CDATA[Save VS Persist from https://hibernate.onjira.com/browse/HHH-1273 The persist() operation on Session is not cascaded at flush time. This is somewhat unexpected from a users point of view and very difficult to explain and understand (you need excellent knowledge of flushing and cascading). Reason #1 for removal. The persist() operation in general does not return a database <a href="http://www.jayxu.com/2011/12/15/13164/"> read more <span class="meta-nav">&#187;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<h2>Save VS Persist</h2>
<address>from <a href="https://hibernate.onjira.com/browse/HHH-1273" target="_blank">https://hibernate.onjira.com/browse/HHH-1273</a></address>
<blockquote><p>The persist() operation on Session is not cascaded at flush time. This is somewhat unexpected from a users point of view and very difficult to explain and understand (you need excellent knowledge of flushing and cascading). Reason #1 for removal.</p>
<p>The persist() operation in general does not return a database identifier. This is surprising, as the JPA spec clearly requires a persistent instance to have a database identifier value. Since persist() makes instances persistent, it has to have the same semantics for assigning identifiers as save(). Hence, I expect that once this mismatch is resolved in the expert group, the persist() method will return a database identifier. Everything else doesn&#8217;t make much sense, given the current specification and documentation. Conclusion is that persist() will have the same signature as save(). Reason #2 for removal.</p>
<p>To document persist() properly I need a reason for its existence. Right now I&#8217;m telling readers/users to ignore it on the Session API, because it only complicates the situation with no benefit.</p></blockquote>
<h2>Update VS Merge</h2>
<address>from <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-saveorupdate" target="_blank">http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-saveorupdate</a></address>
<blockquote><p>Usually update() or saveOrUpdate() are used in the following scenario:</p>
<ul>
<li>the application loads an object in the first session</li>
<li>the object is passed up to the UI tier</li>
<li>some modifications are made to the object</li>
<li>the object is passed back down to the business logic tier</li>
<li>the application persists these modifications by calling update() in a second session</li>
</ul>
<p>saveOrUpdate() does the following:</p>
<ul>
<li>if the object is already persistent in this session, do nothing</li>
<li>if another object associated with the session has the same identifier, throw an exception</li>
<li> if the object has no identifier property, save() it</li>
<li> if the object&#8217;s identifier has the value assigned to a newly instantiated object, save() it</li>
<li> if the object is versioned by a &lt;version&gt; or &lt;timestamp&gt;, and the version property value is the same value assigned to a newly instantiated object, save() it</li>
<li> otherwise update() the object</li>
</ul>
<p>and merge() is very different:</p>
<ul>
<li>if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance</li>
<li> if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance</li>
<li> the persistent instance is returned</li>
<li> the given instance does not become associated with the session, it remains detached</li>
</ul>
</blockquote>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2011/12/15/13164/">About save/persist, update/merge in JPA/Hibernate</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&title=About+save%2Fpersist%2C+update%2Fmerge+in+JPA%2FHibernate" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2011/12/15/13164/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用Spring LDAP ODM操作LDAP</title>
		<link>http://www.jayxu.com/2011/12/14/13131/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e4%25bd%25bf%25e7%2594%25a8spring-ldap-odm%25e6%2593%258d%25e4%25bd%259cldap</link>
		<comments>http://www.jayxu.com/2011/12/14/13131/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 15:10:16 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring context]]></category>
		<category><![CDATA[spring ldap]]></category>
		<category><![CDATA[spring ldap odm]]></category>
		<category><![CDATA[spring mvc]]></category>
		<category><![CDATA[spring orm]]></category>
		<category><![CDATA[spring tx]]></category>
		<category><![CDATA[struts]]></category>
		<category><![CDATA[牢骚]]></category>

		<guid isPermaLink="false">http://www.jayxu.com/?p=13131</guid>
		<description><![CDATA[Spring Source真的是个很神奇的开源社区，从《J2EE without EJB》开始为大家所知晓，到把IoC、AOP大范围地带到大家的开发模型中，再到和Hibernate、Struts组成每个Java函授学校必教的“SSH”组合，从此绿遍大江南北…… 在国内，大马路上随便抓一个Java程序员，10个里有9个半知道Spring；有9个知道怎么把SSH“组装”到一起（是的，“组装”，和流水线上的蓝领熟练工没有本质区别）；有9个半知道IoC、AOP；有2个知道IoC和AOP的本质；有3个知道Spring其实分为core、context、orm、tx等不同子框架；有1个知道Spring 3之后可以用annotation取代之前版本大部分的xml配置；有2个知道Spring mvc和security；有半个知道其实Spring的子框架远不止这些……这就是我们当前Java码农们的实际情况。而造成这一局面的，我谓之三害——高校计算机系功利的培养模式、以北大青鸟为首的各种速成班的祸害和程序员们自己懒惰、人云亦云、拒绝思考的慢性自杀 牢骚发完，本文将通过介绍Spring的两个子框架：Spring LDAP和Spring LDAP ODM，来实现对LDAP的操作 项目主页：http://www.springsource.org/ldap，该框架通过提供和ORM中相似的机制对LDAP相关操作进行封装，主要包括： 类比SessionFactory的LdapContextSource 类比HibernateTemplate等的LdapTemplate 伪事务支持，能否与tx框架的TransactionManager混用未知 类比JPA的使用@Entry、@Attribute、@Id标注的ODM——Object Directory Mapping 本文（原先）目标：使用ODM将User类与LDAP中的实体进行映射，LDAP中的实体的objectClass包括inetOrgPerson，organizationalPerson，person，shadowAccount和top 相关配置文件、代码在http://static.springsource.org/spring-ldap/docs/1.3.x/reference/html/上已经很详细了，这里只是做一点旁注，全部来自于这几天和Spring LDAP的斗智斗勇 对于shadowAccount的shadowLastChange域，值是一个整形，至于具体含义网上没有找到解释，试了几次之后发现是从1970-1-1 00:00:00至今的天数，如果model里定义的是Date类型的话，需要自行实现org.springframework.ldap.odm.typeconversion.impl.Converter接口进行转换 对于@Entry标记，其中的objectClasses定义必须与objectClass完全一致。在新建和查询object时，ODM会根据此标记进行匹配，无需再指定objectClass 每个entry必须指定@Id字段，类型为javax.naming.Name，其实就是DN。但是若在LdapContextSource中指定了base，则DN将会按照base截取相对路径。比如，DN为cn=user,ou=users,dc=jayxu,dc=com，base为dc=jayxu,dc=com，则取出的user对象DN为cn=user,ou=users 如果使用Spring MVC对LDAP对象进行JSON序列化，必须注意javax.naming.Name中的某些字段无法被序列化，所以在转换成JSON之前需要将DN置null。一种方法是使用@JsonIgnoreProperties标记model类，比如：@JsonIgnoreProperties(“dn”) 对于不需要与LDAP进行映射的字段使用@Transient进行标记 考虑使用org.springframework.ldap.pool.factory.PoolingContextSource引入连接池 如果事务中需要与JDBC进行互操作，需要使用org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager作为tx manager 原创内容，转载请注明： 转载自拈花微笑 本文链接地址: 使用Spring LDAP ODM操作LDAP<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F02%2F02%2F13237%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">有关Java泛型的类型擦除（type erasing）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2009%2F12%2F02%2F1899%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Apache给EJB 3.1（JSR #318）投了否决票</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>Spring Source真的是个很神奇的开源社区，从《J2EE without EJB》开始为大家所知晓，到把IoC、AOP大范围地带到大家的开发模型中，再到和Hibernate、Struts组成每个Java函授学校必教的“SSH”组合，从此绿遍大江南北……</p>
<p>在国内，大马路上随便抓一个Java程序员，10个里有9个半知道Spring；有9个知道怎么把SSH“组装”到一起（是的，“组装”，和流水线上的蓝领熟练工没有本质区别）；有9个半知道IoC、AOP；有2个知道IoC和AOP的本质；有3个知道Spring其实分为core、context、orm、tx等不同子框架；有1个知道Spring 3之后可以用annotation取代之前版本大部分的xml配置；有2个知道Spring mvc和security；有半个知道其实Spring的子框架远不止这些……这就是我们当前Java码农们的实际情况。而造成这一局面的，我谓之三害——高校计算机系功利的培养模式、以北大青鸟为首的各种速成班的祸害和程序员们自己懒惰、人云亦云、拒绝思考的慢性自杀</p>
<p>牢骚发完，本文将通过介绍Spring的两个子框架：Spring LDAP和Spring LDAP ODM，来实现对LDAP的操作</p>
<p>项目主页：<a href="http://www.springsource.org/ldap" target="_blank">http://www.springsource.org/ldap</a>，该框架通过提供和ORM中相似的机制对LDAP相关操作进行封装，主要包括：</p>
<ul>
<li>类比SessionFactory的LdapContextSource</li>
<li>类比HibernateTemplate等的LdapTemplate</li>
<li>伪事务支持，能否与tx框架的TransactionManager混用未知</li>
<li>类比JPA的使用@Entry、@Attribute、@Id标注的ODM——Object Directory Mapping</li>
</ul>
<p>本文（原先）目标：使用ODM将User类与LDAP中的实体进行映射，LDAP中的实体的objectClass包括inetOrgPerson，organizationalPerson，person，shadowAccount和top</p>
<p>相关配置文件、代码在<a href="http://static.springsource.org/spring-ldap/docs/1.3.x/reference/html/" target="_blank">http://static.springsource.org/spring-ldap/docs/1.3.x/reference/html/</a>上已经很详细了，这里只是做一点旁注，全部来自于这几天和Spring LDAP的斗智斗勇</p>
<ul>
<li>对于shadowAccount的shadowLastChange域，值是一个整形，至于具体含义网上没有找到解释，试了几次之后发现是从1970-1-1 00:00:00至今的天数，如果model里定义的是Date类型的话，需要自行实现org.springframework.ldap.odm.typeconversion.impl.Converter接口进行转换</li>
<li>对于@Entry标记，其中的objectClasses定义必须与objectClass完全一致。在新建和查询object时，ODM会根据此标记进行匹配，无需再指定objectClass</li>
<li>每个entry必须指定@Id字段，类型为javax.naming.Name，其实就是DN。但是若在LdapContextSource中指定了base，则DN将会按照base截取相对路径。比如，DN为cn=user,ou=users,dc=jayxu,dc=com，base为dc=jayxu,dc=com，则取出的user对象DN为cn=user,ou=users</li>
<li>如果使用Spring MVC对LDAP对象进行JSON序列化，必须注意javax.naming.Name中的某些字段无法被序列化，所以在转换成JSON之前需要将DN置null。一种方法是使用@JsonIgnoreProperties标记model类，比如：@JsonIgnoreProperties(“dn”)</li>
<li>对于不需要与LDAP进行映射的字段使用@Transient进行标记</li>
<li>考虑使用org.springframework.ldap.pool.factory.PoolingContextSource引入连接池</li>
<li>如果事务中需要与JDBC进行互操作，需要使用org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager作为tx manager</li>
</ul>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2011/12/14/13131/">使用Spring LDAP ODM操作LDAP</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&title=%E4%BD%BF%E7%94%A8Spring+LDAP+ODM%E6%93%8D%E4%BD%9CLDAP" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F02%2F02%2F13237%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">有关Java泛型的类型擦除（type erasing）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2009%2F12%2F02%2F1899%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Apache给EJB 3.1（JSR #318）投了否决票</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2011/12/14/13131/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>谨慎升级至Hibernate 3.6</title>
		<link>http://www.jayxu.com/2010/11/24/10280/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e8%25b0%25a8%25e6%2585%258e%25e5%258d%2587%25e7%25ba%25a7%25e8%2587%25b3hibernate-3-6</link>
		<comments>http://www.jayxu.com/2010/11/24/10280/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 02:39:01 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.jayxu.com/?p=10280</guid>
		<description><![CDATA[今天在我们的项目中发现了Hibernate 3.6的一个bug：如果把某个字段标记为“@Lob”（MySQL中将对应至LONGTEXT），并且在插入对象时该字段的值为空（null或”"），Hibernate将抛出下面的异常，导致插入失败： Could not synchronize database state with session org.hibernate.exception.GenericJDBCException: could not insert: &#91;...&#93; at org.hibernate.exception.SQLStateConverter.handledNonSpecificException&#40;SQLStateConverter.java:140&#41; at org.hibernate.exception.SQLStateConverter.convert&#40;SQLStateConverter.java:128&#41; at org.hibernate.exception.JDBCExceptionHelper.convert&#40;JDBCExceptionHelper.java:66&#41; at org.hibernate.persister.entity.AbstractEntityPersister.insert&#40;AbstractEntityPersister.java:2436&#41; at org.hibernate.persister.entity.AbstractEntityPersister.insert&#40;AbstractEntityPersister.java:2856&#41; at org.hibernate.action.EntityInsertAction.execute&#40;EntityInsertAction.java:79&#41; at org.hibernate.engine.ActionQueue.execute&#40;ActionQueue.java:273&#41; at org.hibernate.engine.ActionQueue.executeActions&#40;ActionQueue.java:265&#41; at org.hibernate.engine.ActionQueue.executeActions&#40;ActionQueue.java:184&#41; at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions&#40;AbstractFlushingEventListener.java:321&#41; at org.hibernate.event.def.DefaultFlushEventListener.onFlush&#40;DefaultFlushEventListener.java:51&#41; at org.hibernate.impl.SessionImpl.flush&#40;SessionImpl.java:1216&#41; at org.hibernate.ejb.AbstractEntityManagerImpl.flush&#40;AbstractEntityManagerImpl.java:795&#41; at ... &#40;...&#41; Caused by: java.sql.SQLException: Start position &#91;1&#93; cannot exceed overall CLOB length &#91;0&#93; <a href="http://www.jayxu.com/2010/11/24/10280/"> read more <span class="meta-nav">&#187;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>今天在我们的项目中发现了Hibernate 3.6的一个bug：如果把某个字段标记为“@Lob”（MySQL中将对应至LONGTEXT），并且在插入对象时该字段的值为空（null或”"），Hibernate将抛出下面的异常，导致插入失败：</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar" style="right: 15px;" ><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"style=" height: 504px;"><div class="java"><ol><li class="li1"><div class="de1">Could not synchronize database state with session</div></li>
<li class="li2"><div class="de2">org.<span class="me1">hibernate</span>.<span class="me1">exception</span>.<span class="me1">GenericJDBCException</span><span class="sy0">:</span> could not insert<span class="sy0">:</span> <span class="br0">&#91;</span>...<span class="br0">&#93;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">exception</span>.<span class="me1">SQLStateConverter</span>.<span class="me1">handledNonSpecificException</span><span class="br0">&#40;</span>SQLStateConverter.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">140</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">exception</span>.<span class="me1">SQLStateConverter</span>.<span class="me1">convert</span><span class="br0">&#40;</span>SQLStateConverter.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">128</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">exception</span>.<span class="me1">JDBCExceptionHelper</span>.<span class="me1">convert</span><span class="br0">&#40;</span>JDBCExceptionHelper.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">66</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">persister</span>.<span class="me1">entity</span>.<span class="me1">AbstractEntityPersister</span>.<span class="me1">insert</span><span class="br0">&#40;</span>AbstractEntityPersister.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">2436</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">persister</span>.<span class="me1">entity</span>.<span class="me1">AbstractEntityPersister</span>.<span class="me1">insert</span><span class="br0">&#40;</span>AbstractEntityPersister.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">2856</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">action</span>.<span class="me1">EntityInsertAction</span>.<span class="me1">execute</span><span class="br0">&#40;</span>EntityInsertAction.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">79</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">engine</span>.<span class="me1">ActionQueue</span>.<span class="me1">execute</span><span class="br0">&#40;</span>ActionQueue.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">273</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">engine</span>.<span class="me1">ActionQueue</span>.<span class="me1">executeActions</span><span class="br0">&#40;</span>ActionQueue.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">265</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">engine</span>.<span class="me1">ActionQueue</span>.<span class="me1">executeActions</span><span class="br0">&#40;</span>ActionQueue.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">184</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">event</span>.<span class="me1">def</span>.<span class="me1">AbstractFlushingEventListener</span>.<span class="me1">performExecutions</span><span class="br0">&#40;</span>AbstractFlushingEventListener.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">321</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">event</span>.<span class="me1">def</span>.<span class="me1">DefaultFlushEventListener</span>.<span class="me1">onFlush</span><span class="br0">&#40;</span>DefaultFlushEventListener.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">51</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">impl</span>.<span class="me1">SessionImpl</span>.<span class="me1">flush</span><span class="br0">&#40;</span>SessionImpl.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">1216</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">ejb</span>.<span class="me1">AbstractEntityManagerImpl</span>.<span class="me1">flush</span><span class="br0">&#40;</span>AbstractEntityManagerImpl.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">795</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at ...</div></li>
<li class="li1"><div class="de1"><span class="br0">&#40;</span>...<span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">Caused by<span class="sy0">:</span> java.<span class="me1">sql</span>.<span class="kw3">SQLException</span><span class="sy0">:</span> Start position <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> cannot exceed overall CLOB length <span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">engine</span>.<span class="me1">jdbc</span>.<span class="me1">ClobProxy</span>.<span class="me1">invoke</span><span class="br0">&#40;</span>ClobProxy.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">146</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at $Proxy52.<span class="me1">getSubString</span><span class="br0">&#40;</span>Unknown Source<span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at com.<span class="me1">mysql</span>.<span class="me1">jdbc</span>.<span class="kw3">PreparedStatement</span>.<span class="me1">setClob</span><span class="br0">&#40;</span><span class="kw3">PreparedStatement</span>.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">3542</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at com.<span class="me1">mysql</span>.<span class="me1">jdbc</span>.<span class="me1">jdbc2</span>.<span class="me1">optional</span>.<span class="me1">PreparedStatementWrapper</span>.<span class="me1">setClob</span><span class="br0">&#40;</span>PreparedStatementWrapper.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">299</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">apache</span>.<span class="me1">commons</span>.<span class="me1">dbcp</span>.<span class="me1">DelegatingPreparedStatement</span>.<span class="me1">setClob</span><span class="br0">&#40;</span>DelegatingPreparedStatement.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">187</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">apache</span>.<span class="me1">commons</span>.<span class="me1">dbcp</span>.<span class="me1">DelegatingPreparedStatement</span>.<span class="me1">setClob</span><span class="br0">&#40;</span>DelegatingPreparedStatement.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">187</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">type</span>.<span class="me1">descriptor</span>.<span class="me1">sql</span>.<span class="me1">ClobTypeDescriptor</span>$1.<span class="me1">doBind</span><span class="br0">&#40;</span>ClobTypeDescriptor.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">60</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">type</span>.<span class="me1">descriptor</span>.<span class="me1">sql</span>.<span class="me1">BasicBinder</span>.<span class="me1">bind</span><span class="br0">&#40;</span>BasicBinder.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">89</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">type</span>.<span class="me1">AbstractStandardBasicType</span>.<span class="me1">nullSafeSet</span><span class="br0">&#40;</span>AbstractStandardBasicType.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">282</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">type</span>.<span class="me1">AbstractStandardBasicType</span>.<span class="me1">nullSafeSet</span><span class="br0">&#40;</span>AbstractStandardBasicType.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">277</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">type</span>.<span class="me1">AbstractSingleColumnStandardBasicType</span>.<span class="me1">nullSafeSet</span><span class="br0">&#40;</span>AbstractSingleColumnStandardBasicType.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">85</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">at org.<span class="me1">hibernate</span>.<span class="me1">persister</span>.<span class="me1">entity</span>.<span class="me1">AbstractEntityPersister</span>.<span class="me1">dehydrate</span><span class="br0">&#40;</span>AbstractEntityPersister.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">2166</span><span class="br0">&#41;</span></div></li>
<li class="li1"><div class="de1">at org.<span class="me1">hibernate</span>.<span class="me1">persister</span>.<span class="me1">entity</span>.<span class="me1">AbstractEntityPersister</span>.<span class="me1">insert</span><span class="br0">&#40;</span>AbstractEntityPersister.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">2412</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2">... <span class="nu0">70</span> more</div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert: [...]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2436)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2856)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
at ...
(...)
Caused by: java.sql.SQLException: Start position [1] cannot exceed overall CLOB length [0]
at org.hibernate.engine.jdbc.ClobProxy.invoke(ClobProxy.java:146)
at $Proxy52.getSubString(Unknown Source)
at com.mysql.jdbc.PreparedStatement.setClob(PreparedStatement.java:3542)
at com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper.setClob(PreparedStatementWrapper.java:299)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setClob(DelegatingPreparedStatement.java:187)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setClob(DelegatingPreparedStatement.java:187)
at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$1.doBind(ClobTypeDescriptor.java:60)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:89)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:282)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:277)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:85)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2166)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2412)
... 70 more</pre></div></div>

<p>经google，Hibernate的JIRA上已经记录了该bug：<a href="https://hibernate.onjira.com//browse/HHH-5704" target="_blank">http://opensource.atlassian.com/projects/hibernate/browse/HHH-5704</a>，10/31刚刚创建。因此在这里建议使用Hibernate的项目暂时不使用3.6版本，而是降级至3.5.2（我们的另一个项目使用了3.5.2，没有发生上述问题）
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2010/11/24/10280/">谨慎升级至Hibernate 3.6</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&title=%E8%B0%A8%E6%85%8E%E5%8D%87%E7%BA%A7%E8%87%B3Hibernate+3.6" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2010/11/24/10280/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>如何在Hibernate中让SQLServer使用nvarchar代替varchar</title>
		<link>http://www.jayxu.com/2010/11/23/10275/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e5%25a6%2582%25e4%25bd%2595%25e5%259c%25a8hibernate%25e4%25b8%25ad%25e8%25ae%25a9sqlserver%25e4%25bd%25bf%25e7%2594%25a8nvarchar%25e4%25bb%25a3%25e6%259b%25bfvarchar</link>
		<comments>http://www.jayxu.com/2010/11/23/10275/#comments</comments>
		<pubDate>Tue, 23 Nov 2010 08:19:38 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[dialect]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlserver]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.jayxu.com/?p=10275</guid>
		<description><![CDATA[有关SQLServer中varchar和nvarchar的区别可以直接去google。一般在中文系统中应该使用nvarchar作为字符串的对应类型，但是Hibernate中的默认实现SQLServerDialect使用了varchar。以下方法可以简单地转为使用nvarchar： 自己写一个dialect，继承SQLServerDialect，在构造器中将原先varchar类型的注册声明覆盖： registerColumnType&#40;Types.VARCHAR, &#34;nvarchar($l)&#34;&#41;; registerColumnType(Types.VARCHAR, "nvarchar($l)"); 千万注意，“$”后面的是字段长度的占位符，是“l(ength)”，而不是数字“1”（因为看hibernate的doc时没分清“l”和“1”，浪费了我一上午去找原因） 然后在hibernate的配置文件中将hibernate.dialect的值设为你的dialect实现类就OK了 原创内容，转载请注明： 转载自拈花微笑 本文链接地址: 如何在Hibernate中让SQLServer使用nvarchar代替varchar<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2008%2F11%2F09%2F59%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">小心Derby的ResultSet陷阱</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2009%2F03%2F02%2F1425%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Java RMI中的NoSuchObjectException</font>
                    </a>
                </td>
            </tr>
        <br/>
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fbfya.com%2Fsql-wordpress-keywords&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">SQL 查询包含某些文字的内容 如wordpress查询所有带图片的文章 (@bfya)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.hui-wang.info%2F2012%2F01%2F08%2Fjava-enum-and-polymorphism%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Java的枚举和接口 (@hui-wang)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.dedeadmin.com%2F%3Fp%3D1967&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">用过SQL语句获取织梦DedeCMS每个栏目各有多少文章 (@dedeadmin)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.dedeadmin.com%2F%3Fp%3D316&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">利用SQL语句删除或修改织梦DedeCms数据库内容 (@dedeadmin)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.swhack.cn%2Farchives%2F173.html&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">SQL语句导入导出大全 (@swhack)</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>有关SQLServer中varchar和nvarchar的区别可以直接去google。一般在中文系统中应该使用nvarchar作为字符串的对应类型，但是Hibernate中的默认实现SQLServerDialect使用了varchar。以下方法可以简单地转为使用nvarchar：</p>
<p>自己写一个dialect，继承SQLServerDialect，在构造器中将原先varchar类型的注册声明覆盖：</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar"><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"><div class="java"><ol><li class="li1"><div class="de1">registerColumnType<span class="br0">&#40;</span><span class="kw3">Types</span>.<span class="me1">VARCHAR</span>, <span class="st0">&quot;nvarchar($l)&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">registerColumnType(Types.VARCHAR, "nvarchar($l)");</pre></div></div>

<p>千万注意，“$”后面的是字段长度的占位符，是“l(ength)”，而不是数字“1”（因为看hibernate的doc时没分清“l”和“1”，浪费了我一上午去找原因）</p>
<p>然后在hibernate的配置文件中将hibernate.dialect的值设为你的dialect实现类就OK了
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2010/11/23/10275/">如何在Hibernate中让SQLServer使用nvarchar代替varchar</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F&title=%E5%A6%82%E4%BD%95%E5%9C%A8Hibernate%E4%B8%AD%E8%AE%A9SQLServer%E4%BD%BF%E7%94%A8nvarchar%E4%BB%A3%E6%9B%BFvarchar" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2008%2F11%2F09%2F59%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">小心Derby的ResultSet陷阱</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2009%2F03%2F02%2F1425%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Java RMI中的NoSuchObjectException</font>
                    </a>
                </td>
            </tr>
        <br/>
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">来自无觅网络的相关文章：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fbfya.com%2Fsql-wordpress-keywords&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">SQL 查询包含某些文字的内容 如wordpress查询所有带图片的文章 (@bfya)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.hui-wang.info%2F2012%2F01%2F08%2Fjava-enum-and-polymorphism%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Java的枚举和接口 (@hui-wang)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.dedeadmin.com%2F%3Fp%3D1967&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">用过SQL语句获取织梦DedeCMS每个栏目各有多少文章 (@dedeadmin)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.dedeadmin.com%2F%3Fp%3D316&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">利用SQL语句删除或修改织梦DedeCms数据库内容 (@dedeadmin)</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.swhack.cn%2Farchives%2F173.html&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">SQL语句导入导出大全 (@swhack)</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2010/11/23/10275/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring之经验教训（一）</title>
		<link>http://www.jayxu.com/2010/07/18/2373/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring%25e4%25b9%258b%25e7%25bb%258f%25e9%25aa%258c%25e6%2595%2599%25e8%25ae%25ad%25ef%25bc%2588%25e4%25b8%2580%25ef%25bc%2589</link>
		<comments>http://www.jayxu.com/2010/07/18/2373/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 17:27:24 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[struts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[transaction]]></category>
		<category><![CDATA[依赖注入]]></category>

		<guid isPermaLink="false">http://jayxu.com/?p=2373</guid>
		<description><![CDATA[在现在的项目中我们使用了spring + hibernate + struts的架构，在享受aop, orm, ioc, di带来的种种便利的同时，我们亦遇到了很多莫名其妙或者说刻骨铭心的教训，今天先整理两点，日后继续补充 经验一：时刻牢记，spring、hibernate对对象 进行了动态代理，尽量不要试图在动态代理后的对象上进行反射，尤其是field！ 不管是hibernate的orm还是spring的声明式事务管理，都对原来的pojo、dao进行了动态代理。虽然s、h&#8220;号称&#8221;动态代理做得天衣无缝且无色无味，但是，那只是在&#8220;绝大多数情况下&#8221;，如果想对动态代理后的对象进行反射，麻烦便来了，代码片段： public static void setCreditInfoStatus&#40;CreditInfo info, CreditType type, CreditValidateStatus status&#41; &#123; &#160; &#160; ... &#160; &#160; &#160; Field&#91;&#93; fields = CreditInfo.class.getDeclaredFields&#40;&#41;; &#160; &#160; &#160; for &#40;Field f : fields&#41; &#123; &#160; &#160; &#160; &#160; if &#40;f.isAnnotationPresent&#40;Credit.class&#41; &#38;&#38; f.getAnnotation&#40;Credit.class&#41;.value&#40;&#41; == type&#41; &#123; &#160; &#160; &#160; &#160; <a href="http://www.jayxu.com/2010/07/18/2373/"> read more <span class="meta-nav">&#187;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F02%2F02%2F13237%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">有关Java泛型的类型擦除（type erasing）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>在现在的项目中我们使用了spring + hibernate + struts的架构，在享受aop, orm, ioc, di带来的种种便利的同时，我们亦遇到了很多莫名其妙或者说刻骨铭心的教训，今天先整理两点，日后继续补充</p>
<h4>经验一：时刻牢记，spring、hibernate对对象 进行了动态代理，尽量不要试图在动态代理后的对象上进行反射，尤其是field！</h4>
<p>不管是hibernate的orm还是spring的声明式事务管理，都对原来的pojo、dao进行了动态代理。虽然s、h&ldquo;号称&rdquo;动态代理做得天衣无缝且无色无味，但是，那只是在&ldquo;绝大多数情况下&rdquo;，如果想对动态代理后的对象进行反射，麻烦便来了，代码片段：</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar"><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"><div class="java"><ol><li class="li1"><div class="de1"><span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> setCreditInfoStatus<span class="br0">&#40;</span>CreditInfo info, CreditType type, CreditValidateStatus status<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; ...</div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; <span class="kw3">Field</span><span class="br0">&#91;</span><span class="br0">&#93;</span> fields <span class="sy0">=</span> CreditInfo.<span class="kw1">class</span>.<span class="me1">getDeclaredFields</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw3">Field</span> f <span class="sy0">:</span> fields<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>f.<span class="me1">isAnnotationPresent</span><span class="br0">&#40;</span>Credit.<span class="kw1">class</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> f.<span class="me1">getAnnotation</span><span class="br0">&#40;</span>Credit.<span class="kw1">class</span><span class="br0">&#41;</span>.<span class="me1">value</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> type<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">PropertyDescriptor</span> pd <span class="sy0">=</span> BeanUtils.<span class="me1">getPropertyDescriptor</span><span class="br0">&#40;</span>CreditInfo.<span class="kw1">class</span>, f.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>pd <span class="sy0">!=</span> <span class="kw2">null</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">public static void setCreditInfoStatus(CreditInfo info, CreditType type, CreditValidateStatus status) {
    ...

    Field[] fields = CreditInfo.class.getDeclaredFields();

    for (Field f : fields) {
        if (f.isAnnotationPresent(Credit.class) &amp;&amp; f.getAnnotation(Credit.class).value() == type) {
            PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(CreditInfo.class, f.getName());
            if (pd != null)  {
                ...
            }
            break;
        }
    }
}</pre></div></div>

<p>原先第7行写为 info.getClass()，乍一看与现有的代码功能上是一样的，但是别忘了让人又恨又爱的动态代理！上面代码返回的是真正的CreditInfo class，而左边的返回的是动态代理后的class，即意味着，第一个&ldquo;if&rdquo;永远返回的是false，除非动态代理后的对象的field上附带了原有的annotation</p>
<h4>经验二：spring的声明式事务管理确很强大，强大到可以支持多线程，但是，结合上一点，不要在线程中调用this中的事务方法</h4>
<p>声明式事务，即spring中使用aop织入或 @Transactional标记的方法注入事务容器，码农们可以完全不用操心何时begin，何时commit，何时 rollback，有没有嵌套，绝对傻瓜级的编程模型。但是，牢记，spring中的aop、annotation都是使用动态代理实现的，即，如果没有经过动态代理便也没有了事务管理，代码片段：</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar"><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"><div class="java"><ol><li class="li1"><div class="de1"><span class="kw1">public</span> <span class="kw1">class</span> AutomatedService<span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; ...</div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; <span class="me1">loanQueue</span>.<span class="me1">execute</span><span class="br0">&#40;</span><span class="kw1">new</span> <span class="kw3">Runnable</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; @Override</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> run<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">Thread</span> thread <span class="sy0">=</span> &nbsp;<span class="kw3">Thread</span>.<span class="me1">currentThread</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="sy0">!</span>thread.<span class="me1">isInterrupted</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loanService.<span class="me1">checkNSetLoanStatus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">Thread</span>.<span class="me1">sleep</span><span class="br0">&#40;</span>loanStatusCheckIntervalMinutes <span class="sy0">*</span> Consts.<span class="me1">ONE_MINUTE</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span><span class="kw3">InterruptedException</span> ex<span class="br0">&#41;</span> <span class="br0">&#123;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thread.<span class="me1">interrupt</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; ...</div></li>
<li class="li1"><div class="de1"><span class="br0">&#125;</span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">public class AutomatedService{
    ...

    loanQueue.execute(new Runnable() {

        @Override
        public void run() {
            Thread thread =  Thread.currentThread();

            while (!thread.isInterrupted()) {
                try {
                    loanService.checkNSetLoanStatus();

                    Thread.sleep(loanStatusCheckIntervalMinutes * Consts.ONE_MINUTE);
                } catch (InterruptedException ex) {
                    thread.interrupt();
                }
            }
        }
    });

    ...
}</pre></div></div>

<p>原先的checkNSetLoanStatus方法是定义在AutomatedService中的，且标记了@Transactional。 但是，令人发指的是，checkNSetLoanStatus中的事务没有被提交。在痛定思痛仔细回想了spring的声明式事务的本质后，豁然发现当调用this.checkNSetLoanStatus()时，并没有被织入事务管理。spring还没有霸道到或者说聪明到对this进行动态代理，于是将该方法移至其它service并注入，问题解决</p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2010/07/18/2373/">Spring之经验教训（一）</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&title=Spring%E4%B9%8B%E7%BB%8F%E9%AA%8C%E6%95%99%E8%AE%AD%EF%BC%88%E4%B8%80%EF%BC%89" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之Hibernate+JBoss Treecache实现Hibernate集群</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F02%2F02%2F13237%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">有关Java泛型的类型擦除（type erasing）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F23%2F10275&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">如何在Hibernate中让SQLServer使用nvarchar代替varchar</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2010/07/18/2373/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring之Hibernate+JBoss Treecache实现Hibernate集群</title>
		<link>http://www.jayxu.com/2010/06/09/2342/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring%25e4%25b9%258bhibernatejboss-treecache%25e9%2585%258d%25e7%25bd%25ae%25e5%25ae%259e%25e7%258e%25b0hibernate%25e9%259b%2586%25e7%25be%25a4</link>
		<comments>http://www.jayxu.com/2010/06/09/2342/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 06:53:28 +0000</pubDate>
		<dc:creator>Jay</dc:creator>
				<category><![CDATA[技术生活]]></category>
		<category><![CDATA[未分类]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[treecache]]></category>

		<guid isPermaLink="false">http://jayxu.com/?p=2342</guid>
		<description><![CDATA[本文版权归本人所有，任何转载请标明出处 Hibernate作为一个ORM框架可以说已经做到了极致，但是绝大多数情况下Hibernate都被应用于单容器环境中，以至于互联网上能够找到的在集群环境中使用的参考少之又少，和Spring整合的就更别说了。实际上，Hibernate可以使用JBoss的Treecache作为二级缓存以支持分布式集群 本文将使用Spring 3.0.2框架集成Hibernate 3.5.2，并将Treecache 3.2.5作为二级缓存 Spring配置 &#60;context:annotation-config /&#62; &#160; &#60;bean id=&#34;sessionFactory&#34; class=&#34;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&#34;&#62; &#160; &#160; &#160; &#160; ... &#60;property name=&#34;hibernateProperties&#34;&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#60;value&#62; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hibernate.dialect=org.hibernate.dialect.MySQL5Dialect &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hibernate.cache.use_second_level_cache=true &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hibernate.show_sql=true &#160; <a href="http://www.jayxu.com/2010/06/09/2342/"> read more <span class="meta-nav">&#187;</span></a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p><strong><em>本文版权归本人所有，任何转载请标明出处</em></strong></p>
<p>Hibernate作为一个ORM框架可以说已经做到了极致，但是绝大多数情况下Hibernate都被应用于单容器环境中，以至于互联网上能够找到的在集群环境中使用的参考少之又少，和Spring整合的就更别说了。实际上，Hibernate可以使用JBoss的Treecache作为二级缓存以支持分布式集群</p>
<p>本文将使用Spring 3.0.2框架集成Hibernate 3.5.2，并将<a href="http://www.jboss.org/jbosscache" target="_blank">Treecache</a> 3.2.5作为二级缓存</p>
<p>Spring配置</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar"><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"><div class="xml"><ol><li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;context:annotation-config</span> <span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp;</div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;sessionFactory&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;</span><span class="re2">&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; ...</div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;hibernateProperties&quot;</span><span class="re2">&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;value<span class="re2">&gt;</span></span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.dialect=org.hibernate.dialect.MySQL5Dialect</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.cache.use_second_level_cache=true</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.show_sql=true</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.cache.region.factory_class=org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.cache.use_query_cache=true</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hibernate.hbm2ddl.auto=update</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/value<span class="re2">&gt;</span></span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">&lt;context:annotation-config /&gt;

&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
        ...
&lt;property name="hibernateProperties"&gt;
            &lt;value&gt;
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.cache.use_second_level_cache=true
                hibernate.show_sql=true
                hibernate.cache.region.factory_class=org.hibernate.cache.jbc2.SharedJBossCacheRegionFactory
                hibernate.cache.use_query_cache=true
                hibernate.hbm2ddl.auto=update
            &lt;/value&gt;
        &lt;/property&gt;
    &lt;/bean&gt;</pre></div></div>

<p>其中第1行打开Spring的annotation支持，即使用annotation替代之前版本中的大量XML配置（貌似这个选项在Spring 2.5中就被引入，但不知道是因为大家懒还是配置文件的基本内容都是在互联网上Ctrl C+V 2.5之前的代码，反正互联网上一搜Spring的配置满眼都是XML片段）；第8行打开二级缓存；第10行选择Treecache作为缓存region工厂的实现类</p>
<p>对于需要被缓存的实体，只需要在类定义上加上@Cache注释，如：</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar"><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"><div class="java"><ol><li class="li1"><div class="de1">@Cache<span class="br0">&#40;</span>usage <span class="sy0">=</span> CacheConcurrencyStrategy.<span class="me1">TRANSACTIONAL</span><span class="br0">&#41;</span></div></li>
<li class="li2"><div class="de2"><span class="kw1">public</span> <span class="kw1">class</span> MyModel <span class="br0">&#123;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; ...</div></li>
<li class="li2"><div class="de2"><span class="br0">&#125;</span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class MyModel {
    ...
}</pre></div></div>

<p>由于Treecache只支持只读缓存和事务缓存，因此这里usage只能设为READ_ONLY或TRANSACTIONAL，但需要注意的是设为只读缓存时不能打开Hibernate的query cache</p>
<p>最后是Treecache的配置，如果配置文件名是treecache.xml且在classpath中则无需指定，否则需要在上述Spring配置的代码段中指定。至于具体配置内容，可参考Treecache源代码中的sample，调优方式可参考Treecache文档，这里给一个sample</p>

<div class="bwp-syntax-block clearfix">
<div class="bwp-syntax-toolbar" style="right: 15px;" ><div class="bwp-syntax-control"><a href="javascript:;" class="bwp-syntax-source-switch" title="View Source Code"></a></div></div>
<div class="bwp-syntax-wrapper clearfix bwp-syntax-advanced"style=" height: 504px;"><div class="xml"><ol><li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;UTF-8&quot;</span><span class="re2">?&gt;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"><span class="re1">&lt;jbosscache</span> <span class="re0">xmlns:xsi</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> <span class="re0">xmlns</span>=<span class="st0">&quot;urn:jboss:jbosscache-core:config:3.2&quot;</span><span class="re2">&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp;</div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp;<span class="sc-1">&lt;!-- Configure the TransactionManager --&gt;</span></div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;transaction</span> <span class="re0">transactionManagerLookupClass</span>=<span class="st0">&quot;org.jboss.cache.transaction.GenericTransactionManagerLookup&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;clustering</span> <span class="re0">mode</span>=<span class="st0">&quot;replication&quot;</span><span class="re2">&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!--</span></div></li>
<li class="li1"><div class="de1"><span class="sc-1"> &nbsp; &nbsp; &nbsp; timeout: The max amount of time (in milliseconds) we wait until the</span></div></li>
<li class="li2"><div class="de2"><span class="sc-1"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state (i.e. the contents of the cache) are retrieved from</span></div></li>
<li class="li1"><div class="de1"><span class="sc-1"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; existing members in a clustered environment</span></div></li>
<li class="li2"><div class="de2"><span class="sc-1"> &nbsp; &nbsp; &nbsp;--&gt;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;stateRetrieval</span> <span class="re0">timeout</span>=<span class="st0">&quot;20000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!-- JGroups protocol stack properties. --&gt;</span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;jgroupsConfig<span class="re2">&gt;</span></span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;udp</span> <span class="re0">discard_incompatible_packets</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">enable_bundling</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">enable_diagnostics</span>=<span class="st0">&quot;false&quot;</span> <span class="re0">ip_ttl</span>=<span class="st0">&quot;2&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">loopback</span>=<span class="st0">&quot;false&quot;</span> <span class="re0">max_bundle_size</span>=<span class="st0">&quot;64000&quot;</span> <span class="re0">max_bundle_timeout</span>=<span class="st0">&quot;30&quot;</span> <span class="re0">mcast_addr</span>=<span class="st0">&quot;224.12.12.12&quot;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">mcast_port</span>=<span class="st0">&quot;45588&quot;</span> <span class="re0">mcast_recv_buf_size</span>=<span class="st0">&quot;100000000&quot;</span> <span class="re0">mcast_send_buf_size</span>=<span class="st0">&quot;640000&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">oob_thread_pool.enabled</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">oob_thread_pool.keep_alive_time</span>=<span class="st0">&quot;10000&quot;</span> <span class="re0">oob_thread_pool.max_threads</span>=<span class="st0">&quot;20&quot;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">oob_thread_pool.min_threads</span>=<span class="st0">&quot;8&quot;</span> <span class="re0">oob_thread_pool.queue_enabled</span>=<span class="st0">&quot;false&quot;</span> <span class="re0">oob_thread_pool.queue_max_size</span>=<span class="st0">&quot;10&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">oob_thread_pool.rejection_policy</span>=<span class="st0">&quot;Run&quot;</span> <span class="re0">thread_naming_pattern</span>=<span class="st0">&quot;pl&quot;</span> <span class="re0">thread_pool.enabled</span>=<span class="st0">&quot;true&quot;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">thread_pool.keep_alive_time</span>=<span class="st0">&quot;10000&quot;</span> <span class="re0">thread_pool.max_threads</span>=<span class="st0">&quot;15&quot;</span> <span class="re0">thread_pool.min_threads</span>=<span class="st0">&quot;8&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">thread_pool.queue_enabled</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">thread_pool.queue_max_size</span>=<span class="st0">&quot;100000&quot;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">thread_pool.rejection_policy</span>=<span class="st0">&quot;Discard&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">tos</span>=<span class="st0">&quot;8&quot;</span> <span class="re0">ucast_recv_buf_size</span>=<span class="st0">&quot;20000000&quot;</span> <span class="re0">ucast_send_buf_size</span>=<span class="st0">&quot;640000&quot;</span> <span class="re0">use_concurrent_stack</span>=<span class="st0">&quot;true&quot;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">use_incoming_packet_handler</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"><span class="re1">&lt;ping</span> <span class="re0">num_initial_members</span>=<span class="st0">&quot;3&quot;</span> <span class="re0">timeout</span>=<span class="st0">&quot;2000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;merge2</span> <span class="re0">max_interval</span>=<span class="st0">&quot;30000&quot;</span> <span class="re0">min_interval</span>=<span class="st0">&quot;10000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;fd_SOCK</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;fd</span> <span class="re0">max_tries</span>=<span class="st0">&quot;5&quot;</span> <span class="re0">shun</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">timeout</span>=<span class="st0">&quot;10000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;verify_SUSPECT</span> <span class="re0">timeout</span>=<span class="st0">&quot;1500&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;pbcast.NAKACK</span> <span class="re0">discard_delivered_msgs</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">gc_lag</span>=<span class="st0">&quot;0&quot;</span> <span class="re0">retransmit_timeout</span>=<span class="st0">&quot;300,600,1200,2400,4800&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">use_mcast_xmit</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;unicast</span> <span class="re0">timeout</span>=<span class="st0">&quot;300,600,1200,2400,3600&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"><span class="re1">&lt;pbcast.STABLE</span> <span class="re0">desired_avg_gossip</span>=<span class="st0">&quot;50000&quot;</span> <span class="re0">max_bytes</span>=<span class="st0">&quot;400000&quot;</span> <span class="re0">stability_delay</span>=<span class="st0">&quot;1000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;pbcast.GMS</span> <span class="re0">join_timeout</span>=<span class="st0">&quot;5000&quot;</span> <span class="re0">print_local_addr</span>=<span class="st0">&quot;true&quot;</span> <span class="re0">shun</span>=<span class="st0">&quot;false&quot;</span> <span class="re0">view_ack_collection_timeout</span>=<span class="st0">&quot;5000&quot;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">view_bundling</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;fc</span> <span class="re0">max_credits</span>=<span class="st0">&quot;500000&quot;</span> <span class="re0">min_threshold</span>=<span class="st0">&quot;0.2&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;frag2</span> <span class="re0">frag_size</span>=<span class="st0">&quot;60000&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1"><span class="sc3"><span class="re1">&lt;pbcast.STREAMING_STATE_TRANSFER</span><span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"><span class="re1">&lt;pbcast.FLUSH</span> <span class="re0">timeout</span>=<span class="st0">&quot;0&quot;</span><span class="re2">/&gt;</span></span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/jgroupsConfig<span class="re2">&gt;</span></span></span></div></li>
<li class="li2"><div class="de2">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;async</span> <span class="re2">/&gt;</span></span></div></li>
<li class="li2"><div class="de2">&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!-- Alternatively, to use sync replication, comment out the element above and uncomment the element below. &nbsp;--&gt;</span></div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!-- &lt;sync /&gt; --&gt;</span></div></li>
<li class="li2"><div class="de2">&nbsp;</div></li>
<li class="li1"><div class="de1">&nbsp; &nbsp;<span class="sc3"><span class="re1">&lt;/clustering<span class="re2">&gt;</span></span></span></div></li>
<li class="li2"><div class="de2"><span class="sc3"><span class="re1">&lt;/jbosscache<span class="re2">&gt;</span></span></span></div></li>
</ol></div></div>
<div class="bwp-syntax-source"><pre class="no-parse">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;jbosscache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:jboss:jbosscache-core:config:3.2"&gt;

   &lt;!-- Configure the TransactionManager --&gt;
&lt;transaction transactionManagerLookupClass="org.jboss.cache.transaction.GenericTransactionManagerLookup"/&gt;

   &lt;clustering mode="replication"&gt;
      &lt;!--
       timeout: The max amount of time (in milliseconds) we wait until the
             state (i.e. the contents of the cache) are retrieved from
             existing members in a clustered environment
      --&gt;
      &lt;stateRetrieval timeout="20000"/&gt;

      &lt;!-- JGroups protocol stack properties. --&gt;
      &lt;jgroupsConfig&gt;
         &lt;udp discard_incompatible_packets="true" enable_bundling="true" enable_diagnostics="false" ip_ttl="2"
              loopback="false" max_bundle_size="64000" max_bundle_timeout="30" mcast_addr="224.12.12.12"
              mcast_port="45588" mcast_recv_buf_size="100000000" mcast_send_buf_size="640000"
              oob_thread_pool.enabled="true" oob_thread_pool.keep_alive_time="10000" oob_thread_pool.max_threads="20"
              oob_thread_pool.min_threads="8" oob_thread_pool.queue_enabled="false" oob_thread_pool.queue_max_size="10"
              oob_thread_pool.rejection_policy="Run" thread_naming_pattern="pl" thread_pool.enabled="true"
              thread_pool.keep_alive_time="10000" thread_pool.max_threads="15" thread_pool.min_threads="8"
              thread_pool.queue_enabled="true" thread_pool.queue_max_size="100000"
              thread_pool.rejection_policy="Discard"
              tos="8" ucast_recv_buf_size="20000000" ucast_send_buf_size="640000" use_concurrent_stack="true"
              use_incoming_packet_handler="true"/&gt;
&lt;ping num_initial_members="3" timeout="2000"/&gt;
         &lt;merge2 max_interval="30000" min_interval="10000"/&gt;
         &lt;fd_SOCK/&gt;
         &lt;fd max_tries="5" shun="true" timeout="10000"/&gt;
         &lt;verify_SUSPECT timeout="1500"/&gt;
&lt;pbcast.NAKACK discard_delivered_msgs="true" gc_lag="0" retransmit_timeout="300,600,1200,2400,4800"
                        use_mcast_xmit="true"/&gt;
         &lt;unicast timeout="300,600,1200,2400,3600"/&gt;
&lt;pbcast.STABLE desired_avg_gossip="50000" max_bytes="400000" stability_delay="1000"/&gt;
&lt;pbcast.GMS join_timeout="5000" print_local_addr="true" shun="false" view_ack_collection_timeout="5000"
                     view_bundling="true"/&gt;
         &lt;fc max_credits="500000" min_threshold="0.2"/&gt;
         &lt;frag2 frag_size="60000"/&gt;
&lt;pbcast.STREAMING_STATE_TRANSFER/&gt;
&lt;pbcast.FLUSH timeout="0"/&gt;
      &lt;/jgroupsConfig&gt;

      &lt;async /&gt;
      &lt;!-- Alternatively, to use sync replication, comment out the element above and uncomment the element below.  --&gt;
      &lt;!-- &lt;sync /&gt; --&gt;

   &lt;/clustering&gt;
&lt;/jbosscache&gt;</pre></div></div>

<p>需要注意的是第7行，集群mode。Treecache的分布式同步有两种模式：replication和invalidation。这两者的区别是：对于replication，每一个节点（缓存）中的对象改变时，该节点会将新对象通过多播的方式告知多播组中的其它节点，其它节点自行更新；而对于invalidation模式，发生改变的节点只通知其它节点某对象已发生改变，其它节点直接将自己缓存中的该对象标记为invalid，待下次需要使用时通过数据库更新该对象。这样一解释就能发现取舍的策略了：若网络比数据库金贵，选择invalidation，标记自己的对象，让数据库忙活去吧；若数据库金贵，使用replication多播对象</p>
<p>另外，由于Treecache使用了基于多播协议的jgroup库作为底层通信框架，在配置Treecache集群时不需要了解多播组中其他节点IP，只需要把第19行的多播地址（必须是D类地址）配成一个的就行了，jgroup会自行发现多播组中的其它节点</p>
<h3>世界杯大酬宾：如何使用JMX监控Treecache的状态</h3>
<p>启动容器；打开任意JMX客户端，如jconsole；在进程列表中选择容器的java进程并连接；选择“mbean”标签，找到“jboss.cache”节点，如下图</p>
<p><a href="http://www.jayxu.com/log/wp-content/uploads/2010/06/VisualVM-1.3.png"><img src="http://www.jayxu.com/log/wp-content/uploads/2010/06/VisualVM-1.3-400x235.png" alt="" title="VisualVM 1.3" width="400" height="235" /></a><br />
在这个节点下可以看到所有Treecache通过JMX暴露的属性，包括命中率、缓存对象等
<div style="margin-top: 15px; font-style: italic">
<p><strong>原创内容，转载请注明：</strong> 转载自<a href="http://www.jayxu.com/">拈花微笑</a></p>
<p><strong>本文链接地址:</strong> <a href="http://www.jayxu.com/2010/06/09/2342/">Spring之Hibernate+JBoss Treecache实现Hibernate集群</a></p>
</div>
<p><a href="http://www.bshare.cn/share?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F&title=Spring%E4%B9%8BHibernate%2BJBoss+Treecache%E5%AE%9E%E7%8E%B0Hibernate%E9%9B%86%E7%BE%A4" title="用bShare分享或收藏本文"><img src="http://static.bshare.cn/frame/images/button_custom1-zh.gif" alt="用bShare分享或收藏本文" /></a></p><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F14%2F13131%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">使用Spring LDAP ODM操作LDAP</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F07%2F18%2F2373%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">Spring之经验教训（一）</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2012%2F01%2F12%2F13221%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">解决Velocity＋Spring的中文乱码问题</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2010%2F11%2F24%2F10280%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">谨慎升级至Hibernate 3.6</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.jayxu.com%2F2011%2F12%2F15%2F13164%2F&from=http%3A%2F%2Fwww.jayxu.com%2F2010%2F06%2F09%2F2342%2F">
                        <font size="-1"  style="line-height: 1.65em; font-size: 12px !important;">About save/persist, update/merge in JPA/Hibernate</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.jayxu.com/2010/06/09/2342/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

