---
title: "hibernate"
author: "fgh2011"
date: "2011-12-25T23:47:00Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2012-04-28T13:05:59Z"
id: 1496713
url: "https://fgh2011.iteye.com/blog/1496713"
---

2、对于Query接口的list()方法与iterator()方法来说，都可以实现获取查询的对象，、  
但是list()方法返回的每个对象都是完整的(对象中的每个属性都被表中的字段填充上了)，  
而iterator()方法所返回的对象中仅包含了主键值(标识符),只有当你对iterator()中  
的对象进行操作时，hibernate才会向数据库再次发送SQL语句来获取该对象的属性值  
list()方法性能优

——&gt;  
映射关系：  
——&gt;  
一对多双向关联：

在进行一对多双向关联的时候，数据库中只需要建好表结构就可以了，不用指定外键  
数据库中表的字段类型，字段长度一定要和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  
在建表的时候不要创建外键

——&gt;  
get()直接去数据库中把这个对象本身，对象是什么，属性、字段全查出来  
load()首先查出来的是个代理对象，如果在session关闭之前使用这个对象的时候、  
这时候才会对数据库发出一条sql语句，否则的话这个对象中装的仅仅是这个对象中的数据库id，  
当session打开之后，调用load()方法，就会把这个对象加载到内存之中，  
在load的时候，先从缓存中查，存在的话就不会给数据库发出sql语句，缓存中没有在到数据库中查

——&gt;  
延迟加载:  
当程序中获取到了一的一方，但是不需要多的一方，可以使用延迟加载  
在.hbm.xml配置文件中该属性上加上 lazy="false" 属性

——&gt;  
Session级别的缓存又叫做一级缓存，一级缓存不能更改，不能禁用，线程的缓存  
SessionFactory级别的缓存叫做二级缓存 可以修改，进程的缓存