Spring table relationship problems - spring

I have two entities Product and Image, the image has a Product foreign key.
When i try to run the code i get the following error
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'mainController': Unsatisfied dependency
expressed through field 'imageRepository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'imageRepository': Invocation of init method
failed; nested exception is
org.springframework.data.mapping.PropertyReferenceException: No
property product found for type Image! at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
~[spring-context-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
~[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
[spring-boot-1.5.7.RELEASE.jar:1.5.7.RELEASE] at
model.Application.main(Application.java:10) [classes/:na] Caused by:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'imageRepository': Invocation of init method
failed; nested exception is
org.springframework.data.mapping.PropertyReferenceException: No
property product found for type Image! at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] ... 19 common
frames omitted Caused by:
org.springframework.data.mapping.PropertyReferenceException: No
property product found for type Image! at
org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.Part.(Part.java:76)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree.(PartTree.java:89)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:64)
~[spring-data-jpa-1.11.7.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
~[spring-data-jpa-1.11.7.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214)
~[spring-data-jpa-1.11.7.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77)
~[spring-data-jpa-1.11.7.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:436)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
~[spring-data-commons-1.13.7.RELEASE.jar:na] at
org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101)
~[spring-data-jpa-1.11.7.RELEASE.jar:na] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE] ... 29 common
frames omitted
Product entity
#Entity
#Table(name = "product")
public class Product {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private Integer parent_product_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getParent_product_id() {
return parent_product_id;
}
#OneToMany
#JoinColumn(name = "product_id")
public void setParent_product_id(Integer parent_product_id) {
this.parent_product_id = parent_product_id;
}
}
Image Entity
#Entity
public class Image {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String type;
#ManyToOne
private Product product_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Product getParent_product_id() {
return product_id;
}
public void setProduct_id(Product product_id) {
this.product_id = product_id;
}
}
My ImageRepository
package model;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface ImageRepository extends CrudRepository<Image, Long> {
List<Image> findByProduct_id(String id);
}
Thanks for the help guys!

Your repository should be:
public interface ImageRepository extends CrudRepository<Image, Long> {
List<Image> findByProductId(Integer id);
}
Your Product Entity should be:
#Entity
#Table(name = "product")
public class Product {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
#Transient
private Integer parentId;
#ManyToOne
#JoinColumn(name="parent_id")
private Product parentProduct;
//getters/setters
}
And your Image Entity should be:
#Entity
#Table(name = "image")
public class Image {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String type;
#ManyToOne
#JoinColumn(name = "product_id")
private Product product;
//getters/setters
}

Please change the name of the product_id to product and change the String type from the repository to Product !
Also try this query :
#Query("select * from Image image where image.product=:product")
List<Image> findByProduct(#Param("product") Product product);

Related

Adding custom behavior to all repositories with annotations

I am trying to add custom behavior to all repositories in my spring boot application only with annotations. Official documentation deals with JPA and not for MongoDB.
I have not created any custom methods yet as I want to check whether the default is working fine.
My code is as follows:
src/main/java/com/test/shipper/database:
MongoConfig.java
src/main/java/com/test/shipper/modal:
Shipper.java
src/main/java/com/test/shipper/repository:
BaseRepositoryFactoryBean.java
BaseRepositoryImpl.java
BaseRepository.java
ShipperRepository.java
src/main/java/com/test/shipper/service:
ShipperService.java
ShipperService.java
public class ShipperService {
#Autowired
private ShipperRepository repository;
public Shipper createShipper(Shipper shipper){
repository.deleteAll();
Shipper shipper1 = new Shipper("123","shipper123");
repository.insert(shipper1);
Shipper shipper2 = new Shipper("234","shipper234");
repository.insert(shipper2);
System.out.println("Find All!!");
List shippers = repository.findAll();
for (Shipper car: shippers) {System.out.println(car);}
repository.delete(shipper2.getShipperId());
shippers = repository.findAll();
for (Shipper car: shippers) {
System.out.println(car);
}
return null;
}
}
MongoConfig.java
#Configuration
#EnableMongoRepositories(basePackages = "com.test.shipper",
repositoryFactoryBeanClass = com.test.shipper.repository.BaseRepositoryFactoryBean.class)
public class MongoConfig extends AbstractMongoConfiguration {
#Value("test")
private String dbName;
#Value("127.0.0.1")
private String host;
#Value("27017")
private int port;
#Override
protected String getDatabaseName() {
return dbName;
}
#Bean
#Override
public Mongo mongo() throws Exception {
return new MongoClient(host, port);
}
#Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(), dbName);
}
}
BaseRepository.java
#NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable>
extends MongoRepository<T, ID>
{
}
BaseRepositoryImpl.java
#NoRepositoryBean
public class BaseRepositoryImpl
extends SimpleMongoRepository implements BaseRepository
{
private MongoOperations mongoOperations;
public BaseRepositoryImpl(MongoEntityInformation metadata,
MongoOperations mongoOperations) {
super(metadata, mongoOperations);
this.mongoOperations = mongoOperations;
}
}
BaseRepositoryFactoryBean.java
public class BaseRepositoryFactoryBean, T, I extends Serializable>
extends MongoRepositoryFactoryBean {
public BaseRepositoryFactoryBean(Class repositoryInterface) {
super(repositoryInterface);
}
#Override
protected RepositoryFactorySupport getFactoryInstance(
MongoOperations operations) {
return new BaseMongoRepositoryFactory( operations );
}
private static class BaseMongoRepositoryFactory
extends MongoRepositoryFactory {
private MongoOperations mongo;
public BaseMongoRepositoryFactory(MongoOperations mongoOperations) {
super(mongoOperations);
this.mongo = mongoOperations;
}
#SuppressWarnings("unchecked")
protected Object getTargetRepository(RepositoryMetadata metadata) {
TypeInformation information = ClassTypeInformation.from((Class)metadata.getDomainType());
MongoPersistentEntity pe = new BasicMongoPersistentEntity(information);
MongoEntityInformation mongometa =
new MappingMongoEntityInformation(pe);
return new BaseRepositoryImpl( mongometa, mongo);
}
protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepository.class;
}
}
}
ShipperRepository.java
#Repository
public interface ShipperRepository
extends BaseRepository{
}
Shipper.java
#Document(collection = "shipper")
public class Shipper{
#Id
private String shipperId;
private String shipperName;
public Shipper() {
}
public Shipper(String shipperId, String shipperName) {
this.shipperId = shipperId;
this.shipperName = shipperName;
}
public String getShipperId() {
return shipperId;
}
public void setShipperId(String shipperId) {
this.shipperId = shipperId;
}
public String getShipperName() {
return shipperName;
}
public void setShipperName(String shipperName) {
this.shipperName = shipperName;
}
}
When the application starts I am getting the following exception.
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-06 23:08:26.849 ERROR 12828 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shipperService': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shipperRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No suitable constructor found on interface com.test.shipper.repository.BaseRepository to match the given arguments: [class org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation, class org.springframework.data.mongodb.core.MongoTemplate]. Make sure you implement a constructor taking these
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar!/:1.5.6.RELEASE]
at com.test.shipper.ShipperApplication.main(ShipperApplication.java:12) [classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [shipper_microservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [shipper_microservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [shipper_microservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [shipper_microservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shipperRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No suitable constructor found on interface com.test.shipper.repository.BaseRepository to match the given arguments: [class org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation, class org.springframework.data.mongodb.core.MongoTemplate]. Make sure you implement a constructor taking these
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
... 27 common frames omitted
Caused by: java.lang.IllegalStateException: No suitable constructor found on interface com.test.shipper.repository.BaseRepository to match the given arguments: [class org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation, class org.springframework.data.mongodb.core.MongoTemplate]. Make sure you implement a constructor taking these
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:360) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:98) ~[spring-data-mongodb-1.10.6.RELEASE.jar!/:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:199) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.6.RELEASE.jar!/:na]
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:117) ~[spring-data-mongodb-1.10.6.RELEASE.jar!/:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
... 37 common frames omitted
</code></pre>
What could be the issue?

Exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name

I'm not able to run my application Spring Boot JPA Spring Data Service Rest
Client Class include in bnp.bmci.entities package
#Entity
public class Client implements Serializable{
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long codeClient;
private String nomClient;
#OneToMany(mappedBy="client",fetch=FetchType.LAZY)
private Collection<Compte> comptes;
public Long getCodeClient() {
return codeClient;
}
public void setCodeClient(Long codeClient) {
this.codeClient = codeClient;
}
public String getNomClient() {
return nomClient;
}
public void setNomClient(String nomClient) {
this.nomClient = nomClient;
}
public Collection<Compte> getComptes() {
return comptes;
}
public void setComptes(Collection<Compte> comptes) {
this.comptes = comptes;
}
public Client() {
super();
// TODO Auto-generated constructor stub
}
public Client(Long codeClient, String nomClient, Collection<Compte> comptes) {
super();
this.codeClient = codeClient;
this.nomClient = nomClient;
this.comptes = comptes;
}
#Override
public String toString() {
return "Client [codeClient=" + codeClient + ", nomClient=" + nomClient + ", comptes=" + comptes + "]";
}
public Client(Long codeClient, String nomClient) {
super();
this.codeClient = codeClient;
this.nomClient = nomClient;
}
}
Repository include in bnp.bmci.dao package
public interface ClientRepository extends JpaRepository<Client, Long>{
}
ClientMetier interface
package bnp.bmci.metier;
import java.util.List;
import bnp.bmci.entities.Client;
public interface ClientMetier {
public Client saveClient(Client c);
public List<Client> listClient();
}
ClientMetierImpl class
package bnp.bmci.metier;
#Service
public class ClientMetierImpl implements ClientMetier{
#Autowired
private ClientRepository clientRepository;
#Override
public Client saveClient(Client c) {
return clientRepository.save(c);
}
#Override
public List<Client> listClient() {
return clientRepository.findAll();
}
}
And finally ClientRestService in bnp.bmci.service package
#RestController
public class ClientRestService {
#Autowired
private ClientMetier clientMetier;
#RequestMapping(value="/clients", method=RequestMethod.POST)
public Client saveClient(#RequestBody Client c) {
return clientMetier.saveClient(c);
}
#RequestMapping(value="/clients", method=RequestMethod.GET)
public List<Client> listClient() {
return clientMetier.listClient();
}
}
I can't fixe a Spring "unsatisfied dependency" error message
Below is the error which I am getting
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2016-11-14 22:11:46.472 ERROR 9496 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientMetierImpl': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Client!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at bnp.bmci.BanqueApplication.main(BanqueApplication.java:10) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Client!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 19 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type Client!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.neo4j.repository.query.derived.DerivedGraphRepositoryQuery.<init>(DerivedGraphRepositoryQuery.java:47) ~[spring-data-neo4j-4.1.3.RELEASE.jar:na]
at org.springframework.data.neo4j.repository.query.GraphQueryMethod.createQuery(GraphQueryMethod.java:95) ~[spring-data-neo4j-4.1.3.RELEASE.jar:na]
at org.springframework.data.neo4j.repository.query.GraphQueryLookupStrategy.resolveQuery(GraphQueryLookupStrategy.java:45) ~[spring-data-neo4j-4.1.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:435) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) ~[spring-data-commons-1.12.3.RELEASE.jar:na]
at org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean.afterPropertiesSet(GraphRepositoryFactoryBean.java:43) ~[spring-data-neo4j-4.1.3.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 29 common frames omitted
Your ClientRepository should have #Repository and you should have enabled
#EnableJpaRepositories(basePackages = {"xxx.xxx.xxx.yyy"}) in your configuration
#Repository
public interface ClientRepository extends JpaRepository<Client, Long>{
}
You have missed to mark #Repository for your class:
#Repository
public interface ClientRepository extends JpaRepository<Client, Long>{
}

Injecting Request scope bean through constructor using annotation or Java config without xml config

I created a sample Spring boot rest app with the following controller, service and bean code.
#RestController
public class SampleController {
#Autowired
SampleService sampleService;
#Autowired
User user;
#ResponseBody
#GetMapping(value="/user/add")
public String addUser(User user) {
return sampleService.getUserName();
}
}
#Component
public class SampleService {
private User user;
public User getUser() {
return user;
}
#Required
public void setUser(User user) {
this.user = user;
}
public String getUserName() {
return "user name is "+this.getUser();
}
}
#RequestScope
#Component
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
#Configuration
public class RequestBean {
#Bean
#Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public User initUser() {
User user = new User();
return user;
}
}
When I start the spring boot app, I am getting this below error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sampleController': Unsatisfied dependency expressed through field 'sampleService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleService' defined in file [<path>\samplespringrest\target\classes\com\example\SampleService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'user' is required for bean 'sampleService'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
at com.example.SamplespringrestApplication.main(SamplespringrestApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_65]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_65]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.4.1.RELEASE.jar:1.4.1.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleService' defined in file [<path>/SampleService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'user' is required for bean 'sampleService'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'user' is required for bean 'sampleService'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
... 33 common frames omitted
Please help me to solve this.
Thanks in advance.
Update
After I update my service class like the one below
#Component
public class SampleService {
#Autowired
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getUserName() {
return "user name is "+this.getUser().getUsername();
}
}
I am getting response as "user name is null"
Actually you have restricted lifecycle of User bean by request scope and at the same time using #Required for its setter in SampleService. So you haven't got any request at application starting time -> user bean didn't create -> required annotation thrown Exception. It's clear from #RequestScope annotation docs:
{#code #RequestScope} is a specialization of {#link Scope #Scope} for
a * component whose lifecycle is bound to the current web request.
So you configuration provoked this exception.

org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit

I am trying to add a custom implementation (FruitRepositoryCustomImpl) for a JPA repository (FruitRepository). Here is my code -
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import javax.persistence.*;
interface FruitRepository extends JpaRepository<Fruit, Long>, FruitRepositoryCustom {
}
interface FruitRepositoryCustom {
void detachItem(Fruit fruit);
}
#Repository
class FruitRepositoryCustomImpl implements FruitRepositoryCustom {
#PersistenceContext
private EntityManager entityManager;
#Override
public void detachItem(Fruit fruit) {
entityManager.detach(fruit);
}
}
#SpringBootApplication
public class EatFruitApplication {
public static void main(String[] args) {
SpringApplication.run(EatFruitApplication.class, args);
}
}
and Entity
#Entity
class Fruit {
#Id
#GeneratedValue
private Long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
as well as
#RestController
#RequestMapping("/fruits")
class FruitRestController {
#Autowired
private FruitRepository fruitRepository;
#RequestMapping(value = "/{fruitId}", method = RequestMethod.GET)
public Fruit readItem(#PathVariable Long fruitId) {
Fruit fruit = this.fruitRepository.findOne(fruitId);
return fruit;
}
#RequestMapping(method = RequestMethod.POST)
public Fruit addItem(#RequestBody Fruit fruit) {
fruitRepository.save(fruit);
return fruit;
}
}
But I am getting following error while running the application.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRestController': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eat.fruit.FruitRepository eat.fruit.FruitRestController.fruitRepository;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed;
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit! at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] at
eat.fruit.EatFruitApplication.main(EatFruitApplication.java:36) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92] at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92] at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92] at
java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92] at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na] Caused by:
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private eat.fruit.FruitRepository
eat.fruit.FruitRestController.fruitRepository; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit! at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] ... 22 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fruitRepository': Invocation of init method failed; nested exception is
org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit! at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] ... 24 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property detachItem found for type Fruit! at
org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:62) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] at
org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237) ~[spring-data-commons-1.11.4.RELEASE.jar:na] at
org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.9.4.RELEASE.jar:na] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] ... 34 common frames omitted
Please help.
Thanks.
Spring's handling of the #PersistenceContext annotation does almost exactly what you're after, with one big difference: you always get a transaction scoped EntityManager and Spring injects always the same instance for the same thread, so you have kind of propagation and don't have to worry about thread-safety. But you'll never get an extended context this way!
Believe me, Spring 3 and extended persistence context don't play well together, maybe this will change in Spring 3.1 but I'm afraid that's not in their focus. If you want to use an extended persistence context let Spring inject the EntityManagerFactory (via #PersistenceUnit annotation) and then create the EntityManager on your own. For propagation you'll have to either pass the instance as a parameter or store it in a ThreadLocal yourself.
try reading this link: http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
Example you can go through
public class SomeClient {
#Autowired
private PersonRepository repository;
public void doSomething() {
List<Person> persons = repository.findByLastname("Matthews");
}
}

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table

Thia has eaten up my weekend I searched about it but their might be several problems that might be causing it, hope some one will help me out.
I am trying to create a many to many relationship via one to many and many to one relationship using third table but I am getting this error:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,134 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-7) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_95]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_95]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_95]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:286) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
... 20 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,135 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/]] (MSC service thread 1-7) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) [spring-context-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_95]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_95]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_95]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:286) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310) [spring-orm-3.2.0.RELEASE.jar:3.2.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
... 20 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: PoojaFirstCategory, for columns: [org.hibernate.mapping.Column(poojaPanditRelationShip)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:314) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:292) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.Property.isValid(Property.java:239) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:483) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:270) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.validate(Configuration.java:1327) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1789) [hibernate-core-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [hibernate-entitymanager-4.2.8.Final.jar:4.2.8.Final]
... 26 more
2016/04/03 09:12:14,540 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-7) Error listenerStart
2016/04/03 09:12:14,541 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-7) Context [] startup failed due to previous errors
My entities are :
#Entity
public class PoojaFirstCategory {
private Long poojaFirstCategoryId;
private String name;
private String brief;
private Set<PoojaPanditRelationShip> poojaPanditRelationShip;
public PoojaFirstCategory() {
}
public PoojaFirstCategory(String name,String brief) {
this.name = name;
this.brief = brief;
poojaPanditRelationShip = new HashSet<PoojaPanditRelationShip>();
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getPoojaFirstCategoryId() {
return poojaFirstCategoryId;
}
public void setPoojaFirstCategoryId(Long poojaFirstCategoryId) {
this.poojaFirstCategoryId = poojaFirstCategoryId;
}
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
#OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrief() {
return brief;
}
public void setBrief(String brief) {
this.brief = brief;
}
}
#Entity
public class PoojaSecondCategory {
private Long poojaSecondCategoryId;
private Long TId;
private String poojaName;
private String name;
private Integer experince;
private byte[] photo;
private Integer rate;
public PoojaSecondCategory() {
}
public PoojaSecondCategory(String poojaName,String name, Integer experince, Integer rate) {
this.poojaName = poojaName;
this.name = name;
this.experince = experince;
this.rate = rate;
}
private Set<PoojaPanditRelationShip> poojaPanditRelationShip;
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
#OneToMany(mappedBy = "poojaSecondCategory" ,cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
public String getPoojaName() {
return poojaName;
}
public void setPoojaName(String poojaName) {
this.poojaName = poojaName;
}
public Long getTId() {
return TId;
}
public void setTId(Long tId) {
TId = tId;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getPoojaSecondCategoryId() {
return poojaSecondCategoryId;
}
public void setPoojaSecondCategoryId(Long poojaSecondCategoryId) {
this.poojaSecondCategoryId = poojaSecondCategoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getExperince() {
return experince;
}
public void setExperince(Integer experince) {
this.experince = experince;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public Integer getRate() {
return rate;
}
public void setRate(Integer rate) {
this.rate = rate;
}
}
and my relationship entity is :
#Entity
public class PoojaPanditRelationShip implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private PoojaFirstCategory poojaFirstCategory;
private PoojaSecondCategory poojaSecondCategory;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Id
#ManyToOne
#JoinColumn(name = "poojaFirstCategoryId")
public PoojaFirstCategory getPoojaFirstCategory() {
return poojaFirstCategory;
}
public void setPoojaFirstCategory(PoojaFirstCategory poojaFirstCategory) {
this.poojaFirstCategory = poojaFirstCategory;
}
#Id
#ManyToOne
#JoinColumn(name = "poojaSecondCategoryId")
public PoojaSecondCategory getPoojaSecondCategory() {
return poojaSecondCategory;
}
public void setPoojaSecondCategory(PoojaSecondCategory poojaSecondCategory) {
this.poojaSecondCategory = poojaSecondCategory;
}
}
and here I am trying to populate some dummy data:
#Component
public class StartOnStartUp implements ApplicationListener<ContextRefreshedEvent> {
#Autowired
private TouristFirstCategoryRepository touristFirstCategoryRepository;
#Autowired
private TouristSecondCategoryRepository touristSecondCategoryRepository;
#Autowired
private PoojaFirstCategoryRepository poojaFirstCategoryRepository;
#Autowired
private PoojaSecondCategoryRepository poojaSecondCategoryRepository;
#Override
#Transactional
public void onApplicationEvent(ContextRefreshedEvent arg0) {
PoojaFirstCategory poojaFirstCategory = new PoojaFirstCategory("StayaNarayan","SatayNarayan Ki pooja");
PoojaSecondCategory poojaSecondCategory = new PoojaSecondCategory("StayaNarayan","Pandit Brij Bhushan",23,1000);
PoojaPanditRelationShip poojaPanditRelationShip = new PoojaPanditRelationShip();
poojaPanditRelationShip.setPoojaFirstCategory(poojaFirstCategory);
poojaPanditRelationShip.setPoojaSecondCategory(poojaSecondCategory);
poojaFirstCategory.getPoojaPanditRelationShip().add(poojaPanditRelationShip);
poojaSecondCategoryRepository.save(poojaSecondCategory);
poojaFirstCategoryRepository.save(poojaFirstCategory);
}
I am also using hibernate property to create table based on entity classes
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<!-- spring based scanning for entity classes -->
<property name="packagesToScan" value="first.varanasi.erp" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
You should put annotation mapping on getter (or field), not setter. So instead of this:
#OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public void setPoojaPanditRelationShip(Set<PoojaPanditRelationShip> poojaPanditRelationShip) {
this.poojaPanditRelationShip = poojaPanditRelationShip;
}
You should use this:
#OneToMany(mappedBy = "poojaFirstCategory", cascade = CascadeType.ALL)
public Set<PoojaPanditRelationShip> getPoojaPanditRelationShip() {
return poojaPanditRelationShip;
}
HTH.

Resources