欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate 可编程的配置方式 数据库连接经典 

程序员文章站 2024-03-14 08:18:28
...

 

org.hibernate.cfg.Configuration 实例代表了一个应用程序中 Java 类型到SQL数据库映射的完整集合。org.hibernate.cfg.Configuration 被用来构建一个(不可变的(immutable))org.hibernate.SessionFactory。映射定义则由不同的 XML 映射定义文件编译而来。

你可以直接实例化 org.hibernate.cfg.Configuration 来获取一个实例,并为它指定 XML 映射定义文件。如果映射定义文件在类路径(classpath)中,请使用 addResource()。例如:

 

<a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addResource</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"Item.hbm.xml"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addResource</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"Bid.hbm.xml"</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a>  
一个替代方法(有时是更好的选择)是,指定被映射的类,让 Hibernate 帮你寻找映射定义文件:

[JAVA] view plaincopy
<a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Item</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Bid</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a>  
Hibernate 将会在类路径(classpath)中寻找名字为 /org/hibernate/auction/Item.hbm.xml 和/org/hibernate/auction/Bid.hbm.xml 映射定义文件。这种方式消除了任何对文件名的硬编码(hardcoded)。

org.hibernate.cfg.Configuration > 也允许你指定配置属性。例如:

[JAVA] view plaincopy
<a id="configuration-programmatic" style="text-decoration:none"><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_plain" style="color:rgb(0,0,0)"> cfg </span><span class="java_operator" style="color:rgb(0,0,0)">=</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">new</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_type" style="color:rgb(0,0,0)">Configuration</span><span class="java_separator" style="color:rgb(0,0,0)">()</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Item</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">addClass</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_plain" style="color:rgb(0,0,0)">org</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">hibernate</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">auction</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_type" style="color:rgb(0,0,0)">Bid</span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_keyword" style="font-weight:bold; color:rgb(127,27,85)">class</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.dialect"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"org.hibernate.dialect.MySQLInnoDBDialect"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.connection.datasource"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"java:comp/env/jdbc/test"</span><span class="java_separator" style="color:rgb(0,0,0)">)</span>  
<span class="java_plain" style="color:rgb(0,0,0)">    </span><span class="java_separator" style="color:rgb(0,0,0)">.</span><span class="java_plain" style="color:rgb(0,0,0)">setProperty</span><span class="java_separator" style="color:rgb(0,0,0)">(</span><span class="java_literal" style="color:rgb(42,0,255)">"hibernate.order_updates"</span><span class="java_separator" style="color:rgb(0,0,0)">,</span><span class="java_plain" style="color:rgb(0,0,0)"> </span><span class="java_literal" style="color:rgb(42,0,255)">"true"</span><span class="java_separator" style="color:rgb(0,0,0)">);</span></a>  

 当然这不是唯一的传递 Hibernate 配置属性的方式,其他可选方式还包括: