当前位置: 首页 > 知识库问答 >
问题:

EJB删除实体不工作

赵昊阳
2023-03-14

我正在使用 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 + " ]";
    }

}

共有1个答案

丌官淇
2023-03-14

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

尝试以下操作:

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
    getEntityManager().flush();
}
 类似资料:
  • 删除父实体时,我还想删除关联的子实体(从数据库中)。我试图在删除时使用级联,如下所示,但我一定做错了什么。 当对父实体对象调用删除时,我收到错误消息:“该实体仍在数据库的其他地方引用”。我可以确认该实体在数据库的其他地方引用的唯一地方是在下面的两个表中(如果我手动从数据库中删除子行,对父实体对象的删除调用工作正常)。在过去的9个小时里,我一直在阅读实体对象并尝试不同的东西。我做错了什么? 这是我的

  • 我目前正在努力删除一个实体,它涉及到各种关系(只有< code > @ ManyToOne )——然而,Hibernate不删除任何东西——在调用< code>em.remove之后,一切都保持不变。 我有5个实体(< code>E1 - E5),引用问题中的实体(< code>E6)如下: 本身有相反的关系: (省略了id、附加列等...) 每当我调用< code > em . remove(i

  • 我的实体。ValidationStep与documentDetail有一对一的关系,documentDetail与documentValidations有一个完全的关系 我的删除查询 父ValidationStep被删除,但是docDetail和documentValidations仍然在数据库中。

  • 我在从数据库中删除实体时遇到问题。不管我做什么,它都不会删除。 驾驶员等级 评级等级 我做的第一件事是用,当调用时,它会抛出: org.postgresql.util。PSQLException:错误:更新或删除表“drivers”违反了表“ratings”上的外键约束“fk3raf3d9ucm571r485t8e7ew83” 好吧,选择另一种方式。尝试使用删除每个评级对象,但从未发生过,它只是彻

  • 主要内容:JPA实体删除示例要从数据库中删除记录,可以使用接口提供方法。方法使用主键来删除特定的记录。 JPA实体删除示例 在这里,我们将演示如何根据主键删除指定学生的信息。 完整的项目代码如下所示 - 这个例子包含以下步骤 - 第1步: 在包下创建一个名为的实体类,它包含属性:,和。 文件:StudentEntity.java 的代码如下 - 第2步: 将实体类和其他数据库配置映射到文件中。 文件:persistence.

  • 你好,我在删除实体时遇到了问题。entitymanager不会删除实体。有人看到代码中的错误吗? 错误消息: java.lang.Assertionerror: 预期 :null 实际 :帐户{id=1, 客户=客户 id=1, 名字=“金”, 姓氏=“佩德森”, email='kim@yahoo.no“, 电话号码=”90045870“, 出生=1980-11-05 00:00:00.0}, 登