hibernate3学习笔记 HibernatethreadXML工作Cache
程序员文章站
2024-02-20 18:26:58
...
1、hibernate3配置文件中
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
说明如下:
The hibernate.current_session_context_class configuration parameter defines which
org.hibernate.context.CurrentSessionContext implementation should be used. Note that for backwards
compatibility, if this config param is not set but a org.hibernate.transaction.TransactionManagerLookup
is configured, Hibernate will use the org.hibernate.context.JTASessionContext. Typically, the value of this
parameter would just name the implementation class to use; for the two out-of-the-box implementations,
however, there are two corresponding short names, "jta" and "thread".
此设置的作用如下:
What does sessionFactory.getCurrentSession() do? First, you can call it
as many times and anywhere you
like, once you get hold of your SessionFactory (easy thanks to
HibernateUtil). The getCurrentSession()
method always returns the "current" unit of work. Remember that we
switched the configuration option for this
mechanism to "thread" in hibernate.cfg.xml? Hence, the scope of the
current unit of work is the current Java
thread that executes our application. However, this is not the full
truth. A Session begins when it is first
needed, when the first call to getCurrentSession() is made. It is then
bound by Hibernate to the current
thread. When the transaction ends, either committed or rolled back,
Hibernate also unbinds the Session from
the thread and closes it for you. If you call getCurrentSession() again,
you get a new Session and can start a
new unit of work. This thread-bound programming model is the most
popular way of using Hibernate.
sessionFactory.getCurrentSession()可以完成一系列的工作,当调用时,
hibernate将session绑定到当前线程,事务结束后,hibernate
将session从当前线程中释放,并且关闭session。当再次调用getCurrentSession
()时,将得到一个新的session,并重新开始这一系列工作。
这样调用方法如下:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
不需要close session了。
(计划:将此项功能应用到hivetranse中)
2、The rules you have to remember
are straightforward: All bi-directional associations need one side as inverse. In a one-to-many association
it has to be the many-side, in many-to-many association you can pick either side, there is no difference.
你应该记住的规则是简单的:所有的双向关联需要一边的inverse为true。在一对多的关联中,多的一方为true,在多对多的关联中
你可以选择任何一边,没有区别。
3、Hibernate
will use its C3P0ConnectionProvider for connection pooling if you set hibernate.c3p0.* properties.
如果你设定hibernate.c3p0.* 属性,Hibernate将使用
C3P0ConnectionProvider
作为连接池(就是说不需要设定hibernate.cache.provider_class属性,只要有ibernate.c3p0.*的设定就使用c3p0的连接池
)
4、Configuration cfg = new Configuration();将配置hibernate.properties文件
Configuration cfg = new Configuration().configure();将同时配置hibernate.properties和hibernate.cfg.xml,但是后者的属性将覆盖前者
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
说明如下:
The hibernate.current_session_context_class configuration parameter defines which
org.hibernate.context.CurrentSessionContext implementation should be used. Note that for backwards
compatibility, if this config param is not set but a org.hibernate.transaction.TransactionManagerLookup
is configured, Hibernate will use the org.hibernate.context.JTASessionContext. Typically, the value of this
parameter would just name the implementation class to use; for the two out-of-the-box implementations,
however, there are two corresponding short names, "jta" and "thread".
此设置的作用如下:
What does sessionFactory.getCurrentSession() do? First, you can call it
as many times and anywhere you
like, once you get hold of your SessionFactory (easy thanks to
HibernateUtil). The getCurrentSession()
method always returns the "current" unit of work. Remember that we
switched the configuration option for this
mechanism to "thread" in hibernate.cfg.xml? Hence, the scope of the
current unit of work is the current Java
thread that executes our application. However, this is not the full
truth. A Session begins when it is first
needed, when the first call to getCurrentSession() is made. It is then
bound by Hibernate to the current
thread. When the transaction ends, either committed or rolled back,
Hibernate also unbinds the Session from
the thread and closes it for you. If you call getCurrentSession() again,
you get a new Session and can start a
new unit of work. This thread-bound programming model is the most
popular way of using Hibernate.
sessionFactory.getCurrentSession()可以完成一系列的工作,当调用时,
hibernate将session绑定到当前线程,事务结束后,hibernate
将session从当前线程中释放,并且关闭session。当再次调用getCurrentSession
()时,将得到一个新的session,并重新开始这一系列工作。
这样调用方法如下:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
不需要close session了。
(计划:将此项功能应用到hivetranse中)
2、The rules you have to remember
are straightforward: All bi-directional associations need one side as inverse. In a one-to-many association
it has to be the many-side, in many-to-many association you can pick either side, there is no difference.
你应该记住的规则是简单的:所有的双向关联需要一边的inverse为true。在一对多的关联中,多的一方为true,在多对多的关联中
你可以选择任何一边,没有区别。
3、Hibernate
will use its C3P0ConnectionProvider for connection pooling if you set hibernate.c3p0.* properties.
如果你设定hibernate.c3p0.* 属性,Hibernate将使用
C3P0ConnectionProvider
作为连接池(就是说不需要设定hibernate.cache.provider_class属性,只要有ibernate.c3p0.*的设定就使用c3p0的连接池
)
4、Configuration cfg = new Configuration();将配置hibernate.properties文件
Configuration cfg = new Configuration().configure();将同时配置hibernate.properties和hibernate.cfg.xml,但是后者的属性将覆盖前者