hibernate 映射文件配置默认值方法

 

通过设置hibernate映射文件的class处 dynamic-insert="true" dynamic-update="true" 和property 里面的insert="false" update="false" 实现 .

两处都要配置!

<property></property>标签属性:update=”true|false”

如果设置为false,则在hibernate的update语句里面没有<property>标签所指明的属性所对应的字段。

同理,insert=”true|false”

如果设置为false,则在hibernate的insert语句里面没有<property>标签所指明的属性所对应的字段。

这样的弊端是无法从表单上填写信息了。

<hibernate-mapping>
    <class name="org.gecs.hibernate.test.AdDepartment" table="AD_DEPARTMENT" schema="BARCODE"
           dynamic-insert="true" dynamic-update="true">
        <id name="adDepartmentId" type="long">
            <column name="AD_DEPARTMENT_ID" precision="22" scale="0" />
            <generator class="sequence">
                <param name="sequence">AD_DEPARTMENT_SEQ</param>
            </generator>
        </id>
        <property name="departmentName" type="string">
            <column name="DEPARTMENT_NAME" length="50" not-null="true" />
        </property>
        <property name="active" type="java.lang.Character" insert="false" update="true">
            <column name="ACTIVE" length="1" />
        </property>
        <property name="createdTime" type="date" insert="false" update="false">
            <column name="CREATED_TIME" length="7" />
        </property>
        <property name="createdUser" type="string">
            <column name="CREATED_USER" length="20" not-null="true" />
        </property>

    </class>
</hibernate-mapping>
注:insert="false" update="false" 的作用是不对当前字段进行insert和update操作,这样hibernate就不会在未指明默认列的情况下将数据库表中默认值字段清空,但同时也会造成无法对此字段插入或更新非默认值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接数据库需要以下信息: 1. 数据库的驱动名称和驱动程序类名。 2. 数据库的 URL 地址、端口号、数据库名称等连接信息。 3. 数据库的用户名和密码等登录信息。 以 MySQL 数据库为例,连接数据库的相关信息如下: 1. 驱动名称为 `MySQL Connector/J`,驱动程序类名为 `com.mysql.cj.jdbc.Driver`。 2. 数据库的 URL 地址为 `jdbc:mysql://localhost:3306/test`,其中 `localhost` 表示数据库所在服务器的地址,`3306` 是 MySQL 数据库的默认端口号,`test` 是数据库名称。 3. 数据库的用户名为 `root`,密码为 `123456`。 Hibernate 的相关配置包括以下内容: 1. 数据库连接配置,包括驱动名称、URL 地址、用户名和密码等。 2. 数据库方言配置,用于指定 Hibernate 与数据库之间的 SQL 方言。 3. 映射文件或注解配置,用于定义数据表和 Java 对象之间的映射关系。 4. Hibernate 缓存配置,用于优化性能。 5. 其他高级配置,如事务管理、连接池配置等。 以 Hibernate 连接 MySQL 数据库为例,相关配置如下: 1. 数据库连接配置: ``` <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> ``` 2. 数据库方言配置: ``` <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> ``` 3. 映射文件或注解配置: 使用注解方式的示例: ``` @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // 省略 getter 和 setter 方法 } ``` 4. Hibernate 缓存配置: ``` <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> ``` 5. 其他高级配置: 可以使用 Hibernate 的事务管理器来管理事务,也可以使用连接池来优化数据库连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值