2016-10-28 81 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