2017-06-21 40 views
2

我有一个全新的Spring-Boot项目使用最新的技术,只有一个配置文件application.yml,否则我配置全部通过注释。JPA,spring-boot,配置实体管理器与旧的未注释的类

我只是有一个依赖于一些老不标注的实体类,当我开始这个项目它崩溃,因为:

Caused by: org.hibernate.boot.MappingException: Association [old.entity1.SomeOldEntity] references an unmapped entity [old.entity1.SomeOldEntity] : origin(old/entity1/SomeOldEntity.xml) 

这是JPA配置类:

@Configuration 
@EnableJpaRepositories(basePackages = "actual.repositories", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager") 
@EnableTransactionManagement 
public class JpaConfiguration { 

    @Autowired 
    private Environment environment; 

    @Value("${my.maxPoolSize:10}") 
    private int maxPoolSize; 

    /* 
    * Populate SpringBoot DataSourceProperties object directly from application.yml based on prefix.Thanks to .yml, Hierachical data is mapped out of the box 
    * with matching-name properties of DataSourceProperties object]. 
    */ 
    @Bean 
    @Primary 
    @ConfigurationProperties(prefix = "datasource.myApp") 
    public DataSourceProperties dataSourceProperties() { 
     return new DataSourceProperties(); 
    } 

    /* 
    * Configure HikariCP pooled DataSource. 
    */ 
    @Bean 
    public DataSource dataSource() { 
     DataSourceProperties dataSourceProperties = dataSourceProperties(); 
     HikariDataSource dataSource = (HikariDataSource) DataSourceBuilder.create(dataSourceProperties.getClassLoader()) 
       .driverClassName(dataSourceProperties.getDriverClassName()).url(dataSourceProperties.getUrl()).username(dataSourceProperties.getUsername()) 
       .password(dataSourceProperties.getPassword()).type(HikariDataSource.class).build(); 
     dataSource.setMaximumPoolSize(maxPoolSize); 
     return dataSource; 
    } 

    /* 
    * Entity Manager Factory setup. 
    */ 
    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException { 
     LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); 
     factoryBean.setDataSource(dataSource()); 
     factoryBean.setPackagesToScan(
       new String[] { "some.new.model", "old.entity1", "old.entity2" }); 
     factoryBean.setJpaVendorAdapter(jpaVendorAdapter()); 
     factoryBean.setJpaProperties(jpaProperties()); 
     return factoryBean; 
    } 

    /* 
    * Provider specific adapter. 
    */ 
    @Bean 
    public JpaVendorAdapter jpaVendorAdapter() { 
     HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); 
     return hibernateJpaVendorAdapter; 
    } 

    /* 
    * Here you can specify any provider specific properties. 
    */ 
    private Properties jpaProperties() { 
     Properties properties = new Properties(); 

     // Datasource option 
     String dialect = environment.getRequiredProperty("my.dialect"); 
     String method = environment.getRequiredProperty("my.method"); 
     String show_sql = environment.getRequiredProperty("my.show_sql"); 
     String format_sql = environment.getRequiredProperty("my.format_sql"); 
     String defaultSchema = environment.getRequiredProperty("my.schema"); 

     // Set options 
     properties.put("hibernate.dialect", dialect); 
     properties.put("hibernate.hbm2ddl.auto", method); 
     properties.put("hibernate.show_sql", show_sql); 
     properties.put("hibernate.format_sql", format_sql); 
     if (StringUtils.isNotEmpty(defaultSchema)) { 
      properties.put("hibernate.default_schema", defaultSchema); 
     } 
     return properties; 
    } 

    @Bean 
    @Autowired 
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 
     JpaTransactionManager txManager = new JpaTransactionManager(); 
     txManager.setEntityManagerFactory(emf); 
     return txManager; 
    } 

} 

编辑22.06 这里的xml映射SomeOldEntity:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping package="old.entity1"> 

    <class name="SomeOldEntity" table="entity1" lazy="false"> 

     <id name="id" column="ID" type="long" access="field"> 
      <generator class="assigned" /> 
     </id> 

     <property name="moreId" column="MORE_ID" type="long" access="field" /> 

     <property name="aPrefix" column="A_PREFIX" type="java.lang.String" access="field" /> 
     <property name="aKey" column="A_KEY" type="java.lang.String" access="field" /> 

     <property name="active" column="IS_ACTIVE" type="boolean" access="field" /> 

     <one-to-one name="SomeMoreEntity" access="field" fetch="join" /> 
     <one-to-one name="another1OldEntity" property-ref="someOtherId" access="field" fetch="join" /> 
     <one-to-one name="another2OldEntity" property-ref="someOtherId" access="field" fetch="join" /> 
     <one-to-one name="another3OldEntity" property-ref="someOtherId" access="field" fetch="join" /> 

     <list name="impressumList" access="field" lazy="true" fetch="select"> 
      <key column="FK_SOMEID" not-null="true" /> 
      <list-index column="INDEX_ORDER" /> 
      <one-to-many class="some.other.entity.outside.package.SomeEntity" /> 
     </list> 

    </class> 

</hibernate-mapping> 
+0

你在'SomeEntity'中有一些映射,它不会被hibernate知道。 –

+1

所以有些类没有注释,并在'orm.xml'中提到呢? –

+0

你应该提供你的'old.entity1.SomeOldEntity',因为这似乎是问题来自何处......不一定是你的Hibernate配置。 – alexanderific

回答

0

好吧,我想我找到了解决办法,旧的不去注解类具有总是一个XML映射类到数据库实体,以及比如果我通过这样的XML:

// Old Not Annotated class must be passed as xml to MappingResource 
String Entity1 = " old.Entity1.xml"; 
factoryBean.setMappingResources(Entity1); 

的这个代替:

// Annotated classes can be passed as entities throught setPackagesToScan 
String Entity1 = "old.Entity1"; 
factoryBean.setPackagesToScan(new String[] { Entity1 }); 

它工作!

相关问题