当前位置:首页 > 开发 > 编程语言 > Hibernate > 正文

Hibernate 可编程的配置方式

发表于: 2012-04-20   作者:aijuans9   来源:转载   浏览:
摘要:   org.hibernate.cfg.Configuration 实例代表了一个应用程序中 Java 类型到SQL数据库映射的完整集合。org.hibernate.cfg.Configuration 被用来构建一个(不可变的(immutable))org.hibernate.SessionFactory。映射定义则由不同的 XML 映射定义文件编译而来。 你可以

 

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 配置属性的方式,其他可选方式还包括:

Hibernate 可编程的配置方式

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号