当前位置:首页 > 开发 > 编程语言 > 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

    震惊

    震惊

编辑推荐
说是使用详解,其实是我自己做了一些简单的试验.如有说错,请指正. Annotation方式的MappedBy其实就是
来源: http://yangfei520.blog.51cto.com/1041581/269302 以前使用Hibernate都是用最愚蠢的方式:
1.下载hibernate的jar包 http://sourceforge.net/projects/hibernate/files/hibernate4/ 2.加载hibe
Hibernate为操作数据库提供了一系列的API,为了能够准确的使用这些API,要在开发环境里要做很多的环
建立web project工程ShoppingHibernate,工程文件结构图如下所示: 1,将Hibernate的JAR包和MySQL数
大家都知道hibernate有两种方式进行配置实体类配置,第一类为在类似于entity.hbm.xmL中配置对应实体
大家都知道hibernate有两种方式进行配置实体类配置,第一类为在类似于entity.hbm.xmL中配置对应实体
大家都知道hibernate有两种方式进行配置实体类配置,第一类为在类似于entity.hbm.xmL中配置对应实体
大家都知道hibernate有两种方式进行配置实体类配置,第一类为在类似于entity.hbm.xmL中配置对应实体
大家都知道hibernate有两种方式进行配置实体类配置,第一类为在类似于entity.hbm.xmL中配置对应实体
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号