下面是我的文件:
package com.wyp.bbs.hibernate.DB;// Generated 2010-5-3 12:33:31 by Hibernate Tools 3.2.0.b9import 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.b9import 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
这是怎么回事啊??

解决方案 »

  1.   

     <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”
    试试看
      

  2.   

    还有,有必要时要配上延时加载
    即把lazy=“false”配上
      

  3.   


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

  4.   

    只保存USER的话会不会有问题? 如果还有问题把一对多的配置去掉 光是多对一有没有问题?
      

  5.   

    <key foreign-key="id">
         <column name="headpic" />
    </key>
      

  6.   

    cascade="all" 或 save-update  级联关系,你根据图片名字读取到信息,然后修改,然后在save? User,User和Headsculpture 有关联关系,所以你保存user的时候应该进行关联操作!
    你式下!写出来那么久没式过么?
      

  7.   

    cascade="all" 或 save-update 级联关系,你根据图片名字读取到信息,然后在新增加一个用户, User和Headsculpture 有关联关系,所以你保存user的时候应该进行关联操作!
    写出来那么久为什么不试呢?
      

  8.   

    主键生成策略还是用incriment比较好吧。确保user表中的headpic字段对应pic表中的主键。