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

forevermihoutao 2006-10-16 10:29:48
报错信息:
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或者别的处理。

问题着急,大家帮忙!
...全文
3007 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
marine_chen 2006-10-17
  • 打赏
  • 举报
回复
在this.getHibernateTemplate().save(u);之前加上adv和im的load方法

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

然后就可以save了
marine_chen 2006-10-17
  • 打赏
  • 举报
回复
持久化区分问题

临时对象不能作为持久化加载
forevermihoutao 2006-10-16
  • 打赏
  • 举报
回复
大家多多帮忙,着急
forevermihoutao 2006-10-16
  • 打赏
  • 举报
回复
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的是一个类,这个都是自动生成的,我也不知道要不要修改。
forevermihoutao 2006-10-16
  • 打赏
  • 举报
回复
to theforever(碧海情天) :

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

<many-to-one name="advilige" class="com....model.Advilige" not-null="true">
<column name="useradv" length="32" not-null="true" unique="true"/>
</many-to-one>
<many-to-one name="img" class="com.....model.Img" not-null="true">
<column name="userimg" length="32" not-null="true" unique="true"/>
</many-to-one>

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

<property name="advName" type="string" not-null="true">
<column name="adv_name" length="32" not-null="true" unique="true" />
</property>

img表的映射:
<property name="imgName" type="string" not-null="true">
<column name="img_name" length="32" not-null="true" unique="true" />
</property>

关于这点我已经反复检查很多遍了,还没找到问题。
willishz 2006-10-16
  • 打赏
  • 举报
回复
如果光说这个异常的话,是数据类型不匹配导致的。比如应该传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);
  • 打赏
  • 举报
回复
是你关联的键的NULL属性不一致,一个允许为NULL,一个不允许为NULL.

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

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧