【06-17】开发中遇到的异常

本文针对Hibernate框架使用过程中遇到的一些常见错误进行了详细的解析,并提供了具体的解决方案。例如,当执行save操作时遇到Batchupdatereturnedunexpectedrowcountfromupdate[0];actualrowcount:0;expected:1异常时,可以通过设置主键为空来避免这一问题。

hibernate:


bug:

  Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

solution:

  hibernate在执行save(Object obj)时,如果obj的主键不为空,则会去update而不是save操作,此时如果obj的主键不为空,但是在数据库中查不到时则会抛出该异常,因此save时可以显示将主键字段设为Null。

  也有可能是有级联关系的对象没有保存过,在save时

bug:

  a different object with the same identifier value was already associated with the session

solution:

  

bug:

  identifier of an instance of ... is alterde from 150 to null

solution:

  

bug:

  org.hibernate.MappingException: Unknown entity

solution:

  我用的是hibernate5,使用sessionFactory=new Configuration().configure().buildSessionFactory();能够获取到sessionFactory但是hibernate却找不到注解的实体类,抛出MappingException,使用以下方法才能够获取到注解类。

 1     public static SessionFactory sessionFactory;
 2     static{
 3 //        Configuration cfg= new Configuration();
 4 //        cfg.addClass(Person.class);
 5 //        cfg.addClass(Phone.class);
 6 //        cfg.addAnnotatedClass(entity.Person.class);
 7 //        cfg.addAnnotatedClass(entity.Phone.class);
 8 //        sessionFactory=new Configuration().configure().buildSessionFactory();
 9         final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
10                 .configure() // configures settings from hibernate.cfg.xml
11                 .build();
12         try {
13             sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
14         }
15         catch (Exception e) {
16             // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
17             // so destroy it manually.
18             StandardServiceRegistryBuilder.destroy( registry );
19         }
20         
21     }

 

 


 

转载于:https://www.cnblogs.com/achievec/p/5595151.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值