【06-17】开发中遇到的异常
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 }