从多个到一个映射HQL获取列表

时间:2017-08-07 02:49:19

标签: java spring spring-mvc hql

我似乎无法从下面的多对一关系中获取列表我从长远来看都有我的实体和数据访问对象我需要这样的事情select * from test,session where session.starttime =? AND session.id=test.sessionid;

这是我的第一个实体,其中fullSession是我的多对一对象。

@Entity
@Table(name = "test")
public class Test {

    public Test() {
        Date date = new Date();
        startTime = new java.sql.Timestamp(date.getTime());
    }


    //the order of this models variables is coupled to the order in they appear in the view//
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "testid")
    private int testId;

    @Column(name = "intensityfeedback")
    private Integer intensityFeedback;

    @Column(name = "sampleid")
    private String sampleId;

    @Column(name = "panelistid")
    private String panelistId;

    @Column(name = "starttime")
    private Timestamp startTime;

    /*
    @Column(name = "sessionId")
    private int sessionId;
    */

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="sessionid")
    private FullSession fullSession;

    public FullSession getFullSession() {
        return fullSession;
    }

    public void setFullSession(FullSession fullSession) {
        this.fullSession = fullSession;
    }


    @Transient
    private int randomNumber;

    @Embedded
    private List<Sample> samples = new ArrayList<Sample>();

    public String getPanelistId() {
        return panelistId;
    }

    public void setPanelistId(String panelistId) {
        this.panelistId = panelistId;
    }

    public int generateRandomNumber(int numSamples) {
        Random rand = new Random();
        randomNumber = rand.nextInt(numSamples) + 1;
        return randomNumber;
    }

    public List<Sample> getSamples() {
        return samples;
    }

    public void setSamples(List<Sample> samples) {
        this.samples = samples;
    }

    public int getTestId() {
        return testId;
    }

    public void setTestId(int testId) {
        this.testId = testId;
    }

    public int getRandomNumber() {
        return randomNumber;
    }

    public void setRandomNumber(int randomNumber) {
        this.randomNumber = randomNumber;
    }

    public String getSampleId() {
        return sampleId;
    }

    public void setSampleId(String sampleId) {
        this.sampleId = sampleId;
    }

    public Integer getIntensityFeedback() {
        return intensityFeedback;
    }

    public void setIntensityFeedback(Integer intensityFeedback) {
        this.intensityFeedback = intensityFeedback;
    }

    public Timestamp getStartTime() {
        return startTime;
    }

    public void setStartTime(Timestamp startTime) {
        this.startTime = startTime;
    }

这是我的第二个实体

@Entity
@Table(name="session")
public class FullSession {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int sessionId;

    @Column(name="starttime")
    private Timestamp startTime;

    @Column(name="endtime")
    private Timestamp endTime;



    public int getSessionId() {
        return sessionId;
    }
    public void setSessionId(int sessionId) {
        this.sessionId = sessionId;
    }

    public Timestamp getStartTime() {
        return startTime;
    }
    public void setStartTime(Timestamp startTime) {
        this.startTime = startTime;
    }
    public Timestamp getEndTime() {
        return endTime;
    }
    public void setEndTime(Timestamp endTime) {
        this.endTime = endTime;
    }

最后这是我的数据访问对象

package iff.spring.dao;

@Repository
public class TestDaoImpl implements TestDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Test> getTests() {
        List<Test> tests;
        // get the current hibernate session
        Session currentSession = sessionFactory.getCurrentSession();
        // create the query
        Query<Test> query = currentSession.createQuery("from Test where FullSession.id = sessionId ", Test.class);
        // execute the query and get the result list
        tests = query.getResultList();
        // return the list// get the current hibernate session
        return tests;
    }

    @Override
    public void saveTest(List<Test> tests) {
        // get current hibernate sessions
        Session currentSession = sessionFactory.getCurrentSession();
        for (Test test : tests) {
            currentSession.save(test);
        }
    }

2 个答案:

答案 0 :(得分:0)

使用spring注释时,需要在applicationContext.xml文件中添加配置。

&lt; context:component-scan base-package =“pack.pack”/&gt; //pack.pack例如:com.dao

然后,

您需要加载applicationContext.xml文件。

代码: ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);

我建议您粘贴控制台错误信息!

答案 1 :(得分:0)

经过多次试验和错误后,我使用以下查询实现了我的解决方案

Query<Test> query = currentSession.createQuery("select t from Test t INNER JOIN t.fullSession f where f.startTime = ?",Test.class);