【转载】hibernate重复映射

错误显示: 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
或者检查一下是否用了两个数据连接形式,保证使用的是单一实例
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值