Hibernate笔记

来源:互联网 发布:淘宝网天猫女装秋装 编辑:程序博客网 时间:2024/05/20 16:10

Hibernate中常见问题

No row with the given identifier exists    问题的原因及解决 .

hibernatetablestruts数据库nullquery Hibernate中No row with the

given identifier exists问题的原因及解决
            产生此问题的原因:

             有两张表,table1和table2.产生此问题的原因就是table1里

做了关联<one-to-one>或者<many-to-one unique="true">(特殊的多对一映

射,实际就是一对一)来关联table2.当hibernate查找的时候,table2里的数据

没有与table1相匹配的,这样就会报No row with the given identifier

exists这个错.(一句话,就是数据的问题!)

           假如说,table1里有自身的主键id1,还有table2的主键id2,这两

个字段.

          如果hibenrate设置的单项关联,即使table1中的id2为null

值,table2中id2中有值,查询都不会出错.但是如果table1中的id2字段有值,

但是这个值在table2中主键值里并没有,就会报上面的错!

         如果hibernate是双向关联,那么table1中的id2为null值,但是

table2中如果有值,就会报这个错.这种情况目前的解决办法就是改成单项关

联,或者把不对应的数据改对!

           这就是报这个错的原因了,知道原因了就相应的改就行了.或许还

有些人迷惑hibernate关联都配好了,怎么会出现这样的错?其实这是编程的时

候出现的问题,假如说我在添加信息的时候,页面传过来的struts的formbean

到dao方法中需要封装成hibernate的po(就是hibenrate的bean),要是一个个

po.get(form.set())实在太麻烦了,这样一般都会写个专门的方法来封装,遇

到po.get(form.set())这种情况直接把struts的formbean对象传到此方法中

封装就行了,假如我有个字段是创建人id,那么这个字段是永远不会改的,我在

添加的时候还调用这个方法,这个专门封装的方法是有一些判断的,假如说我判

断一下,如果遇到创建人id传过来为空值,我判断如果是空值,我把创建人id设

为0,但是用户表中userid是主键从1开始自增的,那么这样数据就对应不上了,

一查就会出这个错了.这个错在开发刚开始的时候经常发生,因为每个人的模块

都是由相应的人独立开发完成以后再整合在一起的,每个人写单独那一块的时

候往往会忽略这些,所以整合的时候这些问题往往就都一下子全冒出来了....

整合很辛苦,tnnd!


<2>
org.hibernate.TransientObjectException:  

org.hibernate.TransientObjectException: object references an

unsaved transient instance - save the transient instance before

flushing: com.xxx.yyy.Company

设置级联 <set name="orderInfos" inverse="true" cascade="save-

update,persist" >

或看看 数据库是付支持外键inneDB   本人就是设置了inverse=“true”没

有异常了

若出现... a foreign key ....
则删除数据库的级联,用hibernate来做级联
还有就是该设置的属性值(not null)没有设置

org.hibernate.InvalidMappingException: Unable to read XML
Open quote is expected for attribute "{1}" associated with an  

element type  "name". Nested excep

请检查你的配置文件是否缺少"",或者""是否是中文字符

1org.hibernate.LazyInitializationException: could not initialize

proxy - no Session
延迟加载错误,设置many-to-one lazy="false",在session关闭前将对象写

入内存


org.hibernate.cache.NoCacheRegionFactoryAvailableException:

Second-level cache is used in the application, but property

hibernate.cache.region.factory_class is not given, please either

disable second level cache or set correct region factory class

name to property hibernate.cache.region.factory_class (and make

sure the second level cache provider, hibernate-infinispan, for

example, is available in the classpath).

在hibernate.properties中设置

hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.

EhCacheRegionFactory


The content of element type "session-factory" must match

"(property*,mapping*,(class-cache|collectio .
是hibernate.cfg.xml的<property

name="hibernate.connection.isolation">2</property>位置放错了

sessionFactory(SessionFactory的实例)一旦调用close()方法,就会清

楚C://temp下的缓存

原创粉丝点击