本文介绍了如何使用HQL(注释)从两个表中动态获取数据?我正在发布详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL中有两个名为STUDENT(studentId,studentName,courseId)COURSE(courseId,courseName)的表.对于提到的表,我有两个.java文件为:

I have two tables called STUDENT(studentId,studentName,courseId),COURSE(courseId,courseName) in mysql.I have two .java files for mentioned tables as:

Student.java

Student.java

package com.vaannila.course;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.*;

@Entity
@Table(name="STUDENT")
public class Student {

private long courseId;
private String studentName;
private long studentId;

public Student() {
}

@Id
@Column(name="studentId")
public long getStudentId() {
    return this.studentId;
}

public void setStudentId(long studentId) {
    this.studentId = studentId;
}

@Column(name="studentName", nullable=false)
public String getStudentName() {
    return this.studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

@Column(name="courseId")
public long getCourseIdStu() {
    return this.courseId;
}

public void setCourseIdStu(long courseId) {
    this.courseId = courseId;
}

}

Course.java

Course.java

package com.vaannila.course;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="COURSE")
public class Course {

private long courseId;
private String courseName;

public Course() {
}

public Course(String courseName) {
    this.courseName = courseName;
}
@Id
@Column(name="courseId")
public long getCourseId() {
    return this.courseId;
}

public void setCourseId(long courseId) {
    this.courseId = courseId;
}

@Column(name="courseName", nullable=false)
public String getCourseName() {
    return this.courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

}

hibernate.cfg.xml文件具有

The hibernate.cfg.xml file has

<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/emp</property>
    <property name="hibernate.connection.username">root</property>
    <property name="connection.password">aktpl</property>
    <property name="show.sql" >true</property>
    <mapping class="com.vaannila.course.Course"/>
    <mapping class="com.vaannila.course.Student"/>
  </session-factory>

我的HQL是:String hql = "SELECT s.studentName,c.courseName FROM Student s,Course c where s.courseId=c.courseId and s.studentName=':sname'";(sname是我使用setParameter()设置的输入)

And my HQL is:String hql = "SELECT s.studentName,c.courseName FROM Student s,Course c where s.courseId=c.courseId and s.studentName=':sname'"; (sname is the input here which i set using setParameter())

但是我遇到了这个错误:

But i'm having this error:

org.hibernate.QueryException: could not resolve property: courseId of: com.vaannila.course.Student     [SELECT s.studentName,c.courseName FROM com.vaannila.course.Student s,com.vaannila.course.Course c where s.courseId=c.courseId and s.studentName=':sname']
at         org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1809)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:313)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:485)
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:598)
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:266)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:213)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:118)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114)
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:883)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1246)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4252)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3730)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1923)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1848)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:782)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:583)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
at com.vaannila.course.Main.searchResult(Main.java:184)
at Action.ShowStu.doPost(ShowStu.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)

我想显示一个具有属性"studentName"以及相应的"courseName"的表.请帮帮我!预先谢谢你

I want to show a table having attributes 'studentName' along with corresponding 'courseName'.Please help me out !!! Thank u in advance

推荐答案

Student中带注释的吸气剂称为getCourseIdStu().这意味着该属性被命名为courseIdStu(这是一个可怕的名称,BTW).因此,您必须在HQL中使用此属性名称.

The annotated getter in Student is called getCourseIdStu(). This means that the property is named courseIdStu (which is a terriblename, BTW). You must thus use this property name in your HQL.

此外,您不需要在参数两边加上单引号.因此,查询应为:

Moreover, you don't need the single quotes around your parameter. The query should thus be:

select s.studentName, c.courseName from Student s, Course c
where s.courseIdStu = c.courseId and s.studentName = :sname

请注意,您错过了Hibernate的一个重要部分,该部分包括提供数据库上的对象图,而不仅仅是提供包含其他对象ID的单个对象.您不应该在Student中拥有一个courseId.相反,您应该与课程建立(多对一)关联:

Note that you missed one important part of Hibernate, which consists in providing an object graph on to of a database, and not just individual objects containing IDs of other objects. You shouldn't have a courseId in Student. Instead, you should have a (many-to-one) association with a Course:

@ManyToOne
@JoinColumn(name = "courseId")
public Course getCourse() {
    return this.course;
}

这允许从数据库中获取学生,并导航至其课程.然后,您所拥有的查询可以被重写为:

This allows getting a student from the database, and navigating to its course. And the query you had could then be rewritten as:

select s.studentName, c.courseName from Student s
inner join s.course c
where s.studentName = :sname

这篇关于如何使用HQL(注释)从两个表中动态获取数据?我正在发布详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:12