0

jpa マージを使用して既存のエンティティを更新すると、期待される既存のエンティティを更新するのではなく、同じ ID のエンティティが挿入されるようです。挿入後、データベースの行の順序が失われるためです。しかし、私はまだ同じIDを持つ同じエンティティを持っています.JPAは挿入を使用して更新しますか? つまり、既存のエンティティを削除し、更新された値で再度挿入して、更新ジョブを実行します。主な混乱は、データベースの順序が失われることです。

これが私のリスナーメソッドです: userService JPA を使用する内の EJB クラスです。

public void onEditUserOrganization(RowEditEvent event){
    UserOrganization uorg =(UserOrganization) event.getObject();
    try {

    userService.updateUserOrganization(uorg);

    } catch (UserException ex) {
        ex.printStackTrace();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("error)"));
    }finally{
        parentlist = getUserOrganizationParents();
        branchlist = getUserOrganizationBranches();
    }
}

これが私の主な更新方法です

    @Override
public void updateUserOrganization(UserOrganization org) throws UserException{
    if(org != null && !em.contains(org)){
    try{

        UserOrganization existing = em.find(UserOrganization.class, org.getUorgId());
        existing.setOrgcode(org.getOrgcode());
        existing.setOrgname(org.getOrgname());
        existing.setParent(org.getParent());


    }catch(Exception e){
    throw new UserException("Couldn't update user org with id " + org.getUorgId());
    }
    }
}

これが私のエンティティクラスです:

public class UserOrganization implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "uorg_id", nullable = false)
private Integer uorgId;
@Basic(optional = false)
@NotNull
@Column(name = "parent", nullable = false)
private short parent;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "orgcode", nullable = false, length = 10)
private String orgcode;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 150)
@Column(name = "orgname", nullable = false, length = 150)
private String orgname;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "uorgId")
private List<refMain> refList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "uorgId")
private List<User1> user1List;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userOrganization")
private List<Bankrelation> relationList;


public UserOrganization() {
}

//getter setters..
    @Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof UserOrganization)) {
        return false;
    }
    UserOrganization other = (UserOrganization) object;
    if ((this.uorgId == null && other.uorgId != null) || (this.uorgId != null && !this.uorgId.equals(other.uorgId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "mn.bs.crasmon.model.UserOrganization[ uorgId=" + uorgId + " ]";
}

JPAで更新を行う最良の方法は何ですか

4

0 に答える 0