您的位置:首页 > 职场人生

码农小汪-Hibernate学习3-Architecture-框架简述

2016-03-27 23:11 288 查看
了解框架的整体视图还是很有必要啊



The “minimal” architecture has the application manage its own JDBC connections and provide those connections to Hibernate; additionally the application manages transactions for itself. This approach uses a minimal subset of Hibernate APIs.



The “comprehensive” architecture abstracts the application away from the underlying JDBC/JTA APIs and allows Hibernate to manage the details.



SessionFactory(org.hibernate.SessionFactory)


一个线程安全的,不可变的缓存编译映射的一个数据库。一个工厂org.hibernate.Session实例,一个客户端的org.hibernate.connection.ConnectionProvider。(可选) 维护一个second level cache之间的数据是可重用的 交易过程或集群级别。

Session (org.hibernate.Session)


一个单线程的,短暂的对象代表之间的对话 应用程序和持久性存储。 包装JDBCjava.sql.Connection,和 Factory for org.hibernate.Transaction.维护一个first level cache持续的应用程序的持久性对象 和集合;这个缓存时使用导航对象图或查找 对象标识符。

Persistent objects and collections


Short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one org.hibernate.Session. Once the org.hibernate.Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation). 英文更好

Transient and detached objects and collections
瞬态和分离对象和集合


Instances of persistent classes that are not currently associated with a org.hibernate.Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed org.hibernate.Session

Transaction (org.hibernate.Transaction)事务不是事物!!


(可选)单线程,短暂的对象所使用的应用程序 指定原子工作单元。 摘要从底层JDBC应用程序, JTA事务或CORBA。 一个org.hibernate.Session也许跨越了多个org.hibernate.Transaction在某些情况下。 然而, 事务界定,或使用底层APIorg.hibernate.Transaction,从来都不是可选的。

事务是很重要的东西哦,非常重要。很多的操作失败啦,我们都需要解决这些问题的。

ConnectionProvider(org.hibernate.connection.ConnectionProvider) 这个很好理解的


(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying javax.sql.DataSource or java.sql.DriverManager. It is not exposed to application, but it can be extended and/or implemented by the developer.

TransactionFactory(org.hibernate.TransactionFactory)


(可选)的工厂org.hibernate.Transaction实例。 这不是暴露给应用程序,但它可以扩展和/或 由开发人员执行。书上应该讲解的有的,我看过李刚的那个书,还不如看文档。都差不多。

配置和使用,简单的聊聊!其实上文中我们已经是用过啦!

You can obtain a org.hibernate.cfg.Configuration instance by instantiating it directly and specifying XML mapping documents. If the mapping files are in the classpath, use addResource(). For example:

Configuration cfg = new Configuration()
.addResource("Item.hbm.xml")
.addResource("Bid.hbm.xml");
An alternative(二者之一) way is to specify the mapped class and allow Hibernate to find the mapping document for you:
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);

A org.hibernate.cfg.Configuration also allows you to specify configuration properties. For example:
其实我们这,没必要采用编程的方式注入属性
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");


Obtaining a SessionFactory

SessionFactory sessions = cfg.buildSessionFactory();


如果你是在理解不来工厂模式,个人建议去看看设计模式,还是不错的。小汪也要去看看的,我之前看过一次啦,时间长了会忘的,督促自己去写一写吧。

JDBC connections

其实就是个session!

Session session = sessions.openSession(); // open a new Session


Once you start a task that requires access to the database, a JDBC connection will be obtained from the pool. 将从连接池中获取数据



Hibernate自己的连接池的弄的不好,还是用别人的c3p0 文档里面很多etc

hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect


数据源JNDI http://baike.baidu.com/link?url=AkUL3B2onL9oB6Aa2q-1YGBtH7Yb5EkhVNwDdt9vUYjOdLJaRnh7KMlFzNxuyX2v0uViwzF5vi99HjzbF7D-6_

Property name Purpose

hibernate.connection.datasource   datasource JNDI name

hibernate.jndi.url     URL of the JNDI provider (optional)

hibernate.jndi.class   class of the JNDI InitialContextFactory (optional)

hibernate.connection.username  database user (optional)

hibernate.connection.password  database user password (optional)

hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect


还有一些其他的属性,比如显示SQL啊,很多的都在文档中显示的,自己查查就好了Optional configuration properties

属性名目的
hibernate.show_sql所有SQL语句写入控制台。 这是一个替代 设置日志类别org.hibernate.SQL来debug。 true
hibernate.format_sql漂亮的打印日志中的SQL和控制台。
还有好多…..

一口气把配置写完塞

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">

<!-- properties -->
<property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>

<!-- mapping files -->
<mapping resource="org/hibernate/auction/Item.hbm.xml"/>
<mapping resource="org/hibernate/auction/Bid.hbm.xml"/>

<!-- cache settings -->
<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>

</session-factory>

</hibernate-configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: