我试图将每个父表主键Id作为外键插入子表。首先,父表将更新相关的子表。但是,当我尝试它总是显示null。但如果我只检查主数据主键正在生成。我在父母和@ManyToOne中使用JPA @OneToMany关系。
我的第一堂课是......(哪个是交易大师班)
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 List<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 List<PhysicalAllocationDetail> getPhysicalAllocationDetails() {
return this.physicalAllocationDetails;
}
public void setPhysicalAllocationDetails(List<PhysicalAllocationDetail> physicalAllocationDetails) {
this.physicalAllocationDetails = physicalAllocationDetails;
}
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;
}
我的第二堂课是(儿童班)......
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;
}
交易的控制器部分是......
PhysicalAllocationMaster physicalAllocationMaster=new PhysicalAllocationMaster();
PhysicalAllocationDetail physicalAllocationDetail= new PhysicalAllocationDetail();
List<LocationMaster> locationMasterList=locationMasterService.findByProperty("locationId", Integer.parseInt(serial[i]));
Integer locationMasterListSize = locationMasterList.size();
if(locationMasterListSize!=0){
physicalAllocationMaster.setLocationId(locationMasterList.get(0).getLocationId());
physicalAllocationMaster.setLocationTypeId(locationMasterList.get(0).getLocationTypeId());
}
else{
logger.info("Location is not present for Id "+serial[i]);
}
physicalAllocationMaster.setSchemeId(schemeId);
physicalAllocationMaster.setYearId(yearId);
physicalAllocationMaster.setDateOfEntry(sqlDate);
physicalAllocationMaster.setAllocationEntryBy(allocEntryBy);
physicalAllocationMaster.setIsApproved(1);
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);
}
logger.info("Physical Allocation Master Primary Key: "+physicalAllocationMaster.getPhysicalAllocationMId());
physicalAllocationDetailSet.add(physicalAllocationDetail); physicalAllocationMaster.addPhysicalAllocationDetail(physicalAllocationDetail);
physicalAllocationMaster.setPhysicalAllocationDetails(physicalAllocationDetailSet);
physicalAllocationMasterService.persist(physicalAllocationMaster);
请帮助解决这个问题。我不熟悉JPA或Hibernate。