gpt4 book ai didi

java - JPA 在运行时删除约束

转载 作者:行者123 更新时间:2023-12-01 05:58:49 27 4
gpt4 key购买 nike

我正在从与同一实体(表示节点层次结构)具有一对多关系的表中删除实体。如果我将 xincoCoreNodeId 关系设置为级联,那么它就可以工作,但我不希望在实际应用程序中出现这种情况。

我不想移除叶子并移除其父代。有没有办法在运行时修改此关系或禁用约束,以便我可以删除整个表内容而不会收到约束提示?

    package com.bluecubs.xinco.core.server.persistence;

import com.bluecubs.xinco.core.server.AuditedEntityListener;
import com.bluecubs.xinco.core.server.XincoAuditedObject;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.persistence.annotations.PrivateOwned;

/**
*
* @author Javier A. Ortiz Bultrón <javier.ortiz.78@gmail.com>
*/
@Entity
@Table(name = "xinco_core_node")
@EntityListeners(AuditedEntityListener.class)
@NamedQueries({
@NamedQuery(name = "XincoCoreNode.findAll", query = "SELECT x FROM XincoCoreNode x"),
@NamedQuery(name = "XincoCoreNode.findById", query = "SELECT x FROM XincoCoreNode x WHERE x.id = :id"),
@NamedQuery(name = "XincoCoreNode.findByDesignation", query = "SELECT x FROM XincoCoreNode x WHERE x.designation = :designation"),
@NamedQuery(name = "XincoCoreNode.findByStatusNumber", query = "SELECT x FROM XincoCoreNode x WHERE x.statusNumber = :statusNumber")})
public class XincoCoreNode extends XincoAuditedObject implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id", nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "designation", nullable = false, length = 255)
private String designation;
@Basic(optional = false)
@Column(name = "status_number", nullable = false)
private int statusNumber;
@JoinColumn(name = "xinco_core_language_id", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private XincoCoreLanguage xincoCoreLanguageId;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "xincoCoreNodeId", fetch = FetchType.LAZY)
private List<XincoCoreNode> xincoCoreNodeList;
@JoinColumn(name = "xinco_core_node_id", referencedColumnName = "id")
@PrivateOwned
@ManyToOne(fetch = FetchType.LAZY)
private XincoCoreNode xincoCoreNodeId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "xincoCoreNodeId", fetch = FetchType.LAZY)
private List<XincoCoreAce> xincoCoreAceList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "xincoCoreNodeId", fetch = FetchType.LAZY)
private List<XincoCoreData> xincoCoreDataList;

public XincoCoreNode() {
}

public XincoCoreNode(Integer id) {
this.id = id;
}

public XincoCoreNode(Integer id, String designation, int statusNumber) {
this.id = id;
this.designation = designation;
this.statusNumber = statusNumber;
}

public Integer getId() {
return id;
}

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

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public int getStatusNumber() {
return statusNumber;
}

public void setStatusNumber(int statusNumber) {
this.statusNumber = statusNumber;
}

public XincoCoreLanguage getXincoCoreLanguageId() {
return xincoCoreLanguageId;
}

public void setXincoCoreLanguageId(XincoCoreLanguage xincoCoreLanguageId) {
this.xincoCoreLanguageId = xincoCoreLanguageId;
}

public List<XincoCoreNode> getXincoCoreNodeList() {
return xincoCoreNodeList;
}

public void setXincoCoreNodeList(List<XincoCoreNode> xincoCoreNodeList) {
this.xincoCoreNodeList = xincoCoreNodeList;
}

public XincoCoreNode getXincoCoreNodeId() {
return xincoCoreNodeId;
}

public void setXincoCoreNodeId(XincoCoreNode xincoCoreNodeId) {
this.xincoCoreNodeId = xincoCoreNodeId;
}

public List<XincoCoreAce> getXincoCoreAceList() {
return xincoCoreAceList;
}

public void setXincoCoreAceList(List<XincoCoreAce> xincoCoreAceList) {
this.xincoCoreAceList = xincoCoreAceList;
}

public List<XincoCoreData> getXincoCoreDataList() {
return xincoCoreDataList;
}

public void setXincoCoreDataList(List<XincoCoreData> xincoCoreDataList) {
this.xincoCoreDataList = xincoCoreDataList;
}

@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.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 XincoCoreNode)) {
return false;
}
XincoCoreNode other = (XincoCoreNode) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}

@Override
public String toString() {
return "com.bluecubs.xinco.core.server.persistence.XincoCoreNode[id=" + id + "]";
}
}

最佳答案

如果您使用 Hibernate,则可以使用其对 batch processing 的支持有效地删除表中的许多行。基本上,查询从 HQL 转换为 SQL 并直接针对数据库运行。不过,这种方法也有局限性。例如,如果您持有对任何受影响对象的引用,则内存中的状态将不会更新。

不知道JPA本身是否支持批处理。可能不会。但是,如果您预计会影响大量行(数百行或数千行),那么无论如何,这就是 ORM 并不是真正适合该工作的工具的情况之一。使用直接 SQL 或存储过程来执行此操作可能更有意义。无论哪种方式,您都可以隔离数据访问对象内的代码,以便高级业务逻辑不需要关心操作是如何实现的。

关于java - JPA 在运行时删除约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1901637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com