简体   繁体   中英

JPA, spring-boot, configuring entity Manager with old not annotated classes

I have a brand-new Spring-Boot project using the most recent technologies, just one configuration file application.yml, otherwise I configured all via annotations.

I just have a dependency to some old not annotated entity classes, and when I start the project it crashes because of:

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

This is the JPA configuration class:

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

}

EDIT 22.06 here the xml mapping 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>

Ok, I think I found the solution, old not Annotated classes has always a xml mapping the class to database entities, well than if I pass the xml like this:

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

Instead of this:

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

IT WORKS!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM