简体   繁体   English

Hibernate无法确定列的类型

[英]Hibernate Could not determine type for column

I had problem with simple One to One mapping in hibernate. 我在休眠中使用简单的一对一映射时遇到问题。 I had Two simple classes: 我有两个简单的类:

First Entity: 第一实体:

@Entity
public class GrantingOfLoanData {

    @Id
    @GeneratedValue
    private Long id;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="loan", referencedColumnName="loan_id")
    public Loan getLoan() {
        return loan;
    }
    private Loan loan;
}

Second Entity: 第二实体:

@Entity
public class Loan {

    @Id
    @GeneratedValue
    @Column(name="loan_id")
    private Long loan_id;

    public Long getLoan_id() {
        return loan_id;
    }
}

When I trying to run this code with hibernate I got followed exception: 当我尝试使用休眠模式运行此代码时,出现以下异常:

Caused by: org.hibernate.MappingException: Could not determine type for: org.finance.app.core.domain.common.loan.Loan, at table: GrantingOfLoanData, for columns:     [org.hibernate.mapping.Column(loan)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:310) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.mapping.Property.isValid(Property.java:241) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1358) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849) ~[hibernate-core-4.3.5.Final.jar:4.3.5.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ~[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]

I am using Spring version 4.0.5 and trying to be xml-less so as I understood the LocalContainerEntityManagerFactoryBean now supports a 'packagesToScan' property where the packages to scan for @Entity classes can be specified. 我使用的是Spring 4.0.5版,并且尝试不使用xml,因此据我所知,LocalContainerEntityManagerFactoryBean现在支持'packagesToScan'属性,可以在其中指定要扫描@Entity类的软件包。

my @Configuration JPA class looks as follow: 我的@Configuration JPA类如下所示:

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-pgsql.properties" })
@ComponentScan({ "org.finance.app" })
public class PersistenceJPAConfig {

@Autowired
private Environment env;

public PersistenceJPAConfig() {
    super();
}

// beans

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
    final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "org.finance.app" });

    final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));

    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    return hibernateProperties;
   }
}

Hibernate doesn't allow you to mix and match annotation in conjunction with field / getter. Hibernate不允许您将注释与field / getter混合使用。

If your @Id annotation is set over a field , all your mappings should follow fields . 如果在field上设置了@Id批注,则所有映射都应遵循fields

Try moving your @OneToOne to field rather than on setter like below 尝试将@OneToOne移至field而不是像下面的setter

 @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="loan", referencedColumnName="loan_id")
    private Loan loan;

 public Loan getLoan() {
        return loan;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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