用hibernate映射

时间:2013-11-28 17:51:58

标签: hibernate spring-mvc hibernate-mapping

这是我在servlet-context-xml中的bean

<bean id="mySessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="myDataSource" />
            <property name="packagesToScan">
                <array>
                    <value>com.mufi.controller</value>
                    <value>com.mufi.objeto</value>
                    <value>com.mufi.dao.impl</value>
                </array>
            </property>
            <!-- <property name="annotatedClasses"> <list> <value>com.objeto.Formato</value> 
                <value>com.objeto.Timbre</value> <value>com.objeto.Detalle</value> <value>com.objeto.ArteCorreo</value> 
                <value>com.objeto.Almacen</value> <value>com.objeto.Movimiento</value> <value>com.objeto.Usuario</value> 
                <value>com.objeto.ObjetoPostal</value> <value>com.objeto.Pais</value> <value>com.objeto.Prefilatelia</value> 
                </list> </property> -->
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.current_session_context_class">thread</prop>
                    <!-- <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> 
                        <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">thread</prop> -->
                </props>
            </property>
        </bean>

当我启动服务器时抛出此异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'timbreDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.mufi.dao.impl.TimbreDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/servlet-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: persistent class not known: int
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.mufi.dao.impl.TimbreDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/servlet-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: persistent class not known: int
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 29 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/servlet-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: persistent class not known: int
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 31 more
Caused by: org.hibernate.MappingException: persistent class not known: int

这是我的DAO:

@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class TimbreDAOImpl {

    @Autowired private SessionFactory sessionFactory;
    private Session session;



    @Transactional
    public List<Timbre> findAll() {
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {
            tx = session.beginTransaction();
            List timbres = session.createQuery("from Timbre").list();
            tx.commit();
            return timbres;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return null;
        }


    }

    @Transactional
    public List<Formato> findAllFormatos() {
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {
            tx = session.beginTransaction();
            List<Formato> formatos = session.createQuery("from formato").list();
            System.out.println("Pase :'D");
            tx.commit();
            return formatos;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return null;
        }


    }

    @Transactional
    public boolean addTimbre(Timbre t){

        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {

            tx = session.getTransaction();


            session.save(t);
            tx.commit();
            return true;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return false;
        }



    }

    @Transactional
    public Timbre searchTimbre(int idTimbre){
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {
            tx = session.beginTransaction();
            Timbre t = (Timbre) session.get(Pizza.class, idTimbre);
            tx.commit();
            return t;


        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return null;
        }


    }   

    @Transactional
    public boolean deleteTimbre(int id){
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {
            tx = session.getTransaction();
            session.delete(searchTimbre(id));
            tx.commit();
            return true;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return false;
        }


    }

    @Transactional
    public boolean editTimbre(Timbre t){
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {

            tx = session.getTransaction();

            session.update(t);
            tx.commit();
            return true;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return false;
        }


    }

}

每个对象映射如下:

package com.mufi.objeto;

import java.io.Serializable;

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

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

    /**
     * 
     */
    private static final long serialVersionUID = -5855838969946740041L;
    @Id
    @Column(name="idAlmacen")
    @GeneratedValue
    private int idAlmacen;
    @Column(name="nombre")
    private String nombre;
    @Column(name="descripcion")
    private String descripcion;

    public Almacen(){

    }

我认为问题出现在servlet-context.xml中,但我不知道可能是什么,这件事我也不明白:持久化类未知:int,因为int是本机类型java,我看到像Product或User这样的对象的问题,但从未使用过int或String或Double等等。

0 个答案:

没有答案