2016-10-28 83 views
0

保存/更新数据我得到一个javax.persistence.EntityNotFoundException从以下代码:与弹簧

@Transactional 
    public ManagementEmailConfig save(ManagementEmailConfig managementEmailConfig) 
    { 
     logger.info("Save Management Email Config"); 
     try 
     { 
      managementEmailConfig = entityManager.merge(managementEmailConfig); 
      entityManager.flush(); 
     } catch (Exception e) 
     { 
      //ERROR: com.xxx.app.dao.kpi.ManagementEmailConfigDAO - 
Not able to save Management Email Config 
      //javax.persistence.EntityNotFoundException: Unable to find com.xxx.app.model.configuration.AlertCommunicationAddress with id 1260 
      logger.error("Not able to save Management Email Config", e); 
      return null; 
     } 

     return managementEmailConfig; 
    } 

其中模型看起来像这样(缩短的版本):

import java.io.Serializable; 

import javax.persistence.*; 

import java.util.List; 


/** 
* The persistent class for the MANAGEMENT_EMAIL_CONFIG database table. 
* 
*/ 
@Entity 
@Table(name="MANAGEMENT_EMAIL_CONFIG") 
@NamedQuery(name="ManagementEmailConfig.findAll", query="SELECT m FROM ManagementEmailConfig m") 
public class ManagementEmailConfig implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name="MANAGEMENT_EMAIL_CONFIG_ID") 
    private long managementEmailConfigId; 

    //bi-directional many-to-one association to AlertCommunicationAddress 
    @OneToMany(mappedBy="managementEmailConfig") 
    private List<AlertCommunicationAddress> alertCommunicationAddresses; 

    public ManagementEmailConfig() { 
    } 

    public long getManagementEmailConfigId() { 
     return this.managementEmailConfigId; 
    } 

    public void setManagementEmailConfigId(long managementEmailConfigId) { 
     this.managementEmailConfigId = managementEmailConfigId; 
    } 

    public List<AlertCommunicationAddress> getAlertCommunicationAddresses() { 
     return this.alertCommunicationAddresses; 
    } 

    public void setAlertCommunicationAddresses(List<AlertCommunicationAddress> alertCommunicationAddresses) { 
     this.alertCommunicationAddresses = alertCommunicationAddresses; 
    } 

    public AlertCommunicationAddress addAlertCommunicationAddress(AlertCommunicationAddress alertCommunicationAddress) { 
     getAlertCommunicationAddresses().add(alertCommunicationAddress); 
     alertCommunicationAddress.setManagementEmailConfig(this); 

     return alertCommunicationAddress; 
    } 

    public AlertCommunicationAddress removeAlertCommunicationAddress(AlertCommunicationAddress alertCommunicationAddress) { 
     getAlertCommunicationAddresses().remove(alertCommunicationAddress); 
     alertCommunicationAddress.setManagementEmailConfig(null); 

     return alertCommunicationAddress; 
    } 


} 

用例是用户提供了一个新的alertCommunicationAddress到现有的ManagementEmailConfig,我想创建alertCommunicationAddress然后更新ManagementEmailConfig。

回答

0

如果你正在使用Spring,你已经不使用Spring特性

使生活自己真的很难,我建议你做到以下几点:

  1. 使用Spring Data JPA,写一个库接口,允许 您可以轻松坚持您的实体:

    public interface ManagementEmailConfigRepository extends JpaRepository {}

  2. 用它来坚持你的实体(除被插入,如果它不存在,如果 更新它)

    @Inject 私人ManagementEmailConfigRepository managementEmailConfigRepository; .... managementEmailConfigRepository.save(managementEmailConfig);

这摆脱从您的代码如下:

  1. 需要写一个在所有
  2. 保存方法需要做冲洗
  3. 没有必要尝试捕捉类型代码
  4. 无需在您的实体上使用该命名查询 (您可以在您的存储库上免费获得)

我会留给你决定你想要@Transactional注释的位置;它确实不应该放在你的DAO层上,而应该放在你的DAO层上。你的服务层。

+0

我正在寻找更多的东西,使旧的方式工作,相比之下,最终成为重写40+模型和类。 – Adder