Hibernate入门

本文详细介绍了如何使用Hibernate进行实体映射设置,包括id属性、日期和标题字段的映射规则,以及如何配置Hibernate的连接池和SessionFactory。重点讨论了如何避免使用默认映射名称、日期类型映射到SQL数据库类型以及自动类型检测可能带来的问题。

entity.hbm.xml
<hibernate-mapping
 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"/>

</class>


</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


1.Hibernate comes with support for two third-party open source JDBC connection pools:

c3p0 andproxool. However, we will be using the Hibernate built-in connection pool for this tutorial

2.For Hibernate's configuration, we can use a simplehibernate.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.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值