站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题hibernate中有外键的表插入数据报错:not-null property references a null or transient value

hibernate中有外键的表插入数据报错:not-null property references a null or transient value

摘要: hibernate中有外键的表插入数据报错:not-null property references a null or transient value


报错信息:
not-null property references a null or transient value: com.....model.User.advilige; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.....model.User.advilige

org.hibernate.PropertyValueException: not-null property references a null or transient value: com.....model.User.advilige

系统是struts spring hibernate架构,功能是注册。

1,在struts的action的execute方法加载user信息:

...
String img = (String)((RegUser)form).getImage();
String adv = (String)((RegUser)form).getAdv();

Advilige ad = new Advilige(adv);
Img im = new Img(img);

u.setUserName(username);
u.setUserPass(password);
u.setUsernick(usernick);
u.setEmail(email);
u.setImg(im);
u.setAdvilige(ad);

if(regdao.RegUser(u)){
return (map.findForward("success"));
}

其中setImg方法和setAdvilige方法是两个外键,此处我直接加载不知道是否有问题。

2,在spring的DAO里申明保存User信息的方法:

public boolean RegUser(User u){
try{
this.getHibernateTemplate().save(u);
}catch...
}

此处我不知道直接这个save是否会有问题,因为还关联两个外键对应着img和advilige表,不知道这里是不是要进行那两个表的load或者别的处理。

问题着急,大家帮忙!


是你关联的键的NULL属性不一致,一个允许为NULL,一个不允许为NULL.

做外键的两个键必须完全一致才行.


如果光说这个异常的话,是数据类型不匹配导致的。比如应该传Advilige或Img的地方传成了String,或相反。楼主加上端点再检查一下这几句话呢?
String img = (String)((RegUser)form).getImage();
String adv = (String)((RegUser)form).getAdv();

Advilige ad = new Advilige(adv);
Img im = new Img(img);

u.setImg(im);
u.setAdvilige(ad);



to theforever(碧海情天) :

我的user表关于advilige和img的映射是myeclipse自动生成的,我没做修改:








advilige表的映射:(我都加上了notnull属性)





img表的映射:




关于这点我已经反复检查很多遍了,还没找到问题。


to willishz(光与影的奇迹) :

1,String adv = (String)((RegUser)form).getAdv();
是接受表单传递的hidden参数,字符型的。

2,Advilige是myeclipse自动生成的映射advilige表的类,因为adv_name是唯一且不为空,所以有一个重写方法只传入adv_name的方法可以插入数据:
/** minimal constructor */
public Advilige(String advName) {
this.advName = advName;
}
另外还有个完整传入参数的方法:
/** full constructor */
public Advilige(String advName, String advNote, Set users) {
this.advName = advName;
this.advNote = advNote;
this.users = users;
}

3,u.setAdvilige(ad);
user表映射的方法里有一个bean的set get方法,因为这个是外键,所以set的是一个类,这个都是自动生成的,我也不知道要不要修改。



大家多多帮忙,着急


持久化区分问题

临时对象不能作为持久化加载


在this.getHibernateTemplate().save(u);之前加上adv和im的load方法

使临时对象变为持久化对象

然后就可以save了



↑返回目录
前一篇: jsp或html调OCX控件问题,帮帮忙啊!
后一篇: 怎样用javaScript使一个链接不可用?