简体   繁体   中英

Spring Data Jpa not persisting the entity to mysql

Hi have written a Test(junit) case for creating and fetching the User entity from/to MYSQL db. The Read functionality is working but Create feature is not working. Neither I am getting any error log. Below is my code:

package com.learn.springConfig;
//importes

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.learn.persistence.dao")
@ComponentScan("com.learn.persistence")
@PropertySource({ "classpath:persistence-mysql.properties" })
public class PersistenceJpaConfig {

    @Autowired
    Environment env;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
        entityManager.setDataSource(dataSource());
        entityManager.setPackagesToScan(new String[] { "com.learn.persistence.model" });
        final JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        entityManager.setJpaVendorAdapter(jpaVendorAdapter);
        entityManager.setJpaProperties(additionalProperties());
        return entityManager;

    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
        properties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql", "false"));
        properties.setProperty("jpa.generateDdl", env.getProperty("jpa.generateDdl", "true"));
        return properties;
    }

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

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}    

The User entity:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "USER_ID")
    private Long userId;
    private String email;
    @NotNull
    @Size(max = 255)
    private String name;
    @Nullable
    private boolean locked;
    @NotNull
    @Size(max = 255)
    private String password;
    //setters getters
}    

Jpa User Repository:

package com.learn.persistence.dao;

public interface UserJpaRepository extends CrudRepository<User, Long>{

}    

Unit test case:

@ContextConfiguration(classes = {PersistenceJpaConfig.class, ContextConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class TestJPA {

    @Autowired
    private UserJpaRepository userJpaRepository;

    @Test
    @Transactional
    public void insertUsers() {
        User user1 = new User("puspender@fake.com","puspender", false, "12345");
        System.out.println("saving user");
        assertNotNull(userJpaRepository.save(user1));
    }

    @Test
    @Transactional
    public void findAllUsers() {
        Iterable<User> users = userJpaRepository.findAll();
        for(User user : users) {
            System.out.println(user.getName()+"\t"+ user.isLocked());
        }
    }

}

findAllUsers() is bringing me all the users, but insertUsers() is not inserting the User.
Any hint, why this behaviour?

"Neither I am getting any error log" so you tests are passing? But your saved user1 does not show up in findAll() ?

By default tests are transactional and roll back at the end of each test . So in your second test findAllUsers user1 is no longer there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM