орг.спящий режим.бумага HQL. АСТ. Исключение QuerySyntaxException
Привет,
Я новичок в hibernate и столкнулся с проблемой, которая заключается в том, что org.hibernate.hql.ast.QuerySyntaxException класс, который я использовал, не сопоставлен
вот мои файлы, которыми я пользуюсь :
presistence.хм
<<pre>?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="MainTest"> <class>com.hibernate.Course</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="password" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" /> </properties> </persistence-unit> </persistence>l
и мой основной класс:
package com.main; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.hibernate.Course; import com.hibernate.Factory; public class MainTest { public static void main(String[] args) { MainTest mainTest = new MainTest(); mainTest.listDatabase(); } public void listDatabase() { SessionFactory session = Factory.getSessionFactory(); Session session1 = session.getCurrentSession(); Transaction transaction = session1.beginTransaction(); Query query = session1.createQuery(" from Course "); // List course = // Factory.getPrimaryEM().createQuery("from course").getResultList(); List course = query.list(); Iterator iterator = course.iterator(); while (iterator.hasNext()) { Course course1 = (Course) iterator.next(); System.out.println(course1.getCourseId()); System.out.println(course1.getCourseName()); } } }
и наконец-то класс конечно :
package com.hibernate; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Table; import org.hibernate.annotations.Entity; @Entity @Table(name = "course") public class Course { private int courseId; private String courseName; @Column(name ="idCourse") @GeneratedValue(strategy = GenerationType.AUTO) public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } @Column(name ="CourseName") public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } }
Вот исключение, которое у меня есть:
<pre> 501 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured Initial SessionFactory creation failed. Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Course is not mapped [ from Course] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:314) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3355) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3239) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:726) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344) at $Proxy0.createQuery(Unknown Source) at com.main.MainTest.listDatabase(MainTest.java:27) at com.main.MainTest.main(MainTest.java:18)
Я использовал Facroty в качестве фабрики для sessionFactory.
Sergey Alexandrovich Kryukov
Так в чем же проблема?
--СА
hamzah1
смотрите прием на обновления пожалуйста