1    JPA

1.1  JPA  的使用

https://blog.csdn.net/qq_40088250/article/details/88414810

 

1.2  JPA  Dao层命名规范

https://blog.csdn.net/weixin_39676773/article/details/80930826

 

1.3  JPA  增删改查

https://www.jianshu.com/p/b2baadbabc66

 

1.4  JPA  手动生成Page对象

  • 生成有值对象
Po po = new Po();
po.setId("1");
List<Po> list = new ArrayList();
list.add(po);
Page<Po> page = new PageImpl<>(list);
Mockito.when(...).thenReturn(page);

 

  • 生成空对象
Page<Po> page = Page.empty();

 

 

2    hibernate  常用

2.1  hibernate  如何查看版本号

打开hibernate-core jar包。

 右键好压 打开hibernate-core jar 包、进入如下目录:org\hibernate
在里面有两个dtd、一个:hibernate-configuration-版本号.dtd、一个:hibernate-mapping-版本号.dtd
configuration就是hibernate.cfg.xml的dtd而mapping就是hbm.xml的dtd

 

 

 

2.2  hibernate  何为动态绑定(转)

动态绑定只是叫法不同,你可以理解为多态!
例如,有一个抽像fruit水果父类,其中定义了一个方法抽像eat方法,而apple继承fruit并实现fruit的eat方法,而banana继承fruit并实现fruit的eat方法,
外部调用代码如下:

public void do(Fruit fruit){
fruit.eat();//这里就是动态绑定只有在运行时才知道调用哪一个子类的eat()
}
main(){
do(new Apple());
do(new Banana());
}

 

3    hibernate  报错

3.1  报错:org.hibernate.InvalidMappingException: Could not parse mapping document from resource Students.hbm.xml

经分析:我的出错有两个原因:

1    Student.hbm.xml中的component组件的class类没有写包名;


        <component name="address" class="com.ddwei.student.Address">
         <property name="postcode" column="POSTCODE"/>
         <property name="phone" column="PHONE"/>
         <property name="address" column="ADDRESS"/>
        </component>

 

2    Student.hbm.xml中的property属性的name和实体类定义的不一样;

hbm.xml

  <component name="address" class="com.ddwei.student.Address">
         <property name="postcode" column="POSTCODE"/>
         <property name="phone" column="PHONE"/>
         <property name="address" column="ADDRESS"/>
        </component>

 

实体类:

private String postCode;//邮编
 private String phone;//电话
 private String address;//地址

 

 

 

 

3.2  报错  hibernate3.0 xml配置Unknown entity问题

上网查了好多方法,解决方案如下:

一定要保证你的hibernate.cfg.xml配置文件一定要映射你的实体类(参考我的随笔)

  <mapping resource = "Student.hbm.xml"/>  

 

 

 

3.3  报错  hibernate native 主键无法生成

问题:

报错如下:

ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in com.ddwei.student.Student entry (don't flush the Session after an exception occurs)

 

我遇到这类问题的原因是:hibernate的jar包版本不对。

查过无数答案,最后https://stackoverflow.com/questions/24884527/error-hhh000099-an-assertion-failure-occured  解决了我的问题:

 

 
I found the solution for error. I Remove all Hibernate libraries and add latest version of hibernate libraries. 

   

 

 

 

 

 

posted on 2017-12-28 22:25  菜鸟乙  阅读(642)  评论(0)    收藏  举报