Hibernate实体更新现有行,而不是使用saveAndFlush创建新行

时间:2016-09-10 21:31:13

标签: java sql-server spring hibernate jpa

我在Spring Rest应用程序中使用Spring数据JPA,hibernate,sqlserver。

i)对于第一个请求,将记录插入到数据库中。工作正常,直到这里。

ii)当我用新数据更新现有记录而不是将新记录插入数据库时​​再发出新请求

iii)但是当应用程序上下文重新加载时,我能够插入新记录。

以下是代码段。

1)休眠配置

public class HibernateConfiguration {

@Autowired
private Environment env;

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

  dataSource.setDriverClassName(env.getRequiredProperty
       ("db.driverClassName"));
    dataSource.setUrl(env.getRequiredProperty("db.url"));
    dataSource.setUsername(env.getRequiredProperty("db.username"));
    dataSource.setPassword(env.getRequiredProperty("db.password"));

    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource);
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    entityManagerFactoryBean.setPackagesToScan(new String[] { my.domains.package });

    entityManagerFactoryBean.setJpaProperties(hibProperties());

    return entityManagerFactoryBean;
}

private Properties hibProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
    return properties;
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
}

2)域名

@Entity
@Table(name="Emp_Detetail")
public class EmpDetail implements java.io.Serializable {

private static final long serialVersionUID = 7342724430491939936L;

@Column(name="EmployeeId")
@Id
@GeneratedValue
private int employeeId;

   .......
}

3)JPA Respository

public interface EmpDetailRepository extends JpaRepository<EmpDetail, Integer>  {    

   }

4)DAO

@Repository("empDetailDao")
public class EmpDetailDaoImpl implements EmpDetailDao {

  @Resource
   private EmpDetailRepository empDetailRepository;

  @Override
  @Transactional
  public EmpDetail insertEmpDetails(EmpDetail empDetail) {
    return empDetailRepository.saveAndFlush(archive);
  }
}

5)服务类

@Service
public class EmpDetailServiceImpl implements EmpDetailService{

  @Autowired
  private EmpDetailDao empDetailDao;

  @Autowired
  private EmpDetail empBO;

 private EmpDetail toInsertEmpDetails(int active, String empName) throws
  Exception {
     empBO.setName(empName);
     empBO.setActive(active);
    empBO = empDetailDao.insertEmpDetails(empBO);
   }
   return empBO;
 }

6)控制器代码是

 @RestController
 public class EmpDeatilController {

  @Resource
  private EmpDetailService empDetailService;

  @RequestMapping(value = "/insertEmpDetail", method =
    RequestMethod.GET)
  @ResponseBody
  public EmpDetialResponse insertEmpDetail(@RequestParam("empName") String
    empName, @RequestParam("active") int active) throws Exception{

     return empDetailService.toInsertEmpDetails(active, empName);
   }

 }

请帮帮我。

提前致谢

1 个答案:

答案 0 :(得分:2)

插入第一个条目时,将插入的对象保存在字段

@Autowired
private EmpDetail empBO;

EmpDetailServiceImpl bean中。由于这是一个单例bean,当你进一步调用方法toInsertEmpDetails时,它将使用保存的对象,更新其名称和活动标志并保持此状态。由于此对象已经具有id(从您的第一次调用),它将更新数据库中的条目而不是创建新条目。要解决这个问题,只需删除字段empBO,通常不需要在服务中有这样的字段(应该是无状态的)。