JPA,spring-boot,使用未注释的旧类

时间:2017-06-21 10:38:56

标签: spring hibernate jpa

我有一个全新的Spring-Boot项目,使用最新的技术,只有一个配置文件application.yml,否则我通过注释配置了所有。

我只是依赖于一些旧的没有注释的实体类,当我启动项目时它崩溃是因为:

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>

1 个答案:

答案 0 :(得分:0)

好吧,我认为我找到了解决方案,旧的没有Annotated类总是将类映射到数据库实体的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 });

IT工作!!!