我有2个实体A和B,两者都是通过
相关的@OnetoMany(A - > B)和 @ManyToOne(B - > A)关系,两者各有10行。
现在我想要更新子表但子表的外键应该相同意味着父表不会更新。我该怎么做......?
让我告诉你我的实体以及控制器部分,
我的孩子实体,
package com.org.pc.entity;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the physical_allocation_detail database table.
*
*/
@Entity
@Table(name="physical_allocation_detail", schema="spc_cg")
@NamedQuery(name="PhysicalAllocationDetail.findAll", query="SELECT p FROM PhysicalAllocationDetail p")
public class PhysicalAllocationDetail implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="BudgetUpload_GENERATOR",sequenceName="spc_cg.seq_budget_id",allocationSize=1,initialValue=1)
@GeneratedValue(generator="BudgetUpload_GENERATOR",strategy=GenerationType.SEQUENCE)
@Column(name="physical_allocation_d_id")
private Integer physicalAllocationDId;
private double general;
@Column(name="physical_target")
private double physicalTarget;
private double sc;
@Column(name="scheme_component_id")
private Integer schemeComponentId;
private double st;
//bi-directional many-to-one association to PhysicalAllocationMaster
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="physical_allocation_m_id", insertable = true, updatable = true, nullable=false)
private PhysicalAllocationMaster physicalAllocationMaster;
public PhysicalAllocationDetail() {
}
public Integer getPhysicalAllocationDId() {
return this.physicalAllocationDId;
}
public void setPhysicalAllocationDId(Integer physicalAllocationDId) {
this.physicalAllocationDId = physicalAllocationDId;
}
public double getGeneral() {
return this.general;
}
public void setGeneral(double general) {
this.general = general;
}
public double getPhysicalTarget() {
return this.physicalTarget;
}
public void setPhysicalTarget(double physicalTarget) {
this.physicalTarget = physicalTarget;
}
public double getSc() {
return this.sc;
}
public void setSc(double sc) {
this.sc = sc;
}
public Integer getSchemeComponentId() {
return this.schemeComponentId;
}
public void setSchemeComponentId(Integer schemeComponentId) {
this.schemeComponentId = schemeComponentId;
}
public double getSt() {
return this.st;
}
public void setSt(double st) {
this.st = st;
}
public PhysicalAllocationMaster getPhysicalAllocationMaster() {
return this.physicalAllocationMaster;
}
public void setPhysicalAllocationMaster(PhysicalAllocationMaster physicalAllocationMaster) {
this.physicalAllocationMaster = physicalAllocationMaster;
}
}
我的主实体
package com.org.pc.entity;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
* The persistent class for the physical_allocation_master database table.
*
*/
@Entity
@Table(name="physical_allocation_master", schema="spc_cg")
@NamedQuery(name="PhysicalAllocationMaster.findAll", query="SELECT p FROM PhysicalAllocationMaster p")
public class PhysicalAllocationMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="BudgetUpload_GENERATOR",sequenceName="spc_cg.seq_budget_id",allocationSize=1,initialValue=1)
@GeneratedValue(generator="BudgetUpload_GENERATOR",strategy=GenerationType.SEQUENCE)
@Column(name="physical_allocation_m_id")
private Integer physicalAllocationMId;
@Column(name="allocation_entry_by")
private String allocationEntryBy;
@Temporal(TemporalType.DATE)
@Column(name="date_of_entry")
private Date dateOfEntry;
@Column(name="is_approved")
private Integer isApproved;
@Column(name="location_id")
private Integer locationId;
@Column(name="location_type_id")
private Integer locationTypeId;
@Column(name="scheme_id")
private Integer schemeId;
@Column(name="year_id")
private Integer yearId;
//bi-directional many-to-one association to PhysicalAllocationDetail
@OneToMany(mappedBy="physicalAllocationMaster", cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
private Set<PhysicalAllocationDetail> physicalAllocationDetails;
public PhysicalAllocationMaster() {
}
public Integer getPhysicalAllocationMId() {
return this.physicalAllocationMId;
}
public void setPhysicalAllocationMId(Integer physicalAllocationMId) {
this.physicalAllocationMId = physicalAllocationMId;
}
public String getAllocationEntryBy() {
return this.allocationEntryBy;
}
public void setAllocationEntryBy(String allocationEntryBy) {
this.allocationEntryBy = allocationEntryBy;
}
public Date getDateOfEntry() {
return this.dateOfEntry;
}
public void setDateOfEntry(Date dateOfEntry) {
this.dateOfEntry = dateOfEntry;
}
public Integer getIsApproved() {
return this.isApproved;
}
public void setIsApproved(Integer isApproved) {
this.isApproved = isApproved;
}
public Integer getLocationId() {
return this.locationId;
}
public void setLocationId(Integer locationId) {
this.locationId = locationId;
}
public Integer getLocationTypeId() {
return this.locationTypeId;
}
public void setLocationTypeId(Integer locationTypeId) {
this.locationTypeId = locationTypeId;
}
public Integer getSchemeId() {
return this.schemeId;
}
public void setSchemeId(Integer schemeId) {
this.schemeId = schemeId;
}
public Integer getYearId() {
return this.yearId;
}
public void setYearId(Integer yearId) {
this.yearId = yearId;
}
public Set<PhysicalAllocationDetail> getPhysicalAllocationDetails() {
return this.physicalAllocationDetails;
}
public void setPhysicalAllocationDetails(Set<PhysicalAllocationDetail> physicalAllocationDetailSet) {
this.physicalAllocationDetails = physicalAllocationDetailSet;
}
public PhysicalAllocationDetail addPhysicalAllocationDetail(PhysicalAllocationDetail physicalAllocationDetail) {
getPhysicalAllocationDetails().add(physicalAllocationDetail);
physicalAllocationDetail.setPhysicalAllocationMaster(this);
return physicalAllocationDetail;
}
public PhysicalAllocationDetail removePhysicalAllocationDetail(PhysicalAllocationDetail physicalAllocationDetail) {
getPhysicalAllocationDetails().remove(physicalAllocationDetail);
physicalAllocationDetail.setPhysicalAllocationMaster(null);
return physicalAllocationDetail;
}
}
我的控制器部分是,
List<PhysicalAllocationMaster> physicalAllocationMasterList=physicalAllocationMasterService.findByProperty("schemeId",schemeId,"yearId",yearId,"allocationEntryBy",allocEntryBy);
Integer masterListSize=physicalAllocationMasterList.size();
logger.info("Size of physicalAllocationMasterList =>"+masterListSize);
if(masterListSize!=0){
PhysicalAllocationMaster physicalAllocationMaster = new PhysicalAllocationMaster();
logger.info("Inside If------------"+masterListSize);
for(int i = 0; i < masterListSize; i++){
whysicalAllocationDetail physicalAllocationDetail = new PhysicalAllocationDetail();
physicalAllocationDetail.setSchemeComponentId(schemeComponenet);
if(general.length!=0 && general[i] != null && general[i].toString().trim().length() > 0){
physicalAllocationDetail.setGeneral(Double.parseDouble(general[i]));
}
else{
physicalAllocationDetail.setGeneral(0);
}
if(sc.length!=0 && sc[i] != null && sc[i].toString().trim().length() > 0){
physicalAllocationDetail.setSc(Double.parseDouble(sc[i]));
}else{
physicalAllocationDetail.setSc(0);
}
if(st.length!=0 && st[i] != null && st[i].toString().trim().length() > 0){
physicalAllocationDetail.setSt(Double.parseDouble(st[i]));
}else{
physicalAllocationDetail.setSt(0);
}
if(total.length!=0 && total[i] != null && total[i].toString().trim().length() > 0){
physicalAllocationDetail.setPhysicalTarget(Double.parseDouble(total[i]));
}else{
physicalAllocationDetail.setPhysicalTarget(0);
}
Integer physicalAllocationMID = physicalAllocationMasterList.get(i).getPhysicalAllocationMId();
List<PhysicalAllocationDetail> physicalAllocationDetailList = physicalAllocationDetailService.findByProperty("physicalAllocationMaster.physicalAllocationMId", physicalAllocationMID);
if(physicalAllocationDetailList.size()!=0){
System.out.println("Scheme Component Id --- >"+schemeComponenet);
List<PhysicalAllocationDetail> schemeComponentList=physicalAllocationDetailService.findByProperty("schemeComponentId", schemeComponenet);
Integer schemeComponentCount = schemeComponentList.size();
Integer physicalAllocationMId = schemeComponentList.get(0).getPhysicalAllocationMaster().getPhysicalAllocationMId();
if(schemeComponentCount!=0){
Integer physicalAllocationDid = schemeComponentList.get(0).getPhysicalAllocationDId();
physicalAllocationDetail.setPhysicalAllocationMaster(physicalAllocationMaster);
physicalAllocationDetail.setPhysicalAllocationDId(physicalAllocationDid);
physicalAllocationDetailService.merge(physicalAllocationDetail);
}
else{
physicalAllocationMaster.setPhysicalAllocationMId(physicalAllocationMId);
physicalAllocationDetail.setPhysicalAllocationMaster(physicalAllocationMaster);
physicalAllocationDetailService.persist(physicalAllocationDetail);
}
}else{
System.out.println("<=================ERROR========================>");
}
}}
请帮助解决此问题
答案 0 :(得分:0)
删除cascade={CascadeType.ALL}
课程@ManyToOne
中的PhysicalAllocationDetail
。