Hibernate5学习笔记

1

hibernate自增MySQLIntegrityConstraintViolationException:

换一种保存方式:
http://tanglei528.blog.163.com/blog/static/43353399201071313110854/

2 hibernate自动创表

<!-- validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构 -->
<!-- 指定hibernate启动时自动创建表结构 注意:使用create会在第一次编译jsp时重新新建表,如果存在会被删除重建-->
<property name="hbm2ddl.auto">validate</property>

3

spring整合hibernate继承HibernateDaoSupport报错:‘sessionFactory’ or ‘hibernateTemplate’ is required

hibernate5以前可使用:

<!-- 下面的方式hibernate5没效
<bean id="catServiceImpl" class="cn.yunlingfly.xxx.service.impl.CatSHServiceImpl">
	<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->

hibernate5可在DAO层加入:

@Autowired
public void setSessionFacotry(SessionFactory sessionFacotry) {
    super.setSessionFactory(sessionFacotry);
}

4 指定MySQL高本版和hibernate方言:

<!-- 指定使用MySQL数据库格式的sql,指定高版本的MySQL:MySQL5Dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

5 Hibernate5 配置实体类mapping失效:

<!-- 下面的配置在hibernate4及以上失效,要将hbm和实体类放一起 -->
<!-- 指定Cat类为hibernate实体类 -->
<!-- <mapping class="cn.yunlingfly.xxx.bean.Cat"/> -->
<!-- 下面是使用xml文件配置实体类映射 -->
<!-- <mapping resource="Cat.hbm.xml"/> -->

换成在HibernateUtil里配置:

		Configuration config = new Configuration()
												.configure("hibernate.cfg.xml")
												.addClass(Cat.class);
												//加载配置文件
//			动态加载实体类配置文件
//			Configuration config = new Configuration()
//													.addResource("Cat.hbm.xml")
//													.addResource("Student.hbm.xml")
//													.addClass("Cat.class");
//													.addClass("Student.class");

6 hibernate实体类 注解与hbm

@配置中,如果某属性没有配置,则默认该属性名与数据表列名相同;而xml配置中,如果某属性没有配置,则认为该属性没有对应的数据库列,不参与持久化。二者是截然不同的。

7 与Spring的结合

<bean id="dataSource"
	class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
	<property name="driverClassName"
		value="com.mysql.jdbc.Driver" />
	<property name="url"
		value="jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxxxx?useSSL=false" />
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
</bean>

<bean id="sessionFactory"
	class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
	destroy-method="destroy">
	<property name="dataSource" ref="dataSource" />
	<!-- 该package下的所有类都会被当成实体类加载 -->
	<property name="annotatedPackages" value="cn.yunlingfly.xxx.bean" />
	<!-- 使用@注解配置实体类 -->
	<property name="annotatedClasses">
		<list>
			<value>cn.yunlingfly.xxx.bean.Cat</value>
			<value>cn.yunlingfly.exam3.bean.Person</value>
			<value>cn.yunlingfly.exam3.bean.Email</value>
		</list>
	</property>
	<!-- 使用hbm来配置hibernate实体类 -->
	<property name="mappingLocations">
    	<value>classpath:/cn/yunlingfly/exam3/bean/*.hbm.xml </value>
	</property>
	<!-- hibernate属性 -->
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
			<prop key="hibernate.show_sql">true</prop>
			<prop key="hibernate.format_sql">true</prop>
			<prop key="hibernate.hbm2ddl.auto">validate</prop>
		</props>
	</property>
</bean>

8 org.hibernate.Query “UnusedDeclaration”

在hibernate5之后,这个包下的Query整个类都被废弃了,根据代码提示Deprecated. (since 5.2) use org.hibernate.query.Query instead,建议我们使用org.hibernate.query.Query这个类,使用方法例如:

List<TestEntity> testList=session.createQuery(hql,TestEntity.class).getResultList();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值