用于从多个表中选择多个值的Hibernate查询

时间:2017-01-20 08:00:23

标签: sql oracle hibernate

enter image description here

以下是我的数据库结构。

我想要以下结果:

对于用户所追踪的任何retrospective_id,我想要iteration_name,isd,ied,project_name和comments:comment_id,comment_type,comment_text。

我应该解雇什么查询?如何连接表?是否需要内/外连接?

我的代码:

IterationInfo.java

package pojoclasses;

import java.util.Date;

public class IterationInfo
{
    private int iteration_id;
    private int project_id;
    private String iteration_name;
    private Date isd;
    private Date ied;

// getter and setter section

}

Projectinfo.java

package pojoclasses;

public class ProjectInfo 
{
    private int project_id;
    private String project_name;
    // getter and setter section
}

UserInfo.java

package pojoclasses;

public class UserInfo 
{
    private int user_id;
    private String user_name;
    private String email_id;
    private int rally_objectid;
     // getter and setter section
}

RetrospectiveInfo.java

package pojoclasses;

import java.util.Date;

public class RetrospectiveInfo 
{
    private int retrospective_id;
    private Date retrospective_date;
    private int project_id;
    private int iteration_id;
    private int user_id;
    // getter and setter section
}

PageInfo.java

package pojoclasses;

import java.util.Date;

public class PageInfo 
{
    private int comment_id;
    private String comment_text;
    private String comment_type;
    private int user_id;
    private int retrospective_id;
    private Date creation_date;
    private Date modification_date;
    // getter and setter section
}

UserInfo.hbm.cfg

     <?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 20 Jan, 2017 2:07:02 PM by Hibernate Tools 3.5.0.Final -->
 <hibernate-mapping>
 <class name="pojoclasses.UserInfo" table="USERINFO">
    <id name="user_id" type="int">
        <column name="USER_ID" />
        <generator class="assigned" />
    </id>
    <property name="user_name" type="java.lang.String">
        <column name="USER_NAME" />
    </property>
    <property name="email_id" type="java.lang.String">
        <column name="EMAIL_ID" />
    </property>
    <property name="rally_objectid" type="int">
        <column name="RALLY_OBJECTID" />
    </property>
 </class>
 </hibernate-mapping>

RetrospectiveInfo.hbm.xml

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Jan, 2017 2:06:00 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojoclasses.RetrospectiveInfo" table="RETROSPECTIVEINFO">
    <id name="user_id" type="int">
        <column name="USER_ID" />
        <generator class="assigned" />
    </id>
    <property name="retrospective_id" type="int">
        <column name="RETROSPECTIVE_ID" />
    </property>
    <property name="retrospective_date" type="java.util.Date">
        <column name="RETROSPECTIVE_DATE" />
    </property>
    <property name="project_id" type="int">
        <column name="PROJECT_ID" />
    </property>
    <property name="iteration_id" type="int">
        <column name="ITERATION_ID" />
    </property>
</class>
</hibernate-mapping>

Projectinfo.hbm.cfg

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Jan, 2017 2:05:23 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojoclasses.ProjectInfo" table="PROJECTINFO">
    <id name="project_id" type="int">
        <column name="PROJECT_ID" />
        <generator class="assigned" />
    </id>
    <property name="project_name" type="java.lang.String">
        <column name="PROJECT_NAME" />
    </property>
</class>
</hibernate-mapping>

Pageinfo.hbm.cfg

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Jan, 2017 2:03:56 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojoclasses.PageInfo" table="PAGEDETAILS">
    <id name="user_id" type="int">
        <column name="USER_ID" />
        <generator class="assigned" />
    </id>
    <property name="comment_id" type="int">
        <column name="COMMENT_ID" />
    </property>
    <property name="comment_text" type="java.lang.String">
        <column name="COMMENT_TEXT" />
    </property>
    <property name="comment_type" type="java.lang.String">
        <column name="COMMENT_TYPE" />
    </property>
    <property name="retrospective_id" type="int">
        <column name="RETROSPECTIVE_ID" />
    </property>
    <property name="creation_date" type="java.util.Date">
        <column name="CREATION_DATE" />
    </property>
    <property name="modification_date" type="java.util.Date">
        <column name="MODIFICATION_DATE" />
    </property>
</class>
</hibernate-mapping>

IterationInfo.hbm.xml

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Jan, 2017 2:03:23 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojoclasses.IterationInfo" table="ITERATIONINFO">
    <id name="project_id" type="int">
        <column name="PROJECT_ID" />
        <generator class="assigned" />
    </id>
    <property name="iteration_id" type="int">
        <column name="ITERATION_ID" />
    </property>
    <property name="iteration_name" type="java.lang.String">
        <column name="ITERATION_NAME" />
    </property>
    <property name="isd" type="java.util.Date">
        <column name="ISD" />
    </property>
    <property name="ied" type="java.util.Date">
        <column name="IED" />
    </property>
</class>
</hibernate-mapping>

主要Conteroller类

package packagecontroller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/json/retrospective")
public class MainControllerClass 
{
@RequestMapping(value="{userid}", method = RequestMethod.GET)
public @ResponseBody List<ProjectInfo> getIterationInfoInJSON(@PathVariable int userid)

{
    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    SessionFactory SF = con.buildSessionFactory();
    Session session= SF.openSession();
    //Query to be fired..
    session.close();
    SF.close();
    return listiterationinfo;
}
}

在oracle sql数据库中,sql查询给出了我想要的结果:有2个查询:

SELECT ITERATIONINFO.ITERATION_NAME, ITERATIONINFO.ISD, ITERATIONINFO.IED, PROJECTINFO.PROJECT_NAME
FROM RETROSPECTIVEINFO, PROJECTINFO, ITERATIONINFO
WHERE RETROSPECTIVEINFO.RETROSPECTIVE_ID = 500
AND RETROSPECTIVEINFO.PROJECT_ID = PROJECTINFO.PROJECT_ID
AND RETROSPECTIVEINFO.ITERATION_ID = ITERATIONINFO.ITERATION_ID;

SELECT PAGEDETAILS.COMMENT_ID, PAGEDETAILS.COMMENT_TYPE, PAGEDETAILS.COMMENT_TEXT, USERINFO.USER_NAME, USERINFO.EMAIL_ID
FROM PAGEDETAILS, USERINFO, RETROSPECTIVEINFO
WHERE RETROSPECTIVEINFO.RETROSPECTIVE_ID = 500
AND PAGEDETAILS.RETROSPECTIVE_ID = RETROSPECTIVEINFO.RETROSPECTIVE_ID
AND PAGEDETAILS.USER_ID = USERINFO.USER_ID;

1 个答案:

答案 0 :(得分:0)

正如Joe Taras在评论中所说,我认为你必须更好地重写你的pojos和映射,例如我为你编写RetrospectiveInfo类和映射:

 package pojoclasses;

public class RetrospectiveInfo 
        {
            private int retrospective_id;
            private Date retrospective_date;
            private ProjectInfo project;
            private IterationInfo iteration;
            private UserInfo user_id;
            // getter and setter section
        }

你的映射就像这样我为你编辑prjoct映射:

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 Jan, 2017 2:06:00 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="pojoclasses.RetrospectiveInfo" table="RETROSPECTIVEINFO">
    <id name="user_id" type="int">
        <column name="USER_ID" />
        <generator class="assigned" />
    </id>
    <property name="retrospective_id" type="int">
        <column name="RETROSPECTIVE_ID" />
    </property>
    <property name="retrospective_date" type="java.util.Date">
        <column name="RETROSPECTIVE_DATE" />
    </property>
    <many-to-one name="project" column="project_id" entityname="pojoclasses.ProjectInfo" />

    <property name="iteration_id" type="int">
        <column name="ITERATION_ID" />
    </property>
</class>
</hibernate-mapping>

如果你按照我的说法重写它们,你可以写这样的查询:

从RetrospectiveInfo e中选择* 其中e.iteration.iterationName,e.iteration.isd,e.iteration.ied, e.iteration.projec.name

我希望这对你有所帮助。