Проблем с картографирането на Spring Hibernate Class

Просто работя с уроци за mkyong. И надграждане на урок за използване на анотации на Spring3 и хибернация 3.6.9. По-старата версия работи, но с моята модификация получавам тази грешка

org.springframework.orm.hibernate3.HibernateQueryException: Customer is not mapped [from Customer]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:664)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)
    at com.mkyong.customer.dao.impl.CustomerDaoImpl.findAllCustomer(CustomerDaoImpl.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at $Proxy10.findAllCustomer(Unknown Source)
    at com.mkyong.customer.bo.impl.CustomerBoImpl.findAllCustomer(CustomerBoImpl.java:30)
    at com.mkyong.CustomerBean.getCustomerList(CustomerBean.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:470)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:159)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:106)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:178)
    at javax.faces.component.UIData.getValue(UIData.java:553)
    at javax.faces.component.UIData.getDataModel(UIData.java:1293)
    at javax.faces.component.UIData.setRowIndex(UIData.java:446)
    at com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:77)
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:824)
    at javax.faces.component.UIData.encodeBegin(UIData.java:936)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1661)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1666)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:389)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:127)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:117)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:135)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:335)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Customer is not mapped [from Customer]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    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:327)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:256)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    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:124)
    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:1770)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:914)
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:1)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)

Моят код и файловете за картографиране са както по-долу

Persistence.xml

<persistence 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_1_0.xsd"
        version="1.0">

    <persistence-unit name="testa" transaction-type="RESOURCE_LOCAL">
        <class>com.mkyong.customer.model.Customer</class>
    </persistence-unit>
</persistence>

Клас обект

package com.mkyong.customer.model;

import java.io.Serializable;
import java.util.Date;

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

@Entity
@Table(name = "customer")
public class Customer implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "CUSTOMER_ID")
    public long customerId;

    @Column(name = "NAME")
    public String name;

    @Column(name = "ADDRESS")
    public String address;

    @Column(name = "CREATED_DATE")
    public Date createdDate;

// getters setters below 
}

DAO

package com.mkyong.customer.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mkyong.customer.dao.CustomerDao;
import com.mkyong.customer.model.Customer;

@Repository("customerDao")
@Transactional
public class CustomerDaoImpl implements CustomerDao {

    private HibernateTemplate hibernateTemplate;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
    }


    public void addCustomer(Customer customer) {

        customer.setCreatedDate(new Date());
        hibernateTemplate.saveOrUpdate(customer);

    }

    public List<Customer> findAllCustomer() {
                //This throws error
        return hibernateTemplate.find("from Customer");

    }
}

контекст на приложението

<?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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- holding properties for database connectivity / -->
    <import resource="classes/config/spring/beans/DataSource.xml" />
    <!-- <import resource="classes/config/spring/beans/HibernateSessionFactory.xml"/> -->
    <!-- enabling annotation driven configuration / -->
    <context:annotation-config />
    <context:component-scan base-package="com.mkyong" />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="packagesToScan" value="org.adit.spring.hibernate.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <!-- uncomment this for first time run -->
                <prop key="hibernate.hbm2ddl.auto">false</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
        <property name="persistenceUnitName" value="testa"></property>
    </bean>

    <bean id="jpaAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
        p:database="MYSQL" p:showSql="true" />
</beans>

datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
        <value>WEB-INF/classes/config/database/db.properties</value>
   </property>
</bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

</beans>

person Java Ka Baby    schedule 22.01.2012    source източник
comment
Вашият файл наистина ли се казва persistance.xml? Трябва да е persistence.xml.   -  person JB Nizet    schedule 22.01.2012
comment
наименуван е правилно. Съжалявам, ще редактирам въпроса.   -  person Java Ka Baby    schedule 22.01.2012


Отговори (2)


Би било полезно, ако ни покажете файла spring context.xml. Но това, което е странно, е, че използвате файл persistence.xml, което показва, че искате да използвате JPA, но вашият DAO не използва JPA: той използва собствения API на Hibernate.

Предполагам, че фабриката за сесии не е конфигурирана правилно, защото предполагате, че тя автоматично ще прочете файла persistence.xml, което няма да е така, ако не конфигурирате Hibernate като JPA мениджър на обекти.

РЕДАКТИРАНЕ:

Вие декларирате фабрика за сесии в XML, но тя сканира класове в пакета org.adit.spring.hibernate.entity, който не е пакетът на вашия клиентски обект. И вие искате да използвате JPA, така че тази дефиниция на сесиен фабричен компонент не трябва да съществува.

Искате да използвате JPA API, така че трябва да инжектирате мениджър на обекти във вашия DAO, вместо да инжектирате фабрика за сесии. Тази инжекция е възможна благодарение на вашата дефиниция на зърна entityManagerFactory.

Променете вашия DAO код на нещо подобно:

@Repository("customerDao")
@Transactional
public class CustomerDaoImpl implements CustomerDao {

    @PersistenceContext
    private EntityManager em;

    public void addCustomer(Customer customer) {
        customer.setCreatedDate(new Date());
        em.persist(customer);
    }

    public List<Customer> findAllCustomer() {
        TypedQuery<Customer> query = em.createQuery("select c from Customer c", Customer.class);
        return query.getResultList();
    }
}

Прочетете http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#orm-jpa за повече информация.

person JB Nizet    schedule 22.01.2012
comment
Добавено. Знам, че съм изгубен. Да, искам да използвам jpa. Благодаря. - person Java Ka Baby; 22.01.2012

Ако възнамерявате да използвате Hibernate като JPA-имплементация (както изглежда от вашата конфигурация), трябва да използвате JpaTemplate, а не HibernateTemplate. Въпреки това, от пролетта 3.1, JpaTemplate е отхвърлен и трябва да създадете своите DAO с помощта на EntityManager-reference:

Отхвърлено. считано от пролет 3.1, в полза на естественото използване на EntityManager (обикновено получено чрез @PersistenceContext) Имайте предвид, че този клас не е надстроен до JPA 2.0 и никога няма да стане.

@PersistenceContext
private EntityManager entityManager;
person esaj    schedule 22.01.2012