Hibernate实践笔记

1、如何指定使用hibernate.properties还是 hibernate.cfg.xml?
 //使用hibernate.properties
 //通过addClass装入实体类
 Configuration cfg = new Configuration().addClass(Department.class);
 cfg.setProperty("hibernate.show_sql", "true");//指定属性
 
 //使用hibernate.cfg.xml
 Configuration cfg = new Configuration().configure();
2、在Oracle表中指定使用Sequence
 在Department.hbm.xml中使用如下描述:
 
   
    DEPTNO_sequence
    
   
 
 

3、多表操作
 配置 hibernate.cfg.xml 时注意元素书写次序:
 
 
 
 
 表中外键定义   
 表中被参照的键
    
    
   

4、自动产生数据库表 
 Configuration cfg = new Configuration()
  .setProperty(Environment.HBM2DDL_AUTO, "create");
5、查询结果中包含null的项
 对于一些数字项包含null,定义该项时必须使用wrapper类型,而不能是primitive类型,例如使用Double、Float等,而不能是double、float否则会抛出如下错误:
 org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of org.hibernate.scott.Emp.setComm
 at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:203)
 at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:173)
 at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2900)
 at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
 at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:506)
 at org.hibernate.loader.Loader.doQuery(Loader.java:415)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:210)
 at org.hibernate.loader.Loader.doList(Loader.java:1557)
 at org.hibernate.loader.Loader.list(Loader.java:1540)
 at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
 at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:791)
 at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
 at org.hibernate.scott.Main.viewAllScottSlow(Main.java:89)
 at org.hibernate.scott.Main.main(Main.java:129)
Caused by: net.sf.cglib.beans.BulkBeanException
 at org.hibernate.scott.Emp$$BulkBeanByCGLIB$$9815f405.setPropertyValues()
 at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:200)
 ... 14 more
Caused by: java.lang.NullPointerException
 ... 16 more

你可能感兴趣的:(Hibernate,hibernate,primitive,class,generator,exception,wrapper)