I'm trying to connect the database to my vaadin project using javaConfig. But when I try to get all the values ​​from any of the tables in the database, I get null.

enter image description here

The database is of course full. Interface HotelService and its implementation: HotelCategoryServiceImpl.java

package com.gptravel.service.category; import com.gptravel.dao.HotelCategoryRepository; import com.gptravel.entity.HotelCategory; import com.gptravel.service.hotel.HotelServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; import java.util.logging.Logger; @Service("hotelCategoryService") @Transactional public class HotelCategoryServiceImpl implements HotelCategoryService { private static final Logger LOGGER = Logger.getLogger(HotelServiceImpl.class.getName()); @PersistenceContext private EntityManager em; @Autowired private HotelCategoryRepository hotelCategoryRepo; @Override public HotelCategory create(HotelCategory hotelCategory) { HotelCategory savehotelCategory = hotelCategoryRepo.saveAndFlush(hotelCategory); return savehotelCategory; } @Override public void delete(long id) { hotelCategoryRepo.delete(id); } @Override public HotelCategory getById(long id) { return hotelCategoryRepo.findOne(id); } @Override public HotelCategory update(HotelCategory hotelCategory) { return hotelCategoryRepo.saveAndFlush(hotelCategory); } //get all categories @Override @Transactional(readOnly = true) public List<HotelCategory> findAll() { return hotelCategoryRepo.findAll(); } } 

HotelCategoryService.java

 package com.gptravel.service.category; import com.gptravel.entity.HotelCategory; import org.springframework.stereotype.Service; import java.util.List; @Service("hotelCategoryService") public interface HotelCategoryService { HotelCategory create(HotelCategory hotelCategory); void delete(long id); HotelCategory getById(long id); HotelCategory update(HotelCategory hotelCategory); List<HotelCategory> findAll(); } 

Repository:

 import com.gptravel.entity.HotelCategory; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface HotelCategoryRepository extends JpaRepository<HotelCategory, Long> { List<HotelCategory> findAllBy(Pageable pageable); } 

Jpa Config.java

 @Configuration @EnableTransactionManagement @ComponentScan("com.gptravel") @PropertySource(value = { "classpath:application.properties" }) @EnableJpaRepositories(basePackages="com.gptravel.dao") public class HibernateConfig { @Resource private Environment environment; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); dataSource.setUsername(environment.getRequiredProperty("jdbc.username")); dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); return dataSource; } @Bean public EntityManagerFactory entityManagerFactory() throws NamingException { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setJpaVendorAdapter(vendorAdapter); factoryBean.setPackagesToScan("com.gptravel.entity"); factoryBean.setDataSource(dataSource()); factoryBean.setPersistenceProviderClass (HibernatePersistenceProvider.class); factoryBean.setJpaProperties(jpaProperties()); factoryBean.setPersistenceUnitName("demo_hotels"); factoryBean.afterPropertiesSet(); return factoryBean.getObject(); } private Properties jpaProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql")); properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto")); return properties; } @Bean public PlatformTransactionManager transactionManager() throws NamingException { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } @Bean public SpringLiquibase liquibase() { SpringLiquibase liquibase = new SpringLiquibase(); liquibase.setChangeLog("classpath:db/db.changelog.xml"); liquibase.setDataSource(dataSource()); return liquibase; } } 

There is a simple test in the application for receiving data from the database - it completes successfully. TestJpaConfig.java

 @Configuration @EnableTransactionManagement @ComponentScan("com.gptravel") public class TestDataBaseConfig { private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver"; private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:mysql://localhost:3306/demo_hotels"; private static final String PROPERTY_NAME_DATABASE_USERNAME = "demo"; private static final String PROPERTY_NAME_DATABASE_PASSWORD = "demo"; private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQLDialect"; private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true"; private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.gptravel.entity"; private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "update"; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class); entityManagerFactoryBean.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN); entityManagerFactoryBean.setJpaProperties(hibernateProp()); return entityManagerFactoryBean; } @Bean public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER); dataSource.setUrl(PROPERTY_NAME_DATABASE_URL); dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME); dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD); return dataSource; } private Properties hibernateProp() { Properties properties = new Properties(); properties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT); properties.put("hibernate.show_sql", PROPERTY_NAME_HIBERNATE_SHOW_SQL); properties.put("hibernate.hbm2ddl.auto", PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO); return properties; } } 

TestClass.java

 @DirtiesContext @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestDataBaseConfig.class) @WebAppConfiguration public class HotelServiceImplTest { @Resource private EntityManagerFactory emf; protected EntityManager em; @Resource private HotelRepository hotelCategoryService; @Before public void setUp() throws Exception { em = emf.createEntityManager(); } @Test public void testSaveHotelCategory() throws Exception { hotelCategoryService.findAll(); List<Hotel> testList = hotelCategoryService.findAll(); System.out.println("Size - " + testList.size()); } } 

What could be the problem?

UPDATE # 1

Repository link: VaadinJPAHotelApp

  • If you do not mind, add the code in which you think the error appears in text form. - 0xdb
  • Where exactly is a mistake, I can not understand yet - Dmitriu

0