2015-07-12 73 views
0

我有一个表,并且当我按下删除按钮时,我想将映射表的状态字段从1更改为0。这是逻辑。使用休眠merge()和refresh()

这是我的映射表

@Entity 
@Table(name = "POSITION_ACCOUNT") 
public class PositionAccount { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "ID", columnDefinition = "NUMERIC(15, 0)",unique = true,nullable = false) 
    private Long id; 
    public Long getId() { 
     return id; 
    } 

    @Column(name = "ACCT_NUMBER") 
    private String accountNumber; 
    public String getAccountNumber() {  return accountNumber; } 

    @Column(name = "ACCT_NAME") 
    private String accountName; 
    public String getAccountName() { 
     return accountName; 
    } 

    @Column(name = "CURRENCY_CODE") 
    private String currencyCode; 
    public String getCurrencyCode() { 
     return currencyCode; 
    } 

    @Column(name = "BALANCE") 
    private BigDecimal balance = new BigDecimal("0"); 

    public BigDecimal getBalance() { 
     return balance; 
    } 

    @Column(name = "ACCT_TYPE") 
    private String accountType; 
    public String getAccountType() { return accountType; } 

    @Column(name = "STATE") 
    private int state = 1; 
    public int getState() { 
     return state; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public void setAccountNumber(String accountNumber) { 
     this.accountNumber = accountNumber; 
    } 

    public void setAccountName(String accountName) { 
     this.accountName = accountName; 
    } 

    public void setCurrencyCode(String currencyCode) { 
     this.currencyCode = currencyCode; 
    } 

    public void setBalance(BigDecimal balance) { 
     this.balance = balance; 
    } 

    public void setState(int state) { 
     this.state = state; 
    } 

    public void setAccountType(String accountType) { this.accountType = accountType; } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     PositionAccount that = (PositionAccount) o; 

     return !(id != null ? !id.equals(that.id) : that.id != null); 

    } 

    @Override 
    public int hashCode() { 
     return id != null ? id.hashCode() : 0; 
    } 

    @Override 
    public String toString() { 
     return "PositionAccount{" + 
       "id=" + id + 
       ", accountNumber='" + accountNumber + '\'' + 
       ", accountName='" + accountName + '\'' + 
       ", currencyCode='" + currencyCode + '\'' + 
       ", balance=" + balance + 
       ", state=" + state + 
       '}'; 
    } 
} 

这里是我的@ActionMethod

@Inject 
private LoroNostroService service; 
@Inject 
private LoroNostroModel model; 
@ActionMethod(ACTION_DELETE_ACCOUNT) 
public void deleteAccount() { 
    PositionAccount account = tv_loro_nostro_accounts.getSelectionModel().getSelectedItem(); 

    DeleteAccount input = new DeleteAccount(); 
    input.setAccountId(account.getId()); 
    input.setType(account.getAccountType()); 
    input.setAccNum(account.getAccountNumber()); 
    input.setAccName(account.getAccountName()); 
    input.setCurrency(account.getCurrencyCode()); 
    input.setBalance(account.getBalance()); 
    input.setCurState(0); 

    service.deleteAccount(input, context.getTaskView(), result -> { 
     model.getAccounts().addAll(result.getAccount()); 
     tv_loro_nostro_accounts.getSelectionModel().selectFirst(); 
    }); 
} 

其中tv_loro_nostro_accountsTableView我从中进行选择。 DeleteAccount类是一个类,我定义了我的表的所有字段getterssetters。一些操作后service.deleteAccount放在这里:

@Path("deleteAccount") 
@POST 
public DeleteAccount deleteAccount(DeleteAccount model){ 
    try { 
    model = operationService.execute(model,(result, userDetails) -> { 

     PositionAccount a = new PositionAccount(); 
     a.setAccountType(result.getType()); 
     a.setAccountNumber(result.getAccNum()); 
     a.setAccountName(result.getAccName()); 
     a.setCurrencyCode(result.getCurrency()); 
     a.setState(0); 
     service.deleteAccount(a); 

     result.setState(OperationState.DONE); 
     return result; 
    }); 
    }catch (AdcException e) { 
     logger.error("X: ", e); 
     model.setState(OperationState.ERROR); 
     model.setErrorText(e.getLocalizedMessage(model.getLocale())); 
    } catch (Exception e) { 
     logger.error("X: ", e); 
     model.setState(OperationState.ERROR); 
     model.setErrorText(e.getLocalizedMessage()); 
    } 
    return model; 
} 

其中service.deleteAccount

public void deleteAccount(PositionAccount account){ 
     repository.deleteAccount(account); 
    } 

repository.deleteAccount

public void deleteAccount(PositionAccount account){ 
     em.merge(account); 
     em.refresh(account); 
    } 

当我在上面跑,我收到错误

Entity not managed; nested exception is java.lang.IllaegalArgumentException: Entity not managed 

请小心解决以上问题。

+0

您在'deleteAccount(...)'中创建一个新对象。如果我没有错,你需要先坚持对象,这样它才能被管理。不确定,但我认为合并只适合,如果对象处于分离状态。 [查看更多有价值的信息](http://stackoverflow.com/questions/1069992/jpa-entitymanager-why-use-persist-over-merge) – lunatikz

回答

1

merge returnes托管实体实例,所以做出这种不抛出异常做到:

account = em.merge(account); 
em.refresh(account); 

然而,refresh将覆盖所有的变化,所以在这里不需要它。您的方法应该如下所示:

public void deleteAccount(PositionAccount account) { 
     em.merge(account); 
}