简体   繁体   English

Java Spring MVC + Hibernate-sessionFactory“不满意的依赖”错误

[英]Java Spring MVC + Hibernate - sessionFactory “unsatisfied dependency” errror

I'm in the middle of creating my first Spring + Hibernate webapp and this is what I have in console: 我正在创建我的第一个Spring + Hibernate Webapp,这就是我在控制台中所拥有的:

Root Cause
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'quizDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.kubacki.entity.Quiz

I tried to find already existing solutions but nothing seems to work, would you be able to help me please? 我试图找到已经存在的解决方案,但似乎没有任何效果,请问您能为我提供帮助吗?

I am not sure if the issue may be caused by bad XML configuration or some mistake in coding itself, from my perspective "sessionFactory" bean is correctly configured in XML and there should be no problem with injecting it. 我不确定该问题是否是由于XML配置错误或编码本身的错误引起的,从我的角度来看,“ sessionFactory” bean是否已正确配置为XML,并且注入它应该没有问题。

Additionally, I'am using Maven for this project but I have already listed all spring + hibernate stuff in pom.xml 此外,我正在为该项目使用Maven,但我已经在pom.xml中列出了所有spring + hibernate的内容

Here is my source code: 这是我的源代码:

QuizDAO: QuizDAO:

package com.kubacki.dao;

import java.util.List;

import com.kubacki.entity.Quiz;

public interface QuizDAO {

    public Quiz getQuiz(int id);

    public List<Quiz> getQuizzes();

}

QuizDAOImpl: QuizDAOImpl:

package com.kubacki.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kubacki.entity.Quiz;

@Repository
public class QuizDAOImpl implements QuizDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Quiz getQuiz(int id) {

        Session tempSession = sessionFactory.getCurrentSession();

        Quiz tempQuiz = tempSession.get(Quiz.class, id);

        return tempQuiz;
    }

    @Override
    public List<Quiz> getQuizzes() {

        Session tempSession = sessionFactory.getCurrentSession();

        Query<Quiz> theQuery = tempSession.createQuery("from quiz", Quiz.class);

        List<Quiz> tempQuizzes = theQuery.getResultList();

        return tempQuizzes;
    }

}

Web.xml: Web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <display-name>WebApp</display-name>

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

dispatcher-servlet.xml: dispatcher-servlet.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:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.kubacki" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/quizdb?useSSL=false" />
        <property name="user" value="root" />
        <property name="password" value="admin" /> 

        <!-- these are connection pool properties for C3P0 -->
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>  

    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.kubacki.entity" />
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
           </props>
        </property>
   </bean>    

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />


</beans>

As per the error 根据错误

No identifier specified for entity: com.kubacki.entity.Quiz

I think, You are missing a field annotated with @Id in Quiz class. 我认为,您在Quiz类中缺少用@Id注释的字段。 Each @Entity must have an @Id - this will be a primary key in your database. 每个@Entity必须具有一个@Id-这将是数据库中的主键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Hibernate配置Spring。创建名称为&#39;usersDao&#39;的bean时出错:通过字段&#39;sessionFactory&#39;表示的不满意依赖性 - Configuting Spring with Hibernate.Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory' Spring 3 MVC + Hibernate 3.5.x:SessionFactory的自动装配和依赖注入 - Spring 3 MVC + Hibernate 3.5.x : Autowiring and Dependency Injection of SessionFactory Hibernate 5和Spring 4 MVC中的SessionFactory NoClassDefFoundError - SessionFactory NoClassDefFoundError in Hibernate 5 and Spring 4 MVC 通过“sessionFactory”未满足的依赖性; 没有可用的“org.hibernate.SessionFactory”类型的合格 bean - Unsatisfied dependency through 'sessionFactory'; No qualifying bean of type 'org.hibernate.SessionFactory' available Spitter不满意的依赖异常Spring MVC - Spitter Unsatisfied Dependency Exception Spring MVC Spring MVC:未找到依赖项类型为[org.hibernate.SessionFactory]的合格Bean - Spring MVC: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency 配置Spring MVC和Hibernate时sessionFactory中出现错误 - Error in sessionFactory while configuring Spring MVC and Hibernate SessionFactory休眠错误(Spring MVC项目) - SessionFactory Hibernate Error (Spring MVC project) Spring web应用+hibernate问题:不满足的依赖通过字段表达 - Spring web application + hibernate problem: Unsatisfied dependency expressed through field 春季:不满意的依存关系 - Spring : Unsatisfied dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM