2014-06-08 91 views
0

我在休眠中遇到了簡單的One to One映射問題。 我有兩個簡單的類:休眠無法確定列的類型

第一實體:

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

第二實體:

@Entity 
public class Loan { 

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

    public Long getLoan_id() { 
     return loan_id; 
    } 
} 

當我試圖運行此代碼與Hibernate我得到了遵循例外:

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] 

我正在使用Spring版本4.0.5,並試圖使xml更少LocalContainerEntityManagerFactoryBean現在支持'packagesToScan'屬性,可以指定掃描@Entity類的包。

我@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; 
    } 
} 
+0

你加貸款persistence.xml中? – fmodos

+0

我沒有僅通過@Configuration在類 – Mazeryt

+0

中通過xml使用配置檢查您是否使用正確的hibernate.dialect(對於pgsql)。粘貼persistence-pgsql.properties的內容。也粘貼表DDL – Espresso

回答

3

休眠不允許你在現場/吸氣一起混搭詮釋。

如果您的@Id註釋設置超過field,則所有映射都應遵循fields

請嘗試移動@OneToOnefield而不是setter像下面

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

public Loan getLoan() { 
     return loan; 
    } 
+1

它喚醒:)謝謝,我不知道一致性註釋的要求 – Mazeryt

+0

其okie..welcome。 –

+1

這絕對是正確答案! Hibernate期望一致性......看起來合理。謝謝!! –