java - EJB 删除实体不起作用

标签 java jakarta-ee jpa ejb-3.0

我正在使用 netbeans 并从数据库生成实体类。我对插入和更新实体的所有合并调用都运行良好,但是当我尝试删除实体时,它不会将其从数据库中删除,也不会抛出任何异常。有人可以帮我解决吗。我的代码如下:

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 + " ]";
    }

}

最佳答案

您的持久性上下文似乎与底层数据库不同步。

尝试以下操作:

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

关于java - EJB 删除实体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682323/

相关文章:

java- 为什么我在使用 gui 创建 ATM 时收到 "class is not abstract and does not override abstract method"?

java - MySQL 日期和时间插入错误

java - EJB 3.1 - 为什么无状态 bean 必须通过其接口(interface)注入(inject)(如果有的话)?

jpa - @SpringBootTest 干扰 EclipseLink 动态编织

jpa - 从 GF 3.1.2 迁移到 Payara 4.1.1 后 Eclipselink 异常

java - 为什么这两种算法的运行时间存在差异?

java - (高效)删除和添加 Array 或 ArrayList 中的值 - Android/Java

java - JAX-WS - 如何配置完整的 URL

jakarta-ee - JTA和Spring @Transactional批注之间的区别

java - 如何建模多对多关系