Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 插入子表时,外键显示为null_Java_Hibernate_Jpa_One To Many_Hibernate Mapping - Fatal编程技术网

Java 插入子表时,外键显示为null

Java 插入子表时,外键显示为null,java,hibernate,jpa,one-to-many,hibernate-mapping,Java,Hibernate,Jpa,One To Many,Hibernate Mapping,我试图将每个父表主键Id作为外键插入子表。首先父表将更新,然后更新相关子表。但是当我尝试它时,它总是显示为null。但是如果我只插入主数据,主键将正确生成。我在父母中使用了JPA@OneToMany关系,在孩子中使用了@ManyToOne关系 我的第一个类是…这是事务主类 package com.org.pc.entity; import java.io.Serializable; import javax.persistence.*; import java.util.Date; imp

我试图将每个父表主键Id作为外键插入子表。首先父表将更新,然后更新相关子表。但是当我尝试它时,它总是显示为null。但是如果我只插入主数据,主键将正确生成。我在父母中使用了JPA@OneToMany关系,在孩子中使用了@ManyToOne关系

我的第一个类是…这是事务主类

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;
    }
事务的控制器部分是

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不太熟悉。

在坚持使用master之前,您在细节上掌握的最多,很抱歉,最后两条命令行不起作用。。。这就是为什么我对它进行了注释……我怎么能详细地设置master呢?我在代码中看不到任何注释行。但在您的细节中,您有一个用于master的setter方法。把这个交给你的主人。PhysicalLocationDetail.SetPhysicalLocationMasterPhysicalLocationMaster。在坚持大师之前先详细设置大师。谢谢。。我试过了,现在它起作用了。谢谢你的帮助。。。
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);