<?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>-Flyぁ梦- &#187; Hibernate</title>
	<atom:link href="http://blog.11034.org/tag/hibernate/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>SpringMVC+Hibernate的一些配置</title>
		<link>http://blog.11034.org/2011-03/spring_hibernate_annotation.html</link>
		<comments>http://blog.11034.org/2011-03/spring_hibernate_annotation.html#comments</comments>
		<pubDate>Tue, 15 Mar 2011 15:53:54 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=815</guid>
		<description><![CDATA[近期项目又用到了Spring MVC+Hibernate的框架结构和全Annotation（注解）的配置，自然 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>近期项目又用到了Spring MVC+Hibernate的框架结构和全Annotation（注解）的配置，自然是再熟悉不过的了，从Trilogy的达西那儿实习开始，到学院的J2EE、软件工程的大程，一路相同的架构配置，轻车熟路，不过这次可谓变化不小，配置项因此有不小的改变，也学到一些东西。</p>
<p><span id="more-815"></span></p>
<h3>Spring MVC的XML</h3>
<p>Spring MVC的&lt;servlet-name&gt;-servlet.xml（以下用spring-servlet.xml表示）和普通Spring结构的applicationContext.xml，两者有比较微妙的关系。</p>
<p>Spring MVC项目中，web.xml的servle项按如下配置好，Spring会自动加载applicationContext.xml之后再加载spring-servlet.xml（推测原因，因为后者能引用前者的bean，反之则不行），其中关于Controller的<context:component-scan/>项必须放在spring-servlet.xml中，而其他比如datasource、dao、service等bean则建议放在applicationContext.xml中。</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>spring<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">	这里如果填了下面的项，会导致仅加载applicationContext.xml而不加载spring-servlet.xml</span>
<span style="color: #808080; font-style: italic;">	这样就只能在applicationContext.xml里手动去加载其他xml，&lt;import resource=&quot;spring-servlet.xml&quot;/&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;init-param&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span>
<span style="color: #808080; font-style: italic;">		&lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;/init-param&gt;</span>
<span style="color: #808080; font-style: italic;">	--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Spring MVC的模板层配置</h3>
<p>先说Spring MVC的模板层，一直以来习惯使用Freemarker，这就需要配置Freemarker的bean和Spring连接Freemarker的ViewResolver，FreeMarkerViewResolver。一般方法就是在spring-servlet.xml中，将这几个bean的信息注入。如下所示：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fmXmlEscape&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;freemarker.template.utility.XmlEscape&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;freemarkerConfig&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;templateLoaderPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;freemarkerSettings&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;datetime_format&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>yyyy-MM-dd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;number_format&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>0.######<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;freemarkerVariables&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml_escape&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;fmXmlEscape&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultEncoding&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;viewResolver&quot;</span> </span>
<span style="color: #009900;">	<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.ftl&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;exposeSpringMacroHelpers&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contentType&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>这次项目比较正规，我负责后端Java开发，前端页面另有人负责，而且前端程序员希望用JSP页面来完成，于是正好换一个新的模板引擎试试。其实自己一直很想用JSP的，这样又可以省去Freemarker这一第三方类库，少一点总是好的。换成JSP，当然就要换成JSP的ViewResolver，叫InternalResourceViewResolver，而JSP因为是J2EE集成的模板，所以不需要像Freemarker还要配置，的确是方便了！bean如下：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;prefix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/views/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;suffix&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Hibernate Annotation的外键配置</h3>
<p>前几个项目中虽然一直都有用@ManyToOne、@OneToOne和@JoinColumn这种Annotation来配置对应关系，但一直都是代码逻辑来控制的，并没有将外键实施于数据库中。说了这次项目比较正规嘛，要求数据库中也是有外键约束的，于是又要重新动动脑筋。说来也奇怪的，搞了好久的Annotation，也百度了好久才找到：<strong>MySQL中MyISAM类型的数据表是不支持外键的，InnoDB类型才支持！</strong>说来关于MySQL的数据表类型是MyISAM还是InnoDB曾经也遇到过类似问题，不过是不是外键就不记得了。</p>
<p>找到了问题所在，剩下的就是如何将Hibernate的自动建表机制（hibernate.hbm2ddl.auto）将数据表建为InnoDB了。居然没想到百度还搜不到，最后在Google的英文页面中找到了配置方法：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">hibernate.<span style="color: #006633;">dialect</span> <span style="color: #339933;">=</span> org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">dialect</span>.<span style="color: #006633;">MySQLDialect</span>	<span style="color: #339933;">=&gt;</span>	MyISAM表
hibernate.<span style="color: #006633;">dialect</span> <span style="color: #339933;">=</span> org.<span style="color: #006633;">hibernate</span>.<span style="color: #006633;">dialect</span>.<span style="color: #006633;">MySQLInnoDBDialect</span>	<span style="color: #339933;">=&gt;</span>	InnoDB表</pre></td></tr></table></div>

<h3>深爱的Spring</h3>
<p>说起著名的SSH框架，Spring是我第二个学的，初学的时候感觉非常难，《Pro Spring》中的反转控制和依赖注入实在是太抽象的概念。幸运的是，通过实习项目中的coding，立马就深深地理解了Spring的各种编程理念和思维方式。从而也接触了Spring MVC，接触了全Annotation的配置，立马就让我放弃了Structs。至于Hibernate，其实也不是很喜欢，又复杂又臃肿，不如JDBC来的简洁方便！所以，当“SSH你最喜欢哪个框架”这种问题摆在我面前，毫无疑问的！难道不觉得Spring的名字也是最美的么？ <img src="http://blog.11034.org/wp-includes/images/smilies/icon_mrgreen.gif" alt=":mrgreen:" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h4  class="related_post_title">看看 Hibernate , MySQL , Spring</h4><ul class="related_post"><li>2016-03-22 -- <a target="_blank" href="http://blog.11034.org/2016-03/spring_mvc_applicationcontext.html" title="Spring-MVC中的一些问题">Spring-MVC中的一些问题</a></li><li>2015-08-12 -- <a target="_blank" href="http://blog.11034.org/2015-08/rails_nginx.html" title="Liunx下搭建Rails和Nginx环境">Liunx下搭建Rails和Nginx环境</a></li><li>2013-08-10 -- <a target="_blank" href="http://blog.11034.org/2013-08/some_skills_in_java.html" title="学到的一些东西">学到的一些东西</a></li><li>2013-06-23 -- <a target="_blank" href="http://blog.11034.org/2013-06/shida.html" title="院版十大了，那就XYT一下~">院版十大了，那就XYT一下~</a></li><li>2012-05-13 -- <a target="_blank" href="http://blog.11034.org/2012-05/java_play_framework.html" title="Java Play framework">Java Play framework</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2011-03/spring_hibernate_annotation.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
