为什么联接表为空

时间:2016-09-30 08:24:58

标签: java oracle hibernate

我在Oracle SQL Developer编辑器中创建了两个表,其实现是多对多,我还创建了他们的hibernate类' TestEmployee'和' TestProject'如下面的代码所示。由于这两个类之间的关系是 多对多,但是一个名为' Employee_Project2'是在Oracle SQL Developer编辑器中创建的,用于保存其他两个表的主要密钥' TestEmployee'和' TestProject'。

值为' TestEmployee'和' TestProject'通过Hibernate插入,如下所示'记录插入'。

我现在遇到的问题是,当我运行以下命令时:

SELECT * from Employee_Project2;
从Oracle SQL Developer Editor得到一个空表,尽管它在Hibernate类的注释中被提到' TestProject'如下:

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Employee_Project2", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "emp_id"))
private Set<TestEmployee> employeesList;

请告诉我为什么尽管“TestEmployee”和#39;和&#39; TestProject&#39;表格,表格&#39; Employee_Project2&#39;是空的吗?

注意

我没有明确地将任何记录插入到&#39; Employee_Project2&#39;既不通过Hibernate也不通过Oracle SQL Developer编辑器,因为我希望记录&#34;主键&#34;通过Hibernate自动插入表格&#39; Employee_Project2&#39;在注释中提到

TestEmployee

@Entity @Table(schema = "afk_owner", name = "Test_Employee2")
public class TestEmployee {

@Id
@Column(name = "emp_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequencegen")
@SequenceGenerator(name = "sequencegen", sequenceName = "afk_owner.Test_Employee_seq", allocationSize = 1)
private Long mEmpId;

@Column(name = "emp_name")
private String mEmpName;

@Column(name = "emp_experience")
private int mEmpExperience;

@ManyToMany(cascade = CascadeType.ALL, mappedBy = "employeesList")
private Set<TestProject> mProjectsList;

public Long getmEmpId() {
    return mEmpId;
}

public void setmEmpId(Long mEmpId) {
    this.mEmpId = mEmpId;
}

public String getmEmpName() {
    return mEmpName;
}

public void setmEmpName(String mEmpName) {
    this.mEmpName = mEmpName;
}

public int getmEmpExperience() {
    return mEmpExperience;
}

public void setmEmpExperience(int mEmpExperience) {
    this.mEmpExperience = mEmpExperience;
}

public Set<TestProject> getmProjectsList() {
    return mProjectsList;
}

public void setmProjectsList(Set<TestProject> mProjectsList) {
    this.mProjectsList = mProjectsList;
}

public TestEmployee(String empName, int empExperience) {
    this.mEmpName = empName;
    this.mEmpExperience = empExperience;
}

public TestEmployee() {
    // TODO Auto-generated constructor stub
}

}

TestProject

@Entity @Table(schema = "afk_owner", name = "Test_Project2")
public class TestProject {

@Id
@Column(name = "proj_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequencegen")
@SequenceGenerator(name = "sequencegen", sequenceName = "afk_owner.Test_Project_seq", allocationSize = 1)
private Long mProjId;

@Column(name = "proj_name")
private String mProjName;

@Column(name = "proj_desc")
private String mProjDesc;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Employee_Project2", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "emp_id"))
private Set<TestEmployee> employeesList;

public Long getmProjId() {
    return mProjId;
}

public void setmProjId(Long mProjId) {
    this.mProjId = mProjId;
}

public String getmProjName() {
    return mProjName;
}

public void setmProjName(String mProjName) {
    this.mProjName = mProjName;
}

public String getmProjDesc() {
    return mProjDesc;
}

public void setmProjDesc(String mProjDesc) {
    this.mProjDesc = mProjDesc;
}

public Set<TestEmployee> getEmployeesList() {
    return employeesList;
}

public void setEmployeesList(Set<TestEmployee> employeesList) {
    this.employeesList = employeesList;
}

public TestProject(String projName, String projDesc) {
    this.mProjName = projName;
    this.mProjDesc = projDesc;
}

public TestProject() {
    // TODO Auto-generated constructor stub
}

}

记录插入

    Session session = HibernateUtil.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    /*empAhmad*/
    TestEmployee empAhmad = new TestEmployee();
    TestProject projRoadSteepnessEstimation = new TestProject();
    TestProject projObjectTrackingUsingLIDAR = new TestProject();
    TestProject projSalientRegionDetector = new TestProject();
    TestProject projAutonomousNavigationUsingGNSSSensors = new TestProject();
    Set<TestProject> empAhmadProjLists = new HashSet<>();

    empAhmad.setmEmpName("Ahmad");
    empAhmad.setmEmpExperience(9);

    projRoadSteepnessEstimation.setmProjName("Road Steepness Est");
    projRoadSteepnessEstimation.setmProjDesc("Kalman Filter, Java");
    empAhmadProjLists.add(projRoadSteepnessEstimation);

    projObjectTrackingUsingLIDAR.setmProjName("Object Tracking LIDAR");
    projObjectTrackingUsingLIDAR.setmProjDesc("C++, OpenCV");
    empAhmadProjLists.add(projObjectTrackingUsingLIDAR);

    projSalientRegionDetector.setmProjName("Salient Region Detector");
    projSalientRegionDetector.setmProjDesc("Java, OpenCV");
    empAhmadProjLists.add(projSalientRegionDetector);

    projAutonomousNavigationUsingGNSSSensors.setmProjName("Autonomous Navigation GNSS");
    projAutonomousNavigationUsingGNSSSensors.setmProjDesc("Android, Kalman Filter");
    empAhmadProjLists.add(projAutonomousNavigationUsingGNSSSensors);

    empAhmad.setmProjectsList(empAhmadProjLists);

    /*empAmr*/
    TestEmployee empAmr = new TestEmployee();
    TestProject projKalmanForOnlineEstimation = new TestProject();
    TestProject projNonLinearControlAndFiltering = new TestProject();
    TestProject projAppForHydrolicProcess = new TestProject();
    Set<TestProject> empAmrProjList = new HashSet<>();

    empAmr.setmEmpName("Amr");
    empAmr.setmEmpExperience(5);

    projKalmanForOnlineEstimation.setmProjName("Kalman For Online Estimation");
    projKalmanForOnlineEstimation.setmProjDesc("Kalman Filter, Java, C++");
    empAmrProjList.add(projKalmanForOnlineEstimation);

    projNonLinearControlAndFiltering.setmProjName("Non-Linear Control And Filtering");
    projNonLinearControlAndFiltering.setmProjDesc("C++, wavelet analysis");
    empAmrProjList.add(projNonLinearControlAndFiltering);

    projAppForHydrolicProcess.setmProjName("App For Hydrolic Process");
    projAppForHydrolicProcess.setmProjDesc("Android, OpenCV, C++");
    empAmrProjList.add(projAppForHydrolicProcess);

    empAmr.setmProjectsList(empAmrProjList);

    /*empAli*/
    TestEmployee empAli = new TestEmployee();
    Set<TestProject> empAliProjList = new HashSet<>();

    empAli.setmEmpName("Ali");
    empAli.setmEmpExperience(7);

    empAliProjList.add(projAutonomousNavigationUsingGNSSSensors);
    empAliProjList.add(projObjectTrackingUsingLIDAR);
    empAliProjList.add(projKalmanForOnlineEstimation);
    empAliProjList.add(projAppForHydrolicProcess);

    empAli.setmProjectsList(empAliProjList);

    session.persist(empAhmad);
    session.persist(empAmr);
    session.persist(empAli);
    transaction.commit();

1 个答案:

答案 0 :(得分:2)

因为您从未在协会的所有方面插入任何内容:Project.employeesList。就如此容易。

您只填充了关联的反面:Employee.mProjectsList,但Hibernate只关心拥有方。