[TOC]
更新
hibernate使用update更新时一般情况下是对所有字段进行更新,如果只更新个别字段,则需要用id或者其他字段将对象先取出来再进行个别字段的更新。
对应字段加上如下注释时,则不进行更新
@Column(updatable=false)
若使用XML配置文件,可以配置dynamic-update,就可以动态更新,但在Annotation中没有此类方法
<class name=”com.sccin.entity.Student” table=”student” dynamic-update=”true”>
字段大小写问题
由于column中name若有大写(如goodNum),到数据库中都会被译成>
good_num,而相对数据库中来说字段不分大小写的,所以若数据库创建表
时,若字段名中有大写,些时在column的name属性中应全部改为小写.
反向工程创建问题
在使用myeclipse的hibernate的反向工程创建时出现点击没有反应的情况,解决方法是是找到工程目录下的.myhibernatedata文件,然后找到reSettingsFile=…那行,删除这一行即可,或者换一个工作空间建立web工程,加入hibernate,即可。
反向工程后若出现XXXId的类,则说明数据表没有主键
hibernate update
hibernate的主键也需要自己管理。特别是当用update的时候,主键为自增,若不进行管理,不对其赋所修改的值时会报如下错误。
1 | org.hibernate.TransientObjectException: The given object has a null identifier: |
出现如下错误,设置主键自动生成
1 | org.hibernate.id.IdentifierGenerationException:ids for this class must be manually assigned before calling save(): |
1 | @Id |
配置文件
注解方式( hibernate.cfg.xml文件中)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/lbsimage
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">cc</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!--如果使用JUnit进行测试:就要加上下面这句话-->
<property name="javax.persistence.validation.mode">none</property>
<!-- 实体配置类 注解方式-->
<mapping class="com.ssh.model.Student" />
<!--或者配置文件方式 如下 -->
<mapping resource="Teacher.hbm.xml" />
</session-factory>
</hibernate-configuration>或者使用配置文件方式(Teacher.hbm.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Teacher" table="teacher" catalog="learnssh">
<id name="tid" type="java.lang.Integer">
<column name="tid" />
<generator class="native" />
</id>
<property name="tname" type="java.lang.String">
<column name="tname" />
</property>
<property name="tage" type="java.lang.Integer">
<column name="tage" />
</property>
</class>
</hibernate-mapping>与Spring结合时,这两种方法的applicationContext.xml文件的为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 <?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</bean>
</beans>Spring+hibernate结合方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 <!-- 用注解进行依赖注入 -->
<context:annotation-config></context:annotation-config>
<!-- 用注解让spring管理bean -->
<context:component-scan base-package="com.lbs" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.1.106:3306/lbsimage" />
<property name="user" value="root" />
<property name="password" value="123456" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="5" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="20" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="10" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="20" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="30" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="10" />
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。 但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<property name="breakAfterAcquireFailure" value="true" />
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。 建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false -->
<property name="testConnectionOnCheckout" value="true" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>com.lbs.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>