Hello
Please explain me what is wrong.
First of all I was generated "entity from tables" from mysql db.
Any table witch has many-to-one relations are generated with compound key.
IS IT CORRECTLY for JPA 2.1 ?
Does it need addition coding after generating?
After that i am create crudRepository class and beans factory ( i am show all code below) .
I use eclipselink engine.
But when i try to get repository bean
i got an error, as i understand the problem in that entity manager does not want use embedded class (compound key).
Please somebody give advice what is wrong ....
---- This is exception i receive at runtime.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scanner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: home.abel.PhotoHub.Domain.CustomerRepository home.abel.PhotoScan.fileScanner.customerRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class home.abel.PhotoHub.Domain.CustomerPK] on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@2008672252:Department [ javaType: class home.abel.PhotoHub.Domain.Department descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Department --> [DatabaseTable(DEPARTMENT)]), mappings: 3],org.eclipse.persistence.mappings.ManyToOneMapping[department]]] on the identifiable type [EntityTypeImpl@336474128:Customer [ javaType: class home.abel.PhotoHub.Domain.Customer descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Customer --> [DatabaseTable(CUSTOMER)]), mappings: 3]] but found attribute type [class home.abel.PhotoHub.Domain.Department].
Cusromer.java - The entity with compound key
@Entity
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CustomerPK id;
. . .
//bi-directional many-to-one association to Department
@ManyToOne
private Department department;
. . .
}
Department is a second entity not shown here
CustomerPK.java compound key class
@Embeddable
public class CustomerPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private String id;
@Column(name="department_id", insertable=false, updatable=false)
private String departmentId;
. . .
}
CustomerRepository.java - My repository class
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, CustomerPK> {
}
It seems to be correctly to. Because it too simply.
Entity factory
@Bean
EclipseLinkJpaVendorAdapter vendorAdapter() {
EclipseLinkJpaVendorAdapter va = new EclipseLinkJpaVendorAdapter();
va.setShowSql(true);
va.setDatabasePlatform("org.eclipse.persistence.platform.database.MySQLPlatform");
va.setGenerateDdl(false);
va.setDatabase(Database.MYSQL);
return va;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
Map<String, String> jpaProperties = new HashMap<String, String>();
jpaProperties.put("eclipselink.weaving", "static");
factory.setJpaPropertyMap(jpaProperties);
factory.setJpaVendorAdapter(vendorAdapter());
factory.setJpaDialect(new EclipseLinkJpaDialect());
factory.setPackagesToScan("home.abel.PhotoHub.Domain");
factory.setDataSource(dataSource());
return factory;
}
This is full stack trace
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scanner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: home.abel.PhotoHub.Domain.CustomerRepository home.abel.PhotoScan.fileScanner.customerRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class home.abel.PhotoHub.Domain.CustomerPK] on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@2008672252:Department [ javaType: class home.abel.PhotoHub.Domain.Department descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Department --> [DatabaseTable(DEPARTMENT)]), mappings: 3],org.eclipse.persistence.mappings.ManyToOneMapping[department]]] on the identifiable type [EntityTypeImpl@336474128:Customer [ javaType: class home.abel.PhotoHub.Domain.Customer descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Customer --> [DatabaseTable(CUSTOMER)]), mappings: 3]] but found attribute type [class home.abel.PhotoHub.Domain.Department].
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1181)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:679)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:582)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:299)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:779)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:768)
at home.abel.PhotoScan.Application.main(Application.java:29)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: home.abel.PhotoHub.Domain.CustomerRepository home.abel.PhotoScan.fileScanner.customerRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class home.abel.PhotoHub.Domain.CustomerPK] on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@2008672252:Department [ javaType: class home.abel.PhotoHub.Domain.Department descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Department --> [DatabaseTable(DEPARTMENT)]), mappings: 3],org.eclipse.persistence.mappings.ManyToOneMapping[department]]] on the identifiable type [EntityTypeImpl@336474128:Customer [ javaType: class home.abel.PhotoHub.Domain.Customer descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Customer --> [DatabaseTable(CUSTOMER)]), mappings: 3]] but found attribute type [class home.abel.PhotoHub.Domain.Department].
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Expected id attribute type [class home.abel.PhotoHub.Domain.CustomerPK] on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@2008672252:Department [ javaType: class home.abel.PhotoHub.Domain.Department descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Department --> [DatabaseTable(DEPARTMENT)]), mappings: 3],org.eclipse.persistence.mappings.ManyToOneMapping[department]]] on the identifiable type [EntityTypeImpl@336474128:Customer [ javaType: class home.abel.PhotoHub.Domain.Customer descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Customer --> [DatabaseTable(CUSTOMER)]), mappings: 3]] but found attribute type [class home.abel.PhotoHub.Domain.Department].
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:151)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1510)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:993)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:936)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:834)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 17 more
Caused by: java.lang.IllegalArgumentException: Expected id attribute type [class home.abel.PhotoHub.Domain.CustomerPK] on the existing id attribute [SingularAttributeImpl[EntityTypeImpl@2008672252:Department [ javaType: class home.abel.PhotoHub.Domain.Department descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Department --> [DatabaseTable(DEPARTMENT)]), mappings: 3],org.eclipse.persistence.mappings.ManyToOneMapping[department]]] on the identifiable type [EntityTypeImpl@336474128:Customer [ javaType: class home.abel.PhotoHub.Domain.Customer descriptor: RelationalDescriptor(home.abel.PhotoHub.Domain.Customer --> [DatabaseTable(CUSTOMER)]), mappings: 3]] but found attribute type [class home.abel.PhotoHub.Domain.Department].
at org.eclipse.persistence.internal.jpa.metamodel.IdentifiableTypeImpl.getId(IdentifiableTypeImpl.java:200)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation$IdMetadata.<init>(JpaMetamodelEntityInformation.java:211)
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:77)
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:65)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:146)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:84)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:67)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:144)
... 25 more