外键未插入子表中

时间:2015-05-28 04:41:56

标签: java hibernate

我正在尝试插入组织及其人员详细信息(一对多的双向关系)。

但是,此处数据正在插入组织及其人员详细信息,但外键(orgid)未插入orgperson表。

Organization.java

package com.techvision.model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name="organization")
@NamedQuery(name="Organization.findAll", query="SELECT o FROM Organization o")
public class Organization implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int orgid;

@Column(length=45)
private String orgname;

//bi-directional many-to-one association to Orgperson
@OneToMany(mappedBy="organization",cascade=CascadeType.ALL)
private Set<Orgperson> orgpersons;

public Organization() {
}

public int getOrgid() {
    return this.orgid;
}

public void setOrgid(int orgid) {
    this.orgid = orgid;
}

public String getOrgname() {
    return this.orgname;
}

public void setOrgname(String orgname) {
    this.orgname = orgname;
}

public Set<Orgperson> getOrgpersons() {
    return this.orgpersons;
}

public void setOrgpersons(Set<Orgperson> orgpersons) {
    this.orgpersons = orgpersons;
}

public Orgperson addOrgperson(Orgperson orgperson) {

    System.out.println(orgperson.getPersonname());
    getOrgpersons().add(orgperson);
    orgperson.setOrganization(this);

    return orgperson;
}

public Orgperson removeOrgperson(Orgperson orgperson) {
    getOrgpersons().remove(orgperson);
    orgperson.setOrganization(null);

    return orgperson;
}

}

Orgperson.java

package com.techvision.model;
import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="orgperson")
@NamedQuery(name="Orgperson.findAll", query="SELECT o FROM Orgperson o")
public class Orgperson implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int orgpersonid;

@Column(length=45)
private String personname;

//bi-directional many-to-one association to Organization
@ManyToOne
@JoinColumn(name="orgid")
private Organization organization;

public Orgperson() {
}

public int getOrgpersonid() {
    return this.orgpersonid;
}

public void setOrgpersonid(int orgpersonid) {
    this.orgpersonid = orgpersonid;
}

public String getPersonname() {
    return this.personname;
}

public void setPersonname(String personname) {
    this.personname = personname;
}

public Organization getOrganization() {
    return this.organization;
}

public void setOrganization(Organization organization) {
    this.organization = organization;
}

}

MainApp.java

package com.techvision.MainApp;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.techvision.Util.HibernateUtil;
import com.techvision.model.Organization;
import com.techvision.model.Orgperson;
public class MyApp {
public static void main (String args[])
{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Organization org=new Organization();
org.setOrgname("AZZ");
Set<Orgperson> orgset=new HashSet<Orgperson>();

Orgperson orgperson1=new Orgperson();
Orgperson orgperson2=new Orgperson();

orgperson1.setPersonname("ravi");
orgperson2.setPersonname("rams");
orgset.add(orgperson1);
orgset.add(orgperson2);
org.setOrgpersons(orgset);
session.save(org);
session.getTransaction().commit();
session.close();
}
}

hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test_hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.techvision.model.Organization"></mapping>
<mapping class="com.techvision.model.Orgperson"></mapping>
</session-factory>
</hibernate-configuration>

表格详情

CREATE TABLE `organization` (
  `orgid` int(11) NOT NULL AUTO_INCREMENT,
  `orgname` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`orgid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

CREATE TABLE `orgperson` (
  `orgpersonid` int(11) NOT NULL AUTO_INCREMENT,
  `orgid` int(11) DEFAULT NULL,
  `personname` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`orgpersonid`),
  KEY `fK_1_1_idx` (`orgid`),
  CONSTRAINT `fK_1_1` FOREIGN KEY (`orgid`) REFERENCES `organization` (`orgid`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


Organization Table:
------------------
|orgid | orgname |
------------------
|1     |ABC  |
|2     |ZYS  |
------------------
orgperson
-------------------------
orgpersonid|orgid|personname|
-------------------------
|1     | NULL|Jhon|
|2     | NULL|Smith  |
--------------------------

1 个答案:

答案 0 :(得分:0)

尝试以下代码,您没有设置orgperson实体..

    package com.techvision.MainApp;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import com.techvision.Util.HibernateUtil;
    import com.techvision.model.Organization;
    import com.techvision.model.Orgperson;
    public class MyApp {
    public static void main (String args[])
    {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();

    Organization org=new Organization();
    org.setOrgname("AZZ");
    Set<Orgperson> orgset=new HashSet<Orgperson>();

    Orgperson orgperson1=new Orgperson();
    Orgperson orgperson2=new Orgperson();

    orgperson1.setPersonname("ravi");
    orgperson2.setPersonname("rams");
    orgperson1.setOrganization(org);
    orgperson2.setOrganization(org);
    orgset.add(orgperson1);
    orgset.add(orgperson2);
    org.setOrgpersons(orgset);
    session.save(org);
    session.getTransaction().commit();
    session.close();
    }
    }