TIMFUNNY
TIMFUNNY

Reputation: 383

entityManager.persist not saving anything to database

I use Spring 4.2.5 and org.hibernate 5.1.0.

When I use entityManager.persist(user) to save user ,it can't save to the database,and no error throw.

But if I add entityManager.getTransaction().begin(); and entityManager.getTransaction().commit(); it worked.

code below

@Service
@Transactional
public class UserTestService {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;


    public void addUser(User user){
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        //entityManager.getTransaction().begin();
        entityManager.persist(user);
        //entityManager.getTransaction().commit();
    }
}

And my config

@Configuration
@EnableTransactionManagement
public class JPAConfig  {

    @Autowired
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter() );
        entityManagerFactoryBean.setPackagesToScan("zhihu.model");
        return entityManagerFactoryBean;
    }


    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        return adapter;
    }



    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);

        return transactionManager;
    }

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

And my model

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

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long userID;

    @Column(name="username")
    private String username;

    @Column(name="password")
    private String password;

    public User(){

    }

    public User(long userID, String username, String password) {
        this.userID = userID;
        this.username = username;
        this.password = password;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }


    public long getUserID() {
        return userID;
    }

    public void setUserID(long userID) {
        this.userID = userID;
    }

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

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

    </dependencies>

I think the problem is Transactional.But the debug log below .

2016-05-05 21:33:10 DEBUG TransactionImpl:51 - begin
2016-05-05 21:33:10 DEBUG TransactionImpl:62 - committing

UPDATE

if I add entityManager.flush(); caused javax.persistence.TransactionRequiredException: no transaction is in progress.What is the problem?

Upvotes: 4

Views: 16131

Answers (4)

Mpho Lesetedi
Mpho Lesetedi

Reputation: 1

Remove the EntityManagerFactory paramenter from the method below. Instead inject the one you set above by setting it to the JpaTransactionManager. The below code will work for you.

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

    return transactionManager;
}

Upvotes: 0

TIMFUNNY
TIMFUNNY

Reputation: 383

The solution is to inject the EntityManager instead of EntityManagerFactory

Upvotes: 3

MyTwoCents
MyTwoCents

Reputation: 7624

If you are using

entityManager.persist(user);

Changed will not reflect in database after this statement executes. Entity manages flush these changes to your persistence layer automatically.

We have option to flush it manually using

entityManager.flush(); 

This should solve your problem

Upvotes: 1

Ahmed Gamal
Ahmed Gamal

Reputation: 1706

Commit will make the database commit. The changes to persistent object will be written to database.

When you have a persisted object and you change a value on it, it becomes dirty and hibernate needs to flush these changes to your persistence layer. It may do this automatically for you or you may need to do this manually, that depends on your flush mode(auto or manual) :)

finally you should use commit : transaction.commit() does flush the session, but it also ends the unit of work.

Check here for more .

Upvotes: 1

Related Questions