Reputation: 55
I'm getting the following exception when I run the app
Could someone advise what might be the issues?
Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509)
at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:503)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in ru.javamentor.ecommerce.service.impl.ReadWriteServiceImpl required a single bean, but 4 were found:
- productCategoryDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\ProductCategoryDaoImpl.class]
- productDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\ProductDaoImpl.class]
- roleDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\RoleDaoImpl.class]
- userDaoImpl: defined in file [D:\Project\ecommerce\target\classes\ru\javamentor\ecommerce\dao\impl\UserDaoImpl.class]
ReadWriteServiceImpl class:
@Service
public class ReadWriteServiceImpl<T, PK> implements ReadWriteService<T, PK> { private final ReadWriteDao<T, PK> readWriteDao;
@Autowired
public ReadWriteServiceImpl(ReadWriteDao<T, PK> readWriteDao) {
this.readWriteDao = readWriteDao;
}
@Override
@Transactional
public void persist(T t) {
readWriteDao.persist(t);
}
@Override
@Transactional
public void update(T t) {
readWriteDao.update(t);
}
@Override
@Transactional
public void delete(T t) {
readWriteDao.delete(t);
}
@Override
public boolean existsById(PK id) {
return readWriteDao.existsById(id);
}
@Override
public T getByKey(PK id) {
return readWriteDao.getByKey(id);
}
@Override
public List<T> getAll() {
return readWriteDao.getAll();
}
}
Upvotes: 0
Views: 3010
Reputation: 724
It showed me the error because I was writing wrong on the path
@GetMapping(myEndpoint+ "/${id}")
I should have written:
@GetMapping(myEndpoint+ "/{id}")
Upvotes: -1
Reputation: 42541
In this case the reason for exception is clear:
There are 4 candidates for injection to ReadWriteServiceImpl
and spring doesn't know which of the beans to inject:
public ReadWriteServiceImpl(ReadWriteDao<T, PK> readWriteDao) {
this.readWriteDao = readWriteDao;
}
There are 4 candidates - implementations of ReadWriteDao
and spring lists them all in the exception...
There are three ways you can go with in this case:
Use Concrete type (or create an interface for that concrete implementation and inject by that interface so that spring won't be confused)
Use @Qualifier
annotation as a hint to spring framework what to inject
Use @Primary
annotation if you think that one repository should be used "by default" to resolve confusions like this.
Upvotes: 3