Hibernate的bug,one-to-one延迟加载时的问题
2010-06-20 12:24
260 查看
今天,用Hibernate3.1+spring开发的时候,测试开发的DAO没想到碰到了这个问题,我在做一个用户购物系统,里面的一个用户对应一个账户和一个购物车,
Mysql表如下:
CREATE TABLE ZH_USERS(
ID VARCHAR(100) not null PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(60) NOT NULL,
email VARCHAR(60) NOT NULL,
is_open bit NOT NULL,
reg_date DATE NOT NULL,
unique(username)
)type=InnoDB;
create table ZH_USER_ACCOUNT(
user_id VARCHAR(100) primary key not null,
balance int(10) default 0,
sale_count int(10) default 0,
version int(4) not null default 1,
constraint f_k foreign key(user_id) references zh_users(id) ON DELETE CASCADE
)type=InnoDB;
create table ZH_CART(
user_id VARCHAR(100) not null primary key,
content text,
version int(4) default 0,
type int(2) default 1,
constraint f_k foreign key(user_id) references zh_users(id) ON DELETE CASCADE
)TYPE=InnoDB;
关联为
User one-to-one Cart User one-to-one Account
user.hbm.xml文件如下:
<one-to-one name="cart" class="org.model.businessobject.Cart" cascade="all"></one-to-one>
<one-to-one name="account" class="org.model.businessobject.Account" cascade="all" fetch="join"></one-to-one>
cart.hbm.xml
<one-to-one name="user" class="org.model.businessobject.User" constrained="true" foreign-key="id" />
问题出来了;我想让User中的cart是延迟加载可以我用了不行所以看了一些网上资料,还翻了翻书说在user.hbm.xml中one-to-one中加入constrained="true"就可以了
我测试了一下果然没有问题,心里正喜,可是没想到这样却带来了一个新的问题,不能够insert提示错误为Cannot add or update a child row: a foreign key constraint fails
我就把这些代码都放到google中果然有很多出现这种问题都没有解答,有一片说是因为type=innoDB的原因,我改了就好了,可是这样就不能支持事务了,我看了另一篇他的办法和我后来做的一样就是不用延迟加载了。呵呵
Mysql表如下:
CREATE TABLE ZH_USERS(
ID VARCHAR(100) not null PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(60) NOT NULL,
email VARCHAR(60) NOT NULL,
is_open bit NOT NULL,
reg_date DATE NOT NULL,
unique(username)
)type=InnoDB;
create table ZH_USER_ACCOUNT(
user_id VARCHAR(100) primary key not null,
balance int(10) default 0,
sale_count int(10) default 0,
version int(4) not null default 1,
constraint f_k foreign key(user_id) references zh_users(id) ON DELETE CASCADE
)type=InnoDB;
create table ZH_CART(
user_id VARCHAR(100) not null primary key,
content text,
version int(4) default 0,
type int(2) default 1,
constraint f_k foreign key(user_id) references zh_users(id) ON DELETE CASCADE
)TYPE=InnoDB;
关联为
User one-to-one Cart User one-to-one Account
user.hbm.xml文件如下:
<one-to-one name="cart" class="org.model.businessobject.Cart" cascade="all"></one-to-one>
<one-to-one name="account" class="org.model.businessobject.Account" cascade="all" fetch="join"></one-to-one>
cart.hbm.xml
<one-to-one name="user" class="org.model.businessobject.User" constrained="true" foreign-key="id" />
问题出来了;我想让User中的cart是延迟加载可以我用了不行所以看了一些网上资料,还翻了翻书说在user.hbm.xml中one-to-one中加入constrained="true"就可以了
我测试了一下果然没有问题,心里正喜,可是没想到这样却带来了一个新的问题,不能够insert提示错误为Cannot add or update a child row: a foreign key constraint fails
我就把这些代码都放到google中果然有很多出现这种问题都没有解答,有一片说是因为type=innoDB的原因,我改了就好了,可是这样就不能支持事务了,我看了另一篇他的办法和我后来做的一样就是不用延迟加载了。呵呵
相关文章推荐
- Hibernate中OneToOne延时加载的问题-现象和原因
- hibernate annotations OneToMany与ManyToOne 分析 以及延迟加载性能分析
- failed to lazily initialize a collection of role: no session or session was closed (Hibernate) 解决方法(延迟加载问题)
- hibernate延迟加载会出现的问题(failed to lazily initialize)
- hibernate annotations OneToMany与ManyToOne 分析 以及延迟加载性能分析
- Hibernate映射mysql问题 one-to-one
- 关于hibernate中的load,get,以及延迟加载问题
- Hibernate延迟加载以及利用Spring事务完美解决延迟加载问题
- 解决Hibernate 注解在属性上 ,多对一,一对一懒加载 当调用one那一方getId 也会查数据库的问题
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org....
- Hibernate中的session和load延迟加载矛盾问题,如何解决?
- Hibernate延迟加载问题
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org....
- hibernate 延迟加载的错误 failed to lazily initialize
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
- hibernate的many-to-one的级联删除问题
- WebLogic下开发Hibernate问题 java.lang.NoSuchMethodError:javax.persistence.OneToMany.orphanRemoval()Z
- hibernate 延迟加载问题探讨
- 关于Hibernate延迟加载的问题
- hibernate 延迟加载问题探讨