gpt4 book ai didi

java - entityManager.persist 不将任何内容保存到数据库

转载 作者:行者123 更新时间:2023-11-30 10:45:21 25 4
gpt4 key购买 nike

我使用 Spring 4.2.5org.hibernate 5.1.0

当我使用 entityManager.persist(user) 保存 user 时,它无法保存到数据库,也没有抛出错误。

但如果我添加 entityManager.getTransaction().begin();entityManager.getTransaction().commit(); 它就起作用了。

下面的代码

@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();
}
}

还有我的配置

@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();
}
}

还有我的模型

@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>

我认为问题是Transactional。但是下面的调试日志。

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

更新

如果我添加 entityManager.flush(); 导致 javax.persistence.TransactionRequiredException: no transaction is in progress.问题是什么?

最佳答案

解决方案是注入(inject) EntityManager 而不是 EntityManagerFactory

关于java - entityManager.persist 不将任何内容保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37053071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com