【问题标题】:JPA, spring-boot, configuring entity Manager with old not annotated classesJPA,spring-boot,使用旧的未注释类配置实体管理器
【发布时间】:2017-06-22 15:37:16
【问题描述】:

我有一个使用最新技术的全新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>

【问题讨论】:

  • 你的 SomeEntity 中有一些映射,它不知道休眠。
  • 所以有些类没有注释,那么orm.xml中是否提到过?
  • 您应该提供您的old.entity1.SomeOldEntity,因为这似乎是问题的根源......不一定是您的 Hibernate 配置。
  • 对于一个全新的 Spring Boot 应用程序,您正在尝试自己配置而不是使用 Spring Boot。这是为什么呢?
  • 嗯,你是什么意思,我必须以某种方式传递存储在其他 15 年以上的项目中的预定义实体。

标签: spring hibernate jpa


【解决方案1】:

好的,我想我找到了解决方案,旧的未注释类总是有一个 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 });

它工作!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2014-09-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 2017-02-11
    相关资源
    最近更新 更多