Reputation: 915
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.
my @Configuration JPA class looks as follow:
@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;
}
}
Upvotes: 0
Views: 5568
Reputation: 2732
Hibernate doesn't allow you to mix and match annotation in conjunction with field / getter.
If your @Id
annotation is set over a field
, all your mappings should follow fields
.
Try moving your @OneToOne
to field
rather than on setter
like below
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="loan", referencedColumnName="loan_id")
private Loan loan;
public Loan getLoan() {
return loan;
}
Upvotes: 8