TIMFUNNY TIMFUNNY - 7 months ago 51
Java Question

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?

Answer

The solution is to inject the EntityManager instead of EntityManagerFactory