org.hibernate.hql.internal.ast.QuerySyntaxException:圆圈未映射[来自Circle]

时间:2016-08-08 12:17:12

标签: spring hibernate hibernate-mapping

我正在构建一个示例spring hibernate application.Spring部分工作正常。在hibernate中我已经映射了bean类和table.Still我得到一个错误,说bean类没有映射。请找到附加的代码以供参考。需要你的帮助解决这个问题。

Circle.java

    package re.test;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import org.hibernate.annotations.*;
    import javax.persistence.Table;
    @Entity
    @Table(name = "EmployeeMaster1")
    public class Circle {
    @Id
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    }

HibernateDAOImpl.java

    package re.test;
    import org.hibernate.Query;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    @Repository
    public class HibernateDAOImpl {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public int getTableCount()
    {
        System.out.println("test");
        String varSql = "from Circle";
        Query query = getSessionFactory().openSession().createQuery(varSql);
        return ((Long) query.uniqueResult()).intValue();            
    }
    }

JDBCDemo.java
    package re.test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class JDBCDemo {
    public static void main(String[] args) {
        ApplicationContext ctx = new      ClassPathXmlApplicationContext("Spring.xml");
        HibernateDAOImpl dao = ctx.getBean("hibernateDaoImpl",HibernateDAOImpl.class);
        int cnt = dao.getTableCount();
    }
    }

spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    <context:annotation-config />

    <context:component-scan base-package = "re.test" />
    <bean id="circle" class="re.test.Circle" autowire="byName"> 
    </bean>

    <bean id="dataSource"       class="org.springframework.jdbc.datasource.DriverManagerDataSource"     autowire="byName">
        <property name="driverClassName"     value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="url"   value="jdbc:sqlserver://localhost:1433;databaseName=JAVADB"/>
        <property name="username" value="sa"/>
        <property name="password" value="dbadmin@234"/>
    </bean>

    <bean id="hibernateDaoImpl" class="re.test.HibernateDAOImpl"/>

    <bean id="sessionFactory"      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop     key="dialect">org.hibernate.dialect.SQLServerDialect</prop>
            </props>
        </property>
    </bean>

    </beans>

Following is the stack trace

Aug 08, 2016 4:53:10 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1077fc9: startup date [Mon Aug 08 16:53:10 IST 2016]; root of context hierarchy
Aug 08, 2016 4:53:10 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Spring.xml]
Aug 08, 2016 4:53:11 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
Aug 08, 2016 4:53:12 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.3.Final}
Aug 08, 2016 4:53:12 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.11.Final}
Aug 08, 2016 4:53:12 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 08, 2016 4:53:12 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 08, 2016 4:53:12 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
Aug 08, 2016 4:53:12 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Aug 08, 2016 4:53:12 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
test
**Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: Circle is not mapped [from Circle]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
    at re.test.HibernateDAOImpl.getTableCount(HibernateDAOImpl.java:26)
    at re.test.JDBCDemo.main(JDBCDemo.java:14)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Circle is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:338)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3678)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3567)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:708)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:564)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 9 more**

1 个答案:

答案 0 :(得分:1)

Spring是这个错误,因为你的bean没有在spring.xml中注册。当您使用基于xml的配置时,请检查spring类。它应该类似于:

 <beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>com.mypackage.model.Person</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
     <beans:props>
     <beans:prop key="hibernate.dialect">
               org. hibernate.dialect. MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
 </beans:bean>