错误显示: net.sf.hibernate.PropertyValueException: not-null property references a null or transient value: com.Order.customer
部分原文件:( customer 和 order 类关系:一对多关联)
Order.hbm.xml
……………
< many-to-one
name = "customer"
column = "CUSTOMER_ID"
class = "com.Customer"
not-null = "true"
cascade = "save-update"
/>
执行文件:
………
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();
Customer customer=new Customer();
customer.setName("Jack");
Order order=new Order();
order.setOrderNumber("Jack_Order001");
session.save(customer);
session.save(order);
tx.commit();
原因分析:因为在执行代码中,没有将 customer 和 order 类一对多关联起来,若单独持久化两个类: session.save(customer);session.save(order); 则在保存 order 的时候,由于 CUSTOMER_ID是与 customer类外键,因此无法读取 customer_id, 而在 order.hbm.xml 中指定其不为空,则产生了以上错误。
问题解决: not-null = "true" 改为:not-null="false" 虽然程序无问题,但order表 CUSTOMER_ID为空,不符合逻辑。应该将指定其一对多的关联。
order.setCustomer(customer);
customer.getOrders().add(order);
2〉
错误显示: RROR SessionImpl:2400 - Could not synchronize database state with session
net.sf.hibernate.exception.GenericJDBCException: could not delete collection: [com.Customer.orders#2]
部分原文件:
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class,new Long(3));
session.delete(customer);
tx.commit();
原因分析:因为 cascade默认值为 none,所以当删除customer时,不会自动删除与其关联的order对象。
问题解决:添加语句 cascade = "delete"
3>
错误显示:
17:24:34,992 ERROR JDBCExceptionReporter:58 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'ORDER' 附近有语法错误。
17:24:34,992 WARN JDBCExceptionReporter:57 - SQL Error: 156, SQLState: HY000
17:24:35,002 ERROR JDBCExceptionReporter:58 - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'ORDER' 附近有语法错误。
17:24:35,022 WARN JDBCExceptionReporter:34 - SQL Warning: 0, SQLState:
net.sf.hibernate.exception.GenericJDBCException : could not initialize collection: [com.Customer.orders#2]
部分原文件: order.hbm.xml
< hibernate-mapping >
< class name = "com.Order" table = "ORDER" >
< id name = "id" type = "long" column = "ID" >
< generator class = "increment" />
</ id >
< property name = "orderNumber" type = "string" >
< column name = "ORDER_NUMBER" length = "15" />
</ property >
< many-to-one
name = "customer"
column = "CUSTOMER_ID"
class = "com.Customer"
outer-join = "true"
/>
原因分析:因为 order 表在 SQL 2000 数据库中已经定义了,如果用户在定义了 order 表,并且程序对该表进行连接等操作就会出错
问题解决:将 引用 order 处改为 [order]
< class name = "com.Order" table = "[ORDER]" >
4>
net.sf.hibernate.exception.SQLGrammarException : Could not save object
at net.sf.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:58 )
at net.sf.hibernate.exception.JDBCExceptionHelper.convert( JDBCExceptionHelper.java:29 )
at net.sf.hibernate.impl.SessionImpl.convert( SessionImpl.java:4131 )
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier( SessionImpl.java:794 )
at net.sf.hibernate.impl.SessionImpl.save( SessionImpl.java:749 )
at com.BusinessService.saveCategoryWithCascade( BusinessService.java:54 )
at com.BusinessService.test( BusinessService.java:104 )
at com.BusinessService.main( BusinessService.java:109 )
Caused by: java.sql.SQLException : [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'CATEGORIES' 无效。
5〉
错误显示: net.sf.hibernate.MappingException : Resource: Add valid path not found
部分原文件: hibernate.hbm.xml
< hibernate-configuration >
< session-factory >
< property name = "connection.username" > sa </ property >
< property name = "connection.url" > jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test </ property >
< property name = "dialect" > net.sf.hibernate.dialect.SQLServerDialect </ property >
< property name = "myeclipse.connection.profile" > MSSQL </ property >
< property name = "connection.password" > hheryh </ property >
< property name = "connection.driver_class" > com.microsoft.jdbc.sqlserver.SQLServerDriver </ property >
< mapping resource = "Add valid path" />
</ session-factory >
原因分析:找不到有效的 xml 文件
问题解决:将所有配置文件都加到 resource 里
将< mapping resource = "Add valid path" />改为
< mapping resource = "com/Customer.hbm.xml" />
< mapping resource = "com/Order.hbm.xml" />
6〉 错误显示
net.sf.hibernate.MappingException : Error reading resource: com/Customer.hbm.xml
at net.sf.hibernate.cfg.Configuration.addResource( Configuration.java:340 )
at net.sf.hibernate.cfg.Configuration.doConfigure( Configuration.java:1027 )
at net.sf.hibernate.cfg.Configuration.doConfigure( Configuration.java:983 )
at net.sf.hibernate.cfg.Configuration.configure( Configuration.java:911 )
at net.sf.hibernate.cfg.Configuration.configure( Configuration.java:897 )
at com.BusinessService.<clinit>( BusinessService.java:17 )
Caused by: net.sf.hibernate.MappingException : duplicate import: Customer
at net.sf.hibernate.cfg.Mappings.addImport( Mappings.java:85 )
at net.sf.hibernate.cfg.Binder.bindClass( Binder.java:126 )
at net.sf.hibernate.cfg.Binder.bindRootClass( Binder.java:221 )
at net.sf.hibernate.cfg.Binder.bindRoot( Binder.java:1256 )
at net.sf.hibernate.cfg.Configuration.add( Configuration.java:253 )
at net.sf.hibernate.cfg.Configuration.addInputStream( Configuration.java:289 )
at net.sf.hibernate.cfg.Configuration.addResource( Configuration.java:337 )
... 5 more
部分原文件 :hibernate.hbm.xml
<? xml version = '1.0' encoding = 'UTF-8' ?>
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name = "connection.username" > sa </ property >
< property name = "connection.url" > jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test </ property >
< property name = "dialect" > net.sf.hibernate.dialect.SQLServerDialect </ property >
< property name = "myeclipse.connection.profile" > MSSQL </ property >
< property name = "connection.password" > hheryh </ property >
< property name = "connection.driver_class" > com.microsoft.jdbc.sqlserver.SQLServerDriver </ property >
< mapping resource = "com/Customer.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
主程序:
static{
try{
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
config.addClass(Customer.class);
// Get the session factory we can use for persistence
sessionFactory = config
.configure()
.buildSessionFactory();
}catch(Exception e){e.printStackTrace();}
}
解决方法: config.addClass(Customer.class);
sessionFactory = config.configure().buildSessionFactory();
原因分析: hibernaet 配置文件有两种格式,一种是 xml 格式,一种是普通的 .property 格式 .
在 1.2 版本中,编译时自动会在 path 路径中查找 property 格式的配置文件。但不会查询 xml 格式的配置文件,因此需要在程序中手动添加 config.configure() ,但此时就不要加载了。
上面的程序 加载了一次 config.configure() ,又映射了一次,所以出错。
解决方法:
若配置文件为 xml 格式的,程序编写如下:
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
// Get the session factory we can use for persistence
sessionFactory = config
.configure()
.buildSessionFactory();
若配置文件为 property 格式的,程序编写如下:
// Create a configuration based on the properties file we've put
Configuration config = new Configuration();
config.addClass(Customer.class);
// Get the session factory we can use for persistence
sessionFactory = config.buildSessionFactory();
=========================================================
==============20090929 add=================================
7、Duplicate class/entity mapping 报错的问题
Could not parse configuration: /hibernate.cfg
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping
Caused by: org.dom4j.DocumentException: FWK005 parse may not be called while pa
sing. Nested exception: FWK005 parse may not be called while parsing.
这些错误的解决方法:
总结:
HibernateSessionFactory 中 getSession() 方法加一个 synchronized
或者检查一下是否用了两个数据连接形式,保证使用的是单一实例
分享到:
相关推荐
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
Hibernate继承映射+C3P0代码
该资源包含了一些常用的hibernate关联映射实例。包括一对多,多对多,继承映射,等,是学习hibernate的必备品
关于对Hibernate关联映射的一个总结,其中有各种映射的例子,详细用法 等
hibernate映射枚举类型,hibernate如何映射枚举类型
注:为节省空间,程序中需要的jar包,均在HibernateManytoManyMapCascadingSave.zip\HibernateManytoManyMapCascadingSave\lib\ <br>Hibernate 多对多实体映射实例 <br>学习Hibernate 实体映射的映射的好帮手...
Hibernate容器映射技术(Set、List、Map)
hibernate实体映射文件字段设置默认值
Hibernate 相关映射关系 Hibernate 相关映射关系 Hibernate 相关映射关系
Hibernate高级映射的几个单向关系,单向一对一(共享主键方式、唯一外键方式),单向多对一,单向一对多,单向多对多。
hibernate 对象关系映射总结hibernate 对象关系映射总结hibernate 对象关系映射总结
文件中包含关于hibernate 各个属性的解释
NULL 博文链接:https://chaoyi.iteye.com/blog/2168511
Hibernate对象关系映射一对多 很基础等文档
hibernate集合的映射 集合的映射 set list array map
教你如何用Hibernate生成映射文件和实体类,简单方便。
用Hibernate映射继承关系
student与Score的Map集合关系 student与class的多对一 一对多映射
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接...
hibernate 映射关系 小结