应用程序启动失败
描述:
com.base.model.abstractDAO中得字段会话需要类型为“org.hibernate.sessionFactory”得bean,但找不到该bean.
我添加了应用程序的实现:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Hibernate dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.3.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序.属性
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
@Configuration
@EnableTransactionManagement
@ComponentScan({"configure"})
@PropertySource({"classpath:application.properties"})
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[]{"com.base","com.base.model"});
sessionFactory.setMappingResources(new String[]{"Employee.hbm.xml"});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hiberante.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.userName"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
return hemf.getSessionFactory();
}
}
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String country;
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public void setCountry(String country){
this.country = country;
}
public String getCountry(){
return this.getCountry();
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", country="
+ country + "]";
}
}
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.base.model.Employee" table="Person">
<id name="id" type="java.lang.Integer">
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true"></column>
</property>
<property name="country" type="java.lang.String">
<column name="country"></column>
</property>
</class>
</hibernate-mapping>
@Component
public class EmployeeDataDaoImpl {
@Autowired
SessionFactory sessionFactory;
public List<Employee> findAllEmployee(){
//// Criteria cri = getSession().createCriteria(Employee.class);
// List<Employee> dbList = cri.list();
// for (Employee employee : dbList) {
// System.out.println(employee.getCountry());
// }
return null;
}
}
我在stackoverflow上查找了相同的错误代码,但没有一个解决方案起作用,因此将它与我的代码一起再次发布在这里。希望别人能指出我错在哪里。
对于初学者来说,您的配置有几个方面
为1。和2。只需删除spring-orm
的
标记以及hibernate-core
和hibernate-entitymanager
管理器依赖项。Spring Boot已经在管理这些了。实际上,您可以删除所有已经由初学者引入的org.springframework
依赖项(实际上还包括hibernate依赖项)。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>html" target="_blank">spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
@Entity
@Table("person")
public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@Column(nullable=false)
private String name;
private String country;
}
@Repository
public class EmployeeDataDaoImpl {
@PersistenceContext
private EntityManager entityManger;
public List<Employee> findAllEmployee(){
return em.createQuery("select e from Employee e", Employee.class).getResultList();
}
}
如果您确实希望使用SessionFactory
的普通hibernate,只需使用HibernateJPasessionFactoryBean
来公开EntityManagerFactory
的基础SessionFactory
。
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
factory.setEntityManagerFactory(emf);
return factory;
}
但是,正如前面提到的,我强烈建议使用普通JPA,因为它更容易设置,并且以JPA的当前状态,它提供的功能几乎与普通Hibernate一样多。
专业提示您对spring-boot-starter-data-jpa
有依赖关系,这意味着您对Spring Data JPA有依赖关系。如果您使用JPA,这将使事情变得更加容易。您可以删除EmployeeDataOImpl
,只需创建一个接口并使用它。
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
问题内容: 每当启动应用程序spring启动时,我都会收到以下错误。 申请开始失败 描述: com.base.model.AbstractDao中的现场会话需要找不到“ org.hibernate.SessionFactory”类型的Bean。 行动: 考虑在配置中定义类型为“ org.hibernate.SessionFactory”的bean。 我添加了我的应用程序的实现: POM.xml 应
我正在尝试用Spring Boot开发一个CRUD web应用程序。我将Hibernate用于我的DAO部分。当我尝试午餐我的主应用程序,我收到以下错误消息: 接口客户端DAO: 类ClienteDAOImpl 类ClienteController: pom.xml:
有人知道我怎么解决这个问题吗?请告诉我,谢谢。
我有两个项目: null 提前感谢您的帮助。
我有一个java项目,它将Spring Boot与JPA结合使用,并将Hibernate用于数据库。我正在尝试建立一个访问数据库的微服务。(我不熟悉微服务和Spring Boot)。 以下是主要课程: IGmCircularsDAO. class: GMCircularsDAOImpl。类别: ParentDAO。班 循环服务。班 当我运行这段代码时,我遇到了以下错误,我已经陷入其中一段时间了。
编辑-更新:我根据本教程创建了一个全新的项目,我注意到,在配置pom后,问题是如果我添加 应用程序类的注释。 我是Spring Boot的新手,我已经创建了一个具有持久性的简单工作应用程序,现在我试图添加Spring security jwt,但它不起作用。 以下是我的项目结构: 在为安全性添加依赖项之前,它在持久性方面都工作得很好,现在是这样。我错过了什么?