hibernate映射数据库表如何使表中字段默认值生效(如更新当传来的值为null时不替换表中值)

 问题描述:
    hibernate技术中对应数据库中每一个表,都会有一个映射文件与之对应,此文件描述数据库表中每一个字段的类型、长度、是否可空等属性。在进行表中 记录的插入(更新)操作时,hibernate会根据映射文件中的描述自动生成一个包含所有字段的插入(更新)sql语句,此时如果映射文件中某字段的值 为空(NULL)而其在数据库表中定义的默认值不为空,hibernate会将空值插入到表中,而不会使用此字段的默认值。

解决方法:
    在hibernate映射文件对数据库表的描述中,加入dynamic-insert="true"和 dynamic-update="true" 语句,这时hibernate在进行插入(更新)操作时,只会为那些值不为空的字段赋值,而值为空的字段就会使用数据库表中定义的默认值了。


举例说明:
表person:
CREATE TABLE person (
i_id int(11) NOT NULL auto_increment,
c_name varchar(100) NOT NULL default '张三',
PRIMARY KEY   (id)
)

person.hbm.xml:
<hibernate-mapping package="cn.com.lough.model">
    <class
        name="Person"
        table="person"
        lazy="false"
    >
        <meta attribute="sync-DAO">true</meta>
        <cache usage="read-write"/>
        <id
            name="IId"
            type="integer"
            column="i_id"
        >
            <generator class="native"/>
        </id>

        <property
            name="CName"
            column="c_name"
            type="string"
            not-null="false"
            length="128"
        />
</hibernate-mapping>

运行程序
Person p = new Person();
HiFactory.save(p);

此时hibernate生成的sql语句为insert into person(c_name) values(null);

数据库表结果为
i_id    c_name
1       null

修改person.hbm.xml为:
<hibernate-mapping package="cn.com.lough.model">
    <class
        name="Person"
        table="person"
        lazy="false"
        dynamic-insert="true"
    >
        <meta attribute="sync-DAO">true</meta>
        <cache usage="read-write"/>
        <id
            name="IId"
            type="integer"
            column="i_id"
        >
            <generator class="native"/>
        </id>

        <property
            name="CName"
            column="c_name"
            type="string"
            not-null="false"
            length="128"
        />
</hibernate-mapping>

再次运行程序,此时hibernate生成的sql语句为 insert into person() values();
数据库表结果为
i_id    c_name
1       null
2       张三

在C语言中,要实现顺序(数组或者单链)中删除重复节点,通常我们会遍历整个列,对于每一个节点,如果它的已经在之前遇到过,则删除它。这里提供一个简单的单链的例子: ```c #include <stdio.h> #include <stdlib.h> // 定义链结构体 typedef struct Node { int data; struct Node* next; } Node; // 创建新节点 Node* create_node(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); return NULL; } newNode->data = value; newNode->next = NULL; return newNode; } // 删除重复节点(仅保留第一个出现的) void remove_duplicates(Node** head) { if (*head == NULL) { return; } Node* current = *head; Node* prev = NULL; while (current != NULL) { Node* temp = current->next; if (prev != NULL && prev->data == current->data) { free(current); current = temp; } else { prev = current; current = temp; } } *head = prev; // 更新头指针,指向最后一个非重复节点 } // 打印链 void print_list(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { Node* list = create_node(1); // 初始化链 list = create_node(2); list->next = create_node(3); // 添加一些重复 list->next->next = create_node(4); list->next->next->next = create_node(3); list->next->next->next->next = create_node(5); printf("Original list with duplicates:\n"); print_list(list); remove_duplicates(&list); printf("List after removing duplicates:\n"); print_list(list); return 0; } ``` 在这个例子中,`remove_duplicates` 函数会遍历链并删除重复的节点。注意,这个函数修改了传入的头指针直接,所以`head`应该是一个指向指针的指针 (`Node**`)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值