简单方案:


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

最佳答案

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

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

关于java - @OneToOne或@ManyToOne引用未知实体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54433479/

10-09 04:06