HibernateJava配置spring引导

HibernateJava配置spring引导,java,spring,hibernate,spring-boot,config,Java,Spring,Hibernate,Spring Boot,Config,您好,我正在尝试从application.properties为hibernate进行Java配置,但代码执行时出现以下错误。 我得到了映射错误,但我检查了所有映射是否正确。它表明在创建sessionFactorybean时id不正确。 我已经试着改正,但我不能请你帮忙 这是我的DBConfig文件: @Configuration @EnableTransactionManagement @PropertySource(value = {"classpath:application.proper

您好,我正在尝试从application.properties为hibernate进行Java配置,但代码执行时出现以下错误。 我得到了映射错误,但我检查了所有映射是否正确。它表明在创建sessionFactorybean时id不正确。 我已经试着改正,但我不能请你帮忙

这是我的DBConfig文件:

@Configuration
@EnableTransactionManagement
@PropertySource(value = {"classpath:application.properties"})
public class DBConfig {

@Value("${spring.datasource.driver-class-name}")
public String driver;

@Value("${spring.datasource.url}")
public String url;

@Value("${spring.datasource.username}")
public String username;

@Value("${spring.datasource.password}")
public String password;

@Value("${spring.jpa.properties.hibernate.dialect}")
public String dialect;

@Value("${spring.jpa.hibernate.ddl-auto}")
public String ddl;

@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);

    return dataSource;
}


@Bean(name = "sessionFactory")
public LocalSessionFactoryBean hibernateSessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan(new String[] { "com.abc.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

//Hibernate Transaction
@Bean 
HibernateTransactionManager transactionManager() {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory((SessionFactory) hibernateSessionFactory());
    return transactionManager;
}



@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
   return new PersistenceExceptionTranslationPostProcessor();
}

Properties hibernateProperties() {
    return new Properties() {
       {
          setProperty("hibernate.hbm2ddl.auto",ddl);
          setProperty("hibernate.connection.useUnicode","true");
          setProperty("spring.jpa.hibernate.ddl-auto",ddl);
          setProperty("hibernate.dialect", dialect);
          setProperty("spring.jpa.properties.hibernate.dialect",dialect);
          setProperty("hibernate.globally_quoted_identifiers", "true");
          setProperty("hibernate.connection.CharSet", "utf8mb4");
          setProperty("hibernate.connection.characterEncoding", "utf8");

       }
    };
 }
}
当我尝试运行此命令时,会出现以下错误:

Caused by: org.hibernate.MappingException: component property not found: id
at org.hibernate.mapping.Component.getProperty(Component.java:335) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2779) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:2620) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.bindComponent(AnnotationBinder.java:2568) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2197) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:972) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:799) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) ~[hibernate-core-5.3.2.Final.jar:5.3.2.Final]
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:511) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:495) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 32 common frames omitted

检查这个。。。看来similar@FarazDurrani是的,我已经尝试过这种方法,但现在仍然有效。你为什么要这样做?在SpringBoot中,配置
spring.datasource
属性将自动配置就绪的数据源?@Strelok实际上我正在使用spring-dispatcher.xml从较旧的spring版本迁移,因此我需要创建sessionFactory和数据源bean,这些bean将在代码中进一步使用。但是如果您“使用”SpringBoot它将为您自动配置。