2012-10-01 27 views
2

我正在使用netbeans并从数据库中生成实体类。我所有的插入和更新实体的合并调用都可以正常工作,但是当我尝试删除一个实体时,它不会从数据库中删除它,也不会引发异常。有人可以帮助我解决。我下面的代码:EJB删除实体不工作

AbstractFacade.java

public abstract class AbstractFacade<T> { 

    private Class<T> entityClass; 

    public AbstractFacade(Class<T> entityClass) { 
     this.entityClass = entityClass; 
    } 

    protected abstract EntityManager getEntityManager(); 

    public void create(T entity) { 
     getEntityManager().persist(entity); 
    } 

    public void edit(T entity) { 
     getEntityManager().merge(entity); 
    } 

    public void remove(T entity) { 
     getEntityManager().remove(getEntityManager().merge(entity)); 
    } 

    public T find(Object id) { 
     return getEntityManager().find(entityClass, id); 
    } 

    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public List<T> findAll() { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     cq.select(cq.from(entityClass)); 
     return getEntityManager().createQuery(cq).getResultList(); 
    } 

    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public List<T> findRange(int[] range) { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     cq.select(cq.from(entityClass)); 
     javax.persistence.Query q = getEntityManager().createQuery(cq); 
     q.setMaxResults(range[1] - range[0]); 
     q.setFirstResult(range[0]); 
     return q.getResultList(); 
    } 

    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    public int count() { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     javax.persistence.criteria.Root<T> rt = cq.from(entityClass); 
     cq.select(getEntityManager().getCriteriaBuilder().count(rt)); 
     javax.persistence.Query q = getEntityManager().createQuery(cq); 
     return ((Long) q.getSingleResult()).intValue(); 
    } 
} 

AccountEntity.java

@Entity 
@Table(name = "Account") 
public class AccountEntity implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "account_id") 
    private Long    accountId; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "account_user", nullable = false, length = 100) 
    private String    accountUser; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "account_pass", nullable = false, length = 100) 
    private String    accountPass; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "account_fullname", nullable = false, length = 100) 
    private String    accountFullName; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "account_email", nullable = false, length = 100) 
    private String    accountEmail; 
    @Basic(optional = false) 
    @Size(min = 1, max = 100) 
    @Column(name = "account_phone", nullable = true, length = 100) 
    private String    accountPhone; 
    @Basic(optional = false) 
    @Size(min = 1, max = 100) 
    @Column(name = "account_address", nullable = true, length = 100) 
    private String    accountAddress; 
    @JoinColumn(name = "role_id", referencedColumnName = "role_id") 
    @ManyToOne(optional = false) 
    private RoleEntity   roleId; 
    @JoinColumn(name = "dealer_id", referencedColumnName = "dealer_id") 
    @ManyToOne(optional = false) 
    private DealerEntity  dealerId; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "isAvailable", nullable = false) 
    private boolean    available; 

    public AccountEntity() { 
    } 

    public AccountEntity(Long accountId) { 
     this.accountId = accountId; 
    } 

    public AccountEntity(Long accountId, String accountUser, String accountPass) { 
     this.accountId = accountId; 
     this.accountUser = accountUser; 
     this.accountPass = accountPass; 
    } 

    public Long getAccountId() { 
     return accountId; 
    } 

    public void setAccountId(Long accountId) { 
     this.accountId = accountId; 
    } 

    public String getAccountUser() { 
     return accountUser; 
    } 

    public void setAccountUser(String accountUser) { 
     this.accountUser = accountUser; 
    } 

    public String getAccountPass() { 
     return accountPass; 
    } 

    public void setAccountPass(String accountPass) { 
     this.accountPass = accountPass; 
    } 

    public String getAccountFullName() { 
     return accountFullName; 
    } 

    public void setAccountFullName(String accountFullName) { 
     this.accountFullName = accountFullName; 
    } 

    public String getAccountEmail() { 
     return accountEmail; 
    } 

    public void setAccountEmail(String accountEmail) { 
     this.accountEmail = accountEmail; 
    } 

    public String getAccountPhone() { 
     return accountPhone; 
    } 

    public void setAccountPhone(String accountPhone) { 
     this.accountPhone = accountPhone; 
    } 

    public String getAccountAddress() { 
     return accountAddress; 
    } 

    public void setAccountAddress(String accountAddress) { 
     this.accountAddress = accountAddress; 
    } 

    public RoleEntity getRoleId() { 
     return roleId; 
    } 

    public void setRoleId(RoleEntity roleId) { 
     this.roleId = roleId; 
    } 

    public DealerEntity getDealerId() { 
     return dealerId; 
    } 

    public void setDealerId(DealerEntity dealerId) { 
     this.dealerId = dealerId; 
    } 

    public boolean isAvailable() { 
     return available; 
    } 

    public void setAvailable(boolean available) { 
     this.available = available; 
    } 

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

    @Override 
    public boolean equals(Object object) { 
     if (!(object instanceof AccountEntity)) { 
      return false; 
     } 
     AccountEntity other = (AccountEntity) object; 
     if ((this.accountId == null && other.accountId != null) || (this.accountId != null && !this.accountId.equals(other.accountId))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entities.AccountEntity[ accountId=" + accountId + " ]"; 
    } 
} 

DealerEntity.java

@Entity 
@Table(name = "Dealer") 
public class DealerEntity implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "dealer_id") 
    private Long dealerId; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 100) 
    @Column(name = "dealer_name", nullable = false, length = 100) 
    private String dealerName; 
    @Size(max = 100) 
    @Column(name = "dealer_phone", length = 100) 
    private String dealerPhone; 
    @Size(max = 100) 
    @Column(name = "dealer_fax", length = 100) 
    private String dealerFax; 
    @Size(max = 100) 
    @Column(name = "dealer_address", length = 100) 
    private String dealerAddress; 
    @Size(max = 100) 
    @Column(name = "dealer_coordinate", length = 100) 
    private String dealerCoordinate; 
    @Size(max = 100) 
    @Column(name = "state_name", length = 100) 
    private String stateName; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "isRoot", nullable = false) 
    private boolean isRoot; 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "isAvailable", nullable = false) 
    private boolean isAvailable; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dealerId") 
    private List<CustomerEntity> customerEntityList; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dealerId") 
    private List<ServiceEntity> serviceEntityList; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dealerId") 
    private List<AccountEntity> accountEntityList; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dealerId") 
    private List<PurchaseOrderEntity> purchaseOrderEntityList; 
    @JoinColumn(name = "country_id", referencedColumnName = "country_id", nullable = false) 
    @ManyToOne(optional = false) 
    private CountryEntity countryId; 

    public DealerEntity() { 
    } 

    public DealerEntity(Long dealerId) { 
     this.dealerId = dealerId; 
    } 

    public DealerEntity(Long dealerId, String dealerName, boolean isRoot, boolean isAvailable) { 
     this.dealerId = dealerId; 
     this.dealerName = dealerName; 
     this.isRoot = isRoot; 
     this.isAvailable = isAvailable; 
    } 

    public Long getDealerId() { 
     return dealerId; 
    } 

    public void setDealerId(Long dealerId) { 
     this.dealerId = dealerId; 
    } 

    public String getDealerName() { 
     return dealerName; 
    } 

    public void setDealerName(String dealerName) { 
     this.dealerName = dealerName; 
    } 

    public String getDealerPhone() { 
     return dealerPhone; 
    } 

    public void setDealerPhone(String dealerPhone) { 
     this.dealerPhone = dealerPhone; 
    } 

    public String getDealerFax() { 
     return dealerFax; 
    } 

    public void setDealerFax(String dealerFax) { 
     this.dealerFax = dealerFax; 
    } 

    public String getDealerAddress() { 
     return dealerAddress; 
    } 

    public void setDealerAddress(String dealerAddress) { 
     this.dealerAddress = dealerAddress; 
    } 

    public String getDealerCoordinate() { 
     return dealerCoordinate; 
    } 

    public void setDealerCoordinate(String dealerCoordinate) { 
     this.dealerCoordinate = dealerCoordinate; 
    } 

    public String getStateName() { 
     return stateName; 
    } 

    public void setStateName(String stateName) { 
     this.stateName = stateName; 
    } 

    public boolean getIsRoot() { 
     return isRoot; 
    } 

    public void setIsRoot(boolean isRoot) { 
     this.isRoot = isRoot; 
    } 

    public boolean getIsAvailable() { 
     return isAvailable; 
    } 

    public void setIsAvailable(boolean isAvailable) { 
     this.isAvailable = isAvailable; 
    } 

    @XmlTransient 
    public List<CustomerEntity> getCustomerEntityList() { 
     return customerEntityList; 
    } 

    public void setCustomerEntityList(List<CustomerEntity> customerEntityList) { 
     this.customerEntityList = customerEntityList; 
    } 

    @XmlTransient 
    public List<ServiceEntity> getServiceEntityList() { 
     return serviceEntityList; 
    } 

    public void setServiceEntityList(List<ServiceEntity> serviceEntityList) { 
     this.serviceEntityList = serviceEntityList; 
    } 

    @XmlTransient 
    public List<AccountEntity> getAccountEntityList() { 
     return accountEntityList; 
    } 

    public void setAccountEntityList(List<AccountEntity> accountEntityList) { 
     this.accountEntityList = accountEntityList; 
    } 

    @XmlTransient 
    public List<PurchaseOrderEntity> getPurchaseOrderEntityList() { 
     return purchaseOrderEntityList; 
    } 

    public void setPurchaseOrderEntityList(List<PurchaseOrderEntity> purchaseOrderEntityList) { 
     this.purchaseOrderEntityList = purchaseOrderEntityList; 
    } 

    public CountryEntity getCountryId() { 
     return countryId; 
    } 

    public void setCountryId(CountryEntity countryId) { 
     this.countryId = countryId; 
    } 

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

    @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 DealerEntity)) { 
      return false; 
     } 
     DealerEntity other = (DealerEntity) object; 
     if ((this.dealerId == null && other.dealerId != null) || (this.dealerId != null && !this.dealerId.equals(other.dealerId))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entities.DealerEntity[ dealerId=" + dealerId + " ]"; 
    } 

} 
+0

我不知道确切,但是为什么你在删除声明额外的代码?为什么你不能只说.remove(实体)?对我来说,这应该起作用。 – Logan

回答

0

看来您的持久性上下文与基础数据库不同步。

尝试以下操作:

public void remove(T entity) { 
    getEntityManager().remove(getEntityManager().merge(entity)); 
    getEntityManager().flush(); 
}