qq_34966912 2017-09-16 13:52 采纳率: 0%
浏览 349

Spring整合注解的hibernate

我不知道spring要何方神圣才能使用 ,已经配置两周了 ,一直一直一直一直一直配置出错!!!!!!!!
问题就出在这段配置上,百度前面10页的各种方法都用了 ,我不知道一个Spring框架的出现到底是为了在配置上体现出它高超的技术还是为了真的方便管理,起码我孤陋寡闻,配置两周了,我只知道它在配置上确实是用着超高的技术要求,看不到它的其他有点





com.example.tables.Dormitory
com.example.tables.Major
com.example.tables.Register


    严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1692)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1647)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1626)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1600)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1535)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:588)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:189)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 21 more

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-06 22:47
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题是由于Spring Boot中Hibernate整合时,没有正确配置Spring Data JPA。以下是一个解决此问题的方法:

    首先,确保你的项目中包含以下依赖项:

    • Hibernate
    • Spring Data JPA
    • Spring Boot

    然后,在application.properties文件中添加以下内容:

    spring.jpa.show-sql=true
    spring.datasource.url=jdbc:mysql://localhost:3306/testdb
    spring.datasource.username=root
    spring.datasource.password=passw0rd
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.data.jpa.repository.config.annotation.EnableJpaRepositories
    spring.data.jpa.repository.config.EnableJpaAuditing
    

    接下来,创建一个新的Spring Data JPA配置类(例如:DormitoryRepositoryConfig.java),并在其中实现@Repository接口,并继承自AbstractNativePersistenceService. 这样就可以将Hibernate与Spring Data JPA整合在一起。

    最后,修改你的DormitoryRepository.java类以使用Spring Data JPA。

    import org.springframework.data.jpa.repository.JpaRepository;
    import com.example.tables.Dormitory;
    
    public interface DormitoryRepository extends JpaRepository<Dormitory, Long> {
    }
    

    现在,你可以在任何地方注入这个Repository,Spring Boot会自动调用Spring Data JPA进行数据库操作。

    评论

报告相同问题?