需要类型的bean的org.hibernate.SessionFactory实例“不能被发现

卡皮尔·乔希:

我得到别的什么时候开始申请春季启动下面的错误。


应用程序未能启动


描述:

在com.base.model.AbstractDao现场会话所需类型的bean的org.hibernate.SessionFactory实例“,可能不会被发现。

行动:

考虑配置中的定义类型的豆“org.hibernate.SessionFactory实例”。

我已经加入执行我的应用程序:

的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>

application.property

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();  
    }  

}

Employee.java

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 + "]";
    }
}

Employee.hbm.xml

<?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>

EmployeeDaoImpl

@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;
}

}

我已经看过了计算器上相同的错误代码,但没有一个解决方案的工作,所以在这里再次与我的代码张贴。其他希望有人能指出我要去的地方错了。

马库斯则:

对于初学者有您的配置有几个事情

  1. 从不同的Spring和Hibernate版本混合罐
  2. 也可能已经成功依赖
  3. 为了做个聪明的话,Spring启动。

对于1和2只是删除<version>了标签spring-ormhibernate-corehibernate-entitymanager经理的依赖。春季启动已经管理这些。实际上,你可以删除所有org.springframework这些都已经在由起动器(实际上是休眠的人也)拉的依赖。

<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>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

接下来,在你的配置至少有2 SessionFactory的配置。我会建议使用注释来定义实体,而不是hbm.xml文件。

@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;

}

当使用JPA注解Hibernate会自动检测您的实体(尤其是春季引导相结合),这使得它非常强大。Ofcourse您现在可以删除Employee.hbm.xml

接下来你EmployeeDataDaoImpl我强烈建议在使用Hibernate使用纯JPA。一般来说,对于您提供足够的与工作。

@Repository
public class EmployeeDataDaoImpl  {


    @PersistenceContext
    private EntityManager entityManger;


    public List<Employee> findAllEmployee(){
        return em.createQuery("select e from Employee e", Employee.class).getResultList();
    }
}

有了这个设置你基本上可以完全删除你的HibernateConfiguration是的,你可以像春天开机检测休眠和自动创建JpaTransactionManager,使交易和预配置一个EntityManagerFactory

如果你真的想使用纯休眠与SessionFactory只使用一个HibernateJpaSessionFactoryBean,露出底层SessionFactoryEntityManagerFactory

@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
    HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
    factory.setEntityManagerFactory(emf);
    return factory;
}

但是如前所述,我会强烈建议使用纯JPA,因为这是一个更容易安装和使用JPA的当前状态,它提供了多达近的功能直接使用Hibernate一样。

专业提示你有依赖spring-boot-starter-data-jpa,这意味着你正在对春数据JPA的依赖。这将使事情,即使你将使用JPA更加容易。您可以移除EmployeeDataDaoImpl,只是创建一个接口并使用它。

public interface EmployeeRepository extends JpaRepository<Employee, Long> {}

这就是它,所有的CRUD方法(findOnefindAllsave提供等)为您无需您创建一个实现。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

现场的JdbcTemplate需要类型的bean的org.springframework.jdbc.core.JdbcTemplate'不能被发现

Spring MVC的:没有合格类型的豆[org.hibernate.SessionFactory实例]发现依赖

参数的构造函数的1所需要类型的bean的org.springframework.social.connect.ConnectionRepository'不能被发现

通过“ sessionFactory”的不满意的依赖;没有可用的'org.hibernate.SessionFactory'类型的合格bean

Micronaut数据-没有类型为[org.hibernate.SessionFactory]的bean

春季引导。找不到类型为“ org.hibernate.SessionFactory”的bean

BeanInstantiationException:无法实例化 [org.hibernate.SessionFactory]:涉及包含 bean 的循环引用

“org.springframework.kafka.core.ConsumerFactory”不能被发现

类型“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder”的豆不能被发现

类所需类型的豆“java.lang.String中”不能被发现

无法建立 Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:无法实例化 id 生成器

定义在Hibernate SessionFactory之前启动的Micronaut bean

无法实例化 [org.springframework.orm.hibernate5.LocalSessionFactoryBean]:工厂方法“sessionFactory”抛出异常

启动 Micronaut 服务器时出错:无法加载 Bean 定义 [org.hibernate.SessionFactory]

NoSuchMethodError:org.hibernate.SessionFactory.getCurrentSession()

导入错误:Org.Hibernate.SessionFactory等

Hibernate sessionFactory的程序化配置需要“ sessionFactory”或“ hibernateTemplate”

参数的构造函数的0所需类型的豆“java.lang.String中”不能被发现

Spring Boot / Thymeleaf / Hibernate:带有Java注释的Sessionfactory Bean

初始SessionFactory创建失败.org.hibernate.HibernateException:缺少列

需要从Hibernate SessionFactory的XML定义引用String JavaBean

java.lang.NoSuchMethodError:org.hibernate.SessionFactory.openSession()Lorg / hibernate / classic / Session

org.hibernate.HibernateException:无法实例化默认tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

Hibernate 5:未知实体,错误类型:org.hibernate.MappingException

org.hibernate.MappingException:无法确定类型

org.hibernate.MappingException:无法确定类型

Hibernate SessionFactory与Spring LocalSessionFactoryBean

Hibernate SessionFactory与JPA EntityManagerFactory

Hibernate + Spring SessionFactory配置