【问题标题】:EntityManager doesn't merge object to databaseEntityManager 不会将对象合并到数据库
【发布时间】:2016-03-19 22:50:08
【问题描述】:

我有一个 java spring 应用程序并使用 JPA

JPA 初始化代码

@Configuration
@EnableJpaRepositories
public class Application {

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/database");
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUsername("root");
    dataSource.setPassword("pass");
    dataSource.setInitialSize(20);
    dataSource.setMaxActive(30);
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource);
    lef.setJpaVendorAdapter(jpaVendorAdapter);
    lef.setPackagesToScan("repository");
    return lef;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(false);
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
    return hibernateJpaVendorAdapter;
}

}

我也有简单的实体对象

@Entity
public class Client {
@Id
private int id;
private String name;
private String email;
private String phone;

public Client(){};

public Client(int id, String name, String email, String phone){
   this.id=id;
    this.name=name;
    this.email=email;
    this.phone=phone;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

}

和存储库

@Repository
public class ProductRepository {

@PersistenceContext
EntityManager em;

@Transactional
public void saveClient() {
Client cl = new Client(1,"Alex","alex@gmail.com","1111111111");
em.merge(cl);

    }
}

当我直接从控制器调用此方法时,我不会将数据合并到数据库中

@Controller
@ComponentScan("repository")
@RequestMapping("/")
public class HomeController {

@Autowired
ProductRepository productRepository;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
productRepository.saveClient();  
return "home";
    }
}

我没有收到任何编译或运行时异常和错误,但合并不会导致我的对象被保存

【问题讨论】:

  • hibernateJpaVendorAdapter.setShowSql(true);更改 true 并通过休眠显示生成的查询并在此处发布
  • 看起来很奇怪((休眠:从客户端client0_中选择client0_.id作为id1_0_0_,client0_.email作为email2_0_0_,client0_.name作为name3_0_0_,client0_.phone作为phone4_0_0_,其中client0_.id=?
  • 你配置了事务管理器吗?
  • 已经完成了。没有帮助
  • @Bean public JpaTransactionManager transactionManager(EntityManagerFactory emf) { return new JpaTransactionManager(emf); }

标签: java spring jpa


【解决方案1】:

试试这个

@Configuration
@EnableTransactionManagement
public class Application {}

而不是

@Configuration
@EnableJpaRepositories
public class Application {}

【讨论】:

  • HTTP 状态 500 - 请求处理失败;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute statement
  • 但是来自 Hibernate 的好消息)) Hibernate:插入到客户端(电子邮件、姓名、电话)值(?、?、?)
  • 甚至 2 个查询 Hibernate:从客户端 client0_ 中选择 client0_.id 作为 id1_0_0_,client0_.email 作为 email2_0_0_,client0_.name 作为 name3_0_0_,client0_.phone 作为 phone4_0_0_,其中 client0_.id=? Hibernate:插入客户端(电子邮件、姓名、电话)值(?、?、?)
  • 在客户端类中注释 @GeneratedValue 也是 id
  • 不幸的是没有帮助
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
相关资源
最近更新 更多