entity.hbm.xml
<hibernate-mapping
</class>
package="org.hibernate.tutorial.domain"><class
name="Event" table="EVENTS"><id
name="id" column="EVENT_ID"><generator
class="native"/></id>
<propertyname="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</hibernate-mapping>
1.The
name="id" mapping attribute declares the name of the JavaBean property and tells Hibernate to use the
getId() and
setId() methods to access the property. The column attribute tells Hibernate which column of the
EVENTS table holds the primary key value.
2.Why does the
date property mapping include the
column attribute, but the
title does not? Without the
column attribute, Hibernate by default uses the property name as the column name. This works for
title, however,
date is a reserved keyword in most databases so you will need to map it to a different name.
3.The
title mapping also lacks a
type attribute. The types declared and used in the mapping files are not Java data types; they are not SQL database types either. These types are called
Hibernate mapping types, converters which can translate from Java to SQL data types and vice versa. Again, Hibernate will try to determine the correct conversion and mapping type itself if the
type attribute is not present in the mapping. In some cases this automatic detection using Reflection on the Java class might not have the default you expect or need. This is the case with the
date property. Hibernate cannot know if the property, which is of
java.util.Date, should map to a SQL
date,
timestamp, or
time column. Full date and time information is preserved by mapping the property with a
timestamp converter.
Tip
Hibernate makes this mapping type determination using reflection when the mapping files are processed. This can take time and resources, so if startup performance is important you should consider explicitly defining the type to use
hibernate.cfg.xml
2.For Hibernate's configuration, we can use a simple
hibernate.properties file(key-value), a more sophisticated hibernate.cfg.xml file, or even complete programmatic setup. Most users prefer the XML configuration file
3.You configure Hibernate's
SessionFactory. SessionFactory is a global factory responsible for a particular database. If you have several databases, for easier startup you should use several
<session-factory> configurations in several configuration files.
4.hibernate 配置文件的默认文件名为hibernate.cfg.xml,当程序调用configuration对象的configure()方法时,hibernate将自动加载该文件
5.hibernate 并不推荐采用DriverManager来连接数据库,而是推荐使用数据源来管理数据库链接,这样能保证最好的性能。Hibernate推荐使用C3P0数据源
6.PO的操作必须在Session管理下才能同步到数据库。session由sessionfactory工厂产生,sessionfactory是数据库编译后的内存镜像,通常一个应用对应一个sessionfactory对象。sessionfactory对象由configuration对象产生,configuration对象负责加载hibernate配置文件
7.hibernate的优点
1)不需要使用编写sql语句,而是允许采用oo方式来访问数据库
2)jdbc访问过程中大量的checked异常被包装成hibernate的runtime异常,从而不再要求程序必须
6.PO的操作必须在Session管理下才能同步到数据库。session由sessionfactory工厂产生,sessionfactory是数据库编译后的内存镜像,通常一个应用对应一个sessionfactory对象。sessionfactory对象由configuration对象产生,configuration对象负责加载hibernate配置文件
7.hibernate的优点
1)不需要使用编写sql语句,而是允许采用oo方式来访问数据库
2)jdbc访问过程中大量的checked异常被包装成hibernate的runtime异常,从而不再要求程序必须
本文详细介绍了如何使用Hibernate进行实体映射设置,包括id属性、日期和标题字段的映射规则,以及如何配置Hibernate的连接池和SessionFactory。重点讨论了如何避免使用默认映射名称、日期类型映射到SQL数据库类型以及自动类型检测可能带来的问题。
2115

被折叠的 条评论
为什么被折叠?



