懂hibernate请进【外键出错】…[
下面是我的文件程序代码:
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 { 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. 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. 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.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
这是怎么回事啊??