2、对于Query接口的list()方法与iterator()方法来说,都可以实现获取查询的对象,、
但是list()方法返回的每个对象都是完整的(对象中的每个属性都被表中的字段填充上了),
而iterator()方法所返回的对象中仅包含了主键值(标识符),只有当你对iterator()中
的对象进行操作时,hibernate才会向数据库再次发送SQL语句来获取该对象的属性值
list()方法性能优
——>
映射关系:
——>
一对多双向关联:
在进行一对多双向关联的时候,数据库中只需要建好表结构就可以了,不用指定外键
数据库中表的字段类型,字段长度一定要和java类、配置文件里面的类型相对应
数据库的编码最好设置为utf-8,避免存中文时造成的错误
一对一映射:
1).主键关联
一对一默认使用的是立即加载,如果需要使用延迟加载,需要在one-to-one中将constrained属性设置为true
并且将待加载的一方的class元素中的lazy属性设置为true(该属性默认值为true)
一對一加載時默認使用左外連接,可以通过修改fetch属性为select修改成每次发送一条select语句的形式
2).外键关联
本质上是一对多的蜕化形式,在many-to-one元素中增加unique="true"属性就变成一对一
报错信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into idcard (number, student_id, id) values (?, ?, ?)
Hibernate: insert into student (name, id) values (?, ?)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.fgh.hibernate.InsertTest.main(InsertTest.java:39)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`hibernate`.`student`, CONSTRAINT `FK8FFE823BC2D322B6` FOREIGN KEY (`id`) REFERENCES `idcard` (`id`))
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more
在建表的时候不要创建外键
——>
get()直接去数据库中把这个对象本身,对象是什么,属性、字段全查出来
load()首先查出来的是个代理对象,如果在session关闭之前使用这个对象的时候、
这时候才会对数据库发出一条sql语句,否则的话这个对象中装的仅仅是这个对象中的数据库id,
当session打开之后,调用load()方法,就会把这个对象加载到内存之中,
在load的时候,先从缓存中查,存在的话就不会给数据库发出sql语句,缓存中没有在到数据库中查
——>
延迟加载:
当程序中获取到了一的一方,但是不需要多的一方,可以使用延迟加载
在.hbm.xml配置文件中该属性上加上 lazy="false" 属性
——>
Session级别的缓存又叫做一级缓存,一级缓存不能更改,不能禁用,线程的缓存
SessionFactory级别的缓存叫做二级缓存 可以修改,进程的缓存
分享到:
相关推荐
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
Hibernate连接SQLite配置步骤: 1、添加SQLite库: 1.1、将两个jar包:com.zy.hibernate.dialect.SQLiteDialect.jar、sqlite-jdbc-3.7.2.jar拷贝到“\WEB-INF\lib”文件夹下; 2、配置hibernate: 2.1、将...
Manning出版 Java Persistence With Hibernate 第二版 清晰pdf Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java ...
hibernate
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
hibernate 资料hibernate 资料hibernate 资料hibernate 资料
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate...
Hibernate连表查询 Hibernate连表查询
Java Persistence with Hibernate is the new bible of Hibernate. As a major revision of the popular Hibernate in Action, it builds on the same single example application to introduce and explain the ...
hibernate
Hibernate稳定版(hibernate-release-5.3.23.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
hibernate5 hibernate PDF 讲义 动力 hibernate ssh hibernate5 节点
hibernate annotation hibernate3
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
Hibernate对象的生命周期 Hibernate对象的生命周期 Hibernate对象的生命周期
hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3