hibernate映射文件

2021-03-23  本文已影响0人  墨线宝

原文链接https://zhhll.icu/2020/%E6%A1%86%E6%9E%B6/hibernate/hibernate%E6%98%A0%E5%B0%84%E6%96%87%E4%BB%B6/

hibernate映射文件

配置访问持久化类属性的策略

可以在property元素中配置access属性来指定hibernate访问持久化类属性的方式

<property name="customerName" type="java.lang.String" access="property">
            <column name="customer_name"/>
 </property>

设置派生属性

有些属性的值需要在运行时进行计算才可以获取到,此时可以使用property元素的formula属性,该属性用来设置一个SQL表达式,hibernate根据SQL来计算出派生属性的值

<property name="totalPrice" formula="(select sum(price) from orders where custom_id=id)"></property>

在查询的时候就会进行拼接子查询来获取该属性的值

select id,name,(select sum(price) from orders where custom_id=1) from customer where id = 1;

控制字段的插入和更新

hibernate生成的save()、update()语句默认是插入和更新全部的字段,可以进行自定义设置

注意:当表的字段比较多时,可以设置dynamic-insert、dynamic-update来提升性能

由于本身的博客百度没有收录,博客地址http://zhhll.icu

上一篇 下一篇

猜你喜欢

热点阅读