懂hibernate请进【外键出错】…

过往记忆
博客专家认证
2010-05-04 12:07:15
下面是我的文件:

package com.wyp.bbs.hibernate.DB;

// Generated 2010-5-3 12:33:31 by Hibernate Tools 3.2.0.b9

import java.util.HashSet;
import java.util.Set;

/**
* User generated by hbm2java
*/
public class User implements java.io.Serializable {

private int id;

private Headsculpture headsculpture;//头像

private String name;

public User() {
}


public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public Headsculpture getHeadsculpture() {
return this.headsculpture;
}

public void setHeadsculpture(Headsculpture headsculpture) {
this.headsculpture = headsculpture;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}
上面的是user类,他和user表对应,user表的headpic和headsculpture表中的Hpath是关联的,
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.wyp.bbs.hibernate.DB.User" table="user" catalog="bbs">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="identity" />
</id>
<many-to-one name="headsculpture" class="com.wyp.bbs.hibernate.DB.Headsculpture" fetch="select" >
<column name="headpic" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
</class>
</hibernate-mapping>


Headsculpture 类
package com.wyp.bbs.hibernate.DB;

// Generated 2010-5-3 12:33:31 by Hibernate Tools 3.2.0.b9

import java.util.HashSet;
import java.util.Set;

/**
* Headsculpture generated by hbm2java
*/
public class Headsculpture {

private int id;

private String hpath;


private Set users = new HashSet(0);

public Headsculpture() {
}



public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getHpath() {
return this.hpath;
}

public void setHpath(String hpath) {
this.hpath = hpath;
}



public Set getUsers() {
return this.users;
}

public void setUsers(Set users) {
this.users = users;
}

}

Headsculpture 的配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.wyp.bbs.hibernate.DB.Headsculpture" table="headsculpture" catalog="bbs">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="identity" />
</id>
<property name="hpath" type="java.lang.String">
<column name="Hpath" not-null="true" />
</property>

<set name="users" inverse="true" >
<key>
<column name="headpic" />
</key>
<one-to-many class="com.wyp.bbs.hibernate.DB.User" />
</set>

</class>
</hibernate-mapping>


下面是测试类

package com.wyp.bbs.Impl;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.wyp.bbs.hibernate.DB.Headsculpture;
import com.wyp.bbs.hibernate.DB.HeadsculptureDAO;
import com.wyp.bbs.hibernate.DB.UserDAO;

import com.wyp.bbs.hibernate.DB.User;


public class UserClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDAO userDao = new UserDAO() ;

User user = new User() ;

HeadsculptureDAO Dao = new HeadsculptureDAO();

List list = Dao.findByHpath("default.gif") ;

Headsculpture headPic =(Headsculpture)list.get(0);

user.setHeadsculpture(headPic);

System.out.println(headPic.getHpath());
user.setName("wyp") ;
userDao.save(user) ;

}

}

上面的代码运行时出现下面的错误
Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`bbs`.`user`, CONSTRAINT `FK_user_1` FOREIGN KEY (`headpic`) REFERENCES `headsculpture` (`Hpath`) ON DELETE SET NULL ON UPDATE SET NULL)"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 17 more
这是怎么回事啊??
...全文
273 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
swit1983 2010-05-10
  • 打赏
  • 举报
回复
主键生成策略还是用incriment比较好吧。确保user表中的headpic字段对应pic表中的主键。
bb100044535 2010-05-04
  • 打赏
  • 举报
回复
还有,有必要时要配上延时加载
即把lazy=“false”配上
bb100044535 2010-05-04
  • 打赏
  • 举报
回复
<many-to-one name="headsculpture" class="com.wyp.bbs.hibernate.DB.Headsculpture" fetch="select" >
<column name="headpic" />
</many-to-one>

应改成:
<many-to-one name="headsculpture" class="com.wyp.bbs.hibernate.DB.Headsculpture" fetch="select" >
<column name="Id" />
</many-to-one>

还有
<set name="users" inverse="true" >
<key>
<column name="headpic" />
</key>
<one-to-many class="com.wyp.bbs.hibernate.DB.User" />
</set>
应该为:
<set name="users" inverse="true" >
<key>
<column name="Id" />
</key>
<one-to-many class="com.wyp.bbs.hibernate.DB.User" />
</set>

你要配多对一或一对多要记得配上主表"headsculpture"的id即“Id”
试试看
过往记忆 2010-05-04
  • 打赏
  • 举报
回复
这个异常我还真是无语,弄了好久,还是没有解决啊??呵呵……希望大家帮帮啊!!急救啊!!5555555
过往记忆 2010-05-04
  • 打赏
  • 举报
回复
呵呵,不是我没有去操作,我已经修改了好多次数,还是出现那样的异常。
xiaomaha 2010-05-04
  • 打赏
  • 举报
回复
cascade="all" 或 save-update 级联关系,你根据图片名字读取到信息,然后在新增加一个用户, User和Headsculpture 有关联关系,所以你保存user的时候应该进行关联操作!
写出来那么久为什么不试呢?
xiaomaha 2010-05-04
  • 打赏
  • 举报
回复
cascade="all" 或 save-update 级联关系,你根据图片名字读取到信息,然后修改,然后在save? User,User和Headsculpture 有关联关系,所以你保存user的时候应该进行关联操作!
你式下!写出来那么久没式过么?
BearKin 2010-05-04
  • 打赏
  • 举报
回复
<key foreign-key="id">
<column name="headpic" />
</key>
BearKin 2010-05-04
  • 打赏
  • 举报
回复
检查下记录 首先你把数据库的记录全部删除 然后做次更新操作是否好使 然后检查下你那个列是怎么回事
过往记忆 2010-05-04
  • 打赏
  • 举报
回复
单个类是没有关系的,因为但各类就会成功,但是连上外键就不行,老是出现外键重复
swit1983 2010-05-04
  • 打赏
  • 举报
回复
没问题。
BearKin 2010-05-04
  • 打赏
  • 举报
回复
只保存USER的话会不会有问题? 如果还有问题把一对多的配置去掉 光是多对一有没有问题?
xiaomaha 2010-05-04
  • 打赏
  • 举报
回复
cascade="all"
hoojo 2010-05-04
  • 打赏
  • 举报
回复

<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="identity" />
</id>
将主键设置成String型的uuid增长模式,因为identity用的是数据库的自增策略。
所以要在插入以后才能生产id,而uuid则是通过hiberntate自动生产的唯一id。

过往记忆 2010-05-04
  • 打赏
  • 举报
回复
你回复的留言我看了一下,我也和那上面的一样配置来一下,但是还是不行

81,091

社区成员

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

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