如何使用HQL(Annotations)从两个表动态获取数据?我发布了详细信息

时间:2012-03-15 06:50:41

标签: hql

我在mysql中有两个名为STUDENT(studentId,studentName,courseId)COURSE(courseId,courseName)的表。我有两个.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

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文件有

<?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()设置的输入)

但我有这个错误:

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'。 请帮帮我!!!提前谢谢你

1 个答案:

答案 0 :(得分:2)

Student中带注释的getter称为getCourseIdStu()。这意味着该属性名为courseIdStu(这是一个糟糕的名字,BTW)。因此,您必须在HQL中使用此属性名称。

此外,您不需要围绕参数的单引号。因此查询应为:

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

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

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

这允许从数据库中获取学生,并导航到其课程。然后可以将您的查询重写为:

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