Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Run time error for DALI generated entity with compound keys( Generated entity does not work with EntityManagerFactory (with EclipseLinkJpaVendorAdapter) for compound primary key)
Run time error for DALI generated entity with compound keys [message #1219999] Sun, 08 December 2013 21:47
Artem Belyaev is currently offline Artem BelyaevFriend
Messages: 2
Registered: December 2013
Junior Member
Hello all.
PLease help me understand me what is wrong.

I use auto generated entity from mysql db by spring-tool-suite->eclipselink with DALI installed
For tables with compound key it generate embedded class and use it as a 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;


also I use EclipseLinkJpaVendorAdapter for LocalContainerEntityManagerFactoryBean
and when factory begin generating repository for this entity,
at the moment
	@Autowired
	CustomerRepository customerRepository;


I got an error

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@768981123: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@92917455: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].


But when i remove compound key class, and use single variable like

@Entity
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
	private static final long serialVersionUID = 1L;

    @id
    private String id;


It work good.

Here is my simple code for check this situationsituation
Customet.java
@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;

	public Customer() {
	}

	public CustomerPK getId() {
		return this.id;
	}
	public void setId(CustomerPK id) {
		this.id = id;
	}
	public Department getDepartment() {
		return this.department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

}

-------------- CustomerPK.java ------------
@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;

 	. . .

}

Department.java
@Entity
@NamedQuery(name="Department.findAll", query="SELECT d FROM Department d")
public class Department implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	private String id;
	. . . 
}


CustomerRepository.java
import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, CustomerPK> {
}


fileScanner.java
public class fileScanner {
	@Autowired
	CustomerRepository customerRepository;
	. . . 
}


Spring config
@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
public class Config {

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

	@Bean
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager txManager = new JpaTransactionManager();
		txManager.setEntityManagerFactory(entityManagerFactory().getObject());
		return txManager;
	}	
	


pom.xml
<dependencies>

        <!-- Spring Data JPA -->
 		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>1.4.2.RELEASE</version>
		</dependency>  
		     				
 		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
 
 
 		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
 
        		
		<!-- EclipseLink -->
		<dependency>
		   <groupId>org.eclipse.persistence</groupId>
		   <artifactId>eclipselink</artifactId>
		   <artifactId>org.eclipse.persistence.jpa</artifactId>
		   <version>2.5.2-M1</version>		   
		   <scope>compile</scope>
			<exclusions>
				<exclusion>
					<groupId>org.eclipse.persistence</groupId>
					<artifactId>commonj.sdo</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		<dependency>
			<groupId>org.eclipse.persistence</groupId>
			<artifactId>javax.persistence</artifactId>
			<version>2.1.0</version>
			<!-- <version>2.0.5</version> -->
		</dependency>				
      . . . 

	</dependencies>	




The 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

[Updated on: Mon, 09 December 2013 08:33]

Report message to a moderator

Previous Topic:(Composite) persistence.xml: NamedQuery of name XYZ not found.
Next Topic:using UNION with ORDER BY clause
Goto Forum:
  


Current Time: Fri Apr 26 14:18:47 GMT 2024

Powered by FUDForum. Page generated in 0.02911 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top