@OneToOne或@ManyToOne引用未知实体

时间:2019-01-30 04:44:06

标签: java hibernate

简单方案:

  • AdmisRejet可以链接到Admis或PreCandidat。
  • Admis和PreCandidat可以有许多AdmisRejet。

当我只有Admis和AdmisRejet时,一切都在工作。

但是随后我添加了PreCandidat(与Admis相同的方式),出现了休眠错误。

找不到原因...

模型(它们都有getter,setter和id):

// package nc.unc.importparcoursup.dao.admisDAO
@Entity
public class AdmisRejet {
    @ManyToOne
    @JoinColumn(name = "ADMIS_ID")
    private Admis admis;

    @ManyToOne
    @JoinColumn(name = "CAND_KEY")
    private PreCandidat preCandidatRejet;

// package nc.unc.importparcoursup.dao.admisDAO
@Entity
public class Admis {
    @OneToMany(mappedBy = "admis", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<AdmisRejet> admisRejet;

// package nc.unc.importparcoursup.dao.pgiCocktailDAO.entity
@Entity
public class PreCandidat {
    @OneToMany(mappedBy = "preCandidatRejet", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<AdmisRejet> admisRejet;

dao配置:

package nc.unc.importparcoursup.dao.admisDAO;


@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "parcoursupEntityManagerFactory", transactionManagerRef = "parcoursupTransactionManager")
public class ParcoursupDAOConfig {

    @Value("${datasource.parcoursup-admis.hibernate-hbm2ddl-auto}")
    private String ddlMode;

    @Value("${datasource.parcoursup-admis.jdbc.driverClassName:#{null}}")
    private String driver;


    @Bean
    public PlatformTransactionManager parcoursupTransactionManager() {
        return new JpaTransactionManager(Objects.requireNonNull(parcoursupEntityManagerFactory().getObject()));
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean parcoursupEntityManagerFactory() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();

        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", ddlMode);
        properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL94Dialect");

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

        factoryBean.setDataSource(parcoursupDataSource());
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        factoryBean.setPackagesToScan(ParcoursupDAOConfig.class.getPackage().getName());
        factoryBean.setJpaPropertyMap(properties);

        return factoryBean;
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.parcoursup-admis")
    public DataSource parcoursupDataSource() {
        if (driver != null) {
            return DataSourceBuilder.create()
                    .driverClassName(driver)
                    .build();
        }
        return DataSourceBuilder.create()
                .build();
    }
}

错误:

  

由以下原因引起:org.hibernate.AnnotationException:nc.unc.importparcoursup.dao.admisDAO.AdmisRejet.preCandidatRejet上的@OneToOne或@ManyToOne引用了未知实体:nc.unc.importparcoursup.dao.pgiCocktailDAO.entity.PreCandidat < / p>

2 个答案:

答案 0 :(得分:1)

好吧,这是因为您告诉factoryBean您的实体在factoryBean.setPackagesToScan(ParcoursupDAOConfig.class.getPackage().getName());包中,即nc.unc.importparcoursup.dao.admisDAO中。并且PreCandidatnc.unc.importparcoursup.dao.pgiCocktailDAO.entity包中...

尝试factoryBean.setPackagesToScan("nc.unc.importparcoursup.dao")或将您的班级配置移到更高的级别。

答案 1 :(得分:0)

我认为您可能会忘记将注释@Table(name =“ table_name”)添加到实体。