使用spring保存/更新数据

时间:2016-10-28 12:35:55

标签: java spring orm

我从以下代码中获取了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;
    }


}

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

1 个答案:

答案 0 :(得分:0)

如果您使用的是Spring,那么通过不使用Spring功能会让自己变得非常困难

我建议你做以下事情:

  1. 使用Spring Data JPA,编写存储库接口以允许 你可以轻松地坚持你的实体:

    public interface ManagementEmailConfigRepository扩展了JpaRepository {}

  2. 用它来保存你的实体(如果不存在则保存插入, 如果是,则更新

    @注入 private ManagementEmailConfigRepository managementEmailConfigRepository; .... managementEmailConfigRepository.save(managementEmailConfig);

  3. 这将从您的代码中删除以下内容:

    1. 需要编写一个保存方法
    2. 需要冲洗
    3. 无需尝试捕获类型代码
    4. 您的实体不需要该命名查询     (您可以在存储库中免费获得)
    5. 我将由您决定@Transactional注释的位置;它真的不应该在你的DAO层上,而是更高,例如你的服务层。