标题:懂hibernate请进【外键出错】…[
只看楼主
w397090770
Rank: 2
来 自:江西赣州
等 级:论坛游民
帖 子:36
专家分:10
注 册:2010-1-23
结帖率:75%
 问题点数:0 回复次数:3 
懂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
这是怎么回事啊??
搜索更多相关主题的帖子: hibernate 
2010-05-04 00:10
w397090770
Rank: 2
来 自:江西赣州
等 级:论坛游民
帖 子:36
专家分:10
注 册:2010-1-23
得分:0 
settop
2010-05-08 18:07
w397090770
Rank: 2
来 自:江西赣州
等 级:论坛游民
帖 子:36
专家分:10
注 册:2010-1-23
得分:0 
xz sf
2010-05-08 18:11
w397090770
Rank: 2
来 自:江西赣州
等 级:论坛游民
帖 子:36
专家分:10
注 册:2010-1-23
得分:0 
这个问题我已经解决了
这个问题我已经解决了这个问题我已经解决了
2010-05-08 18:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-305273-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.468574 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved