双向一对多配置错误,可以帮忙看看吗

package cn.itcast.F_Many2Many;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App1 {

private static SessionFactory sf;
static{
    sf = new Configuration()
            .configure()
            .addClass(Project.class)
            .addClass(Developer.class)
            .buildSessionFactory();
}


@Test
public void save(){
    Session session = sf.openSession();

    Project pro_ds = new Project();
    pro_ds.setP_name("电商");

    Project pro_oa = new Project();
    pro_oa.setP_name("OA系统");

    Developer dev_zs = new Developer();
    dev_zs.setD_name("张三");

    Developer dev_ls = new Developer();
    dev_ls.setD_name("李四");

    Developer dev_ww = new Developer();
    dev_ww.setD_name("王五");

    Developer dev_ml = new Developer();
    dev_ml.setD_name("马六");

    pro_ds.getDeve().add(dev_zs);//电商   张三  李四 王五
    pro_ds.getDeve().add(dev_ls);
    pro_ds.getDeve().add(dev_ww);

    pro_oa.getDeve().add(dev_ls);  // oa 李四王五 马六
    pro_oa.getDeve().add(dev_ww);
    pro_oa.getDeve().add(dev_ml);



    session.save(pro_ds);
    session.save(pro_oa);



    session.beginTransaction().commit();
    session.close();

}

}

 package cn.itcast.F_Many2Many;

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

public class Project {

    private int p_id;
    private String p_name;

    //一个项目多个人员
    private Set<Developer> deve = new HashSet<Developer>();

    public int getP_id() {
        return p_id;
    }

    public void setP_id(int p_id) {
        this.p_id = p_id;
    }

    public String getP_name() {
        return p_name;
    }

    public void setP_name(String p_name) {
        this.p_name = p_name;
    }

    public Set<Developer> getDeve() {
        return deve;
    }

    public void setDeve(Set<Developer> deve) {
        this.deve = deve;
    }




}

package cn.itcast.F_Many2Many;

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

public class Developer {

private int  d_id;
private String d_name;

//一个人员属于多个项目
private Set<Project> pro = new HashSet<Project>();

public int getD_id() {
    return d_id;
}

public void setD_id(int d_id) {
    this.d_id = d_id;
}

public String getD_name() {
    return d_name;
}

public void setD_name(String d_name) {
    this.d_name = d_name;
}

public Set<Project> getPro() {
    return pro;
}

public void setPro(Set<Project> pro) {
    this.pro = pro;
}

}

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
     <class name="cn.itcast.F_Many2Many.Developer"  table="F_Developer">
         <id name="d_id">
            <generator class="native"/>
         </id>

         <property name="d_name" length="10"/>

         <set name="pro" table="F_Project" >
             <key column="p_id"></key>
             <one-to-many class="cn.itcast.F_Many2Many.Project" />
         </set>


     </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">





  <property name="p_name"></property>

  <set name="deve" table="F_Developer"  cascade="save-update,delete" inverse="true">
    <key column="p_id"></key>
    <one-to-many class="cn.itcast.F_Many2Many.Developer"/>
  </set>


1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐