更新外键的空值

时间:2014-06-21 05:37:16

标签: oracle hibernate jpa null foreign-key-relationship

我正在使用JPA,我正在尝试插入外键值,但是在修改它时正在更新空值..

请参阅以下代码

package com.bcits.bfm.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="JOB_CALENDER")
public class JobCalender implements java.io.Serializable  {


    private static final long serialVersionUID = 1L;

    private int jobCalenderId;
    private int scheduleType;
    private String title;
    private String description;
    private String recurrenceRule;
    private String recurrenceException;
    private int recurrenceId;
    private boolean isAllDay;
    private Date start;
    private Date end;

    private String jobNumber;
    private String jobGroup;
    private int expectedDays;
    private String jobPriority;
    private String jobAssets;
    private MaintainanceTypes maintainanceTypes;
    private MaintainanceDepartment departmentName;
    private JobTypes jobTypes;

    @Id
    @Column(name="JOB_CALENDER_ID")
    @SequenceGenerator(name = "JOB_CALENDER_SEQUENCE", sequenceName = "JOB_CALENDER_SEQUENCE")
    @GeneratedValue(generator = "JOB_CALENDER_SEQUENCE")    
    public int getJobCalenderId() {
        return jobCalenderId;
    }

    public void setJobCalenderId(int jobCalenderId ) {
        this.jobCalenderId = jobCalenderId ;
    }

    @Column(name="SCHEDULE_TYPE")
    public int getScheduleType() {
        return scheduleType;
    }

    public void setScheduleType(int scheduleType) {
        this.scheduleType = scheduleType;
    }

    @Column(name="START_DATE")   
    public Date getStart() {
        return start;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    @Column(name="END_DATE")   
    public Date getEnd() {
        return end;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    @Column(name="Title")
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name="DESCRIPTION")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name="RECURRENCE_RULE")
    public String getRecurrenceRule() {
        return recurrenceRule;
    }

    public void setRecurrenceRule(String recurrenceRule) {
        this.recurrenceRule = recurrenceRule;
    }

    @Column(name="IS_ALL_DAY")
    public boolean getIsAllDay() {
        return isAllDay;
    }

    public void setIsAllDay(boolean isAllDay) {
        this.isAllDay = isAllDay;
    }

    @Column(name="RECURENCE_EXCEPTION")
    public String getRecurrenceException() {
        return recurrenceException;
    }

    public void setRecurrenceException(String recurrenceException) {
        this.recurrenceException = recurrenceException;
    }

    @Column(name="RECURRENCE_ID")
    public int getRecurrenceId() {
        return recurrenceId;
    }

    public void setRecurrenceId(int recurrenceId) {
        this.recurrenceId = recurrenceId;
    }


    /*Custom Columns*/

    @Column(name = "JOB_NUMBER", nullable = false, length = 45)
    public String getJobNumber() {
        return this.jobNumber;
    }

    public void setJobNumber(String jobNumber) {
        this.jobNumber = jobNumber;
    }

    @Column(name = "JOB_GROUP", nullable = false, length = 45)
    public String getJobGroup() {
        return this.jobGroup;
    }

    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    @Column(name = "EXPECTED_DAYS", nullable = false, precision = 11, scale = 0)
    public int getExpectedDays() {
        return this.expectedDays;
    }

    public void setExpectedDays(int expectedDays) {
        this.expectedDays = expectedDays;
    }

    @Column(name = "JOB_PRIORITY", nullable = false, length = 45)
    public String getJobPriority() {
        return this.jobPriority;
    }

    public void setJobPriority(String jobPriority) {
        this.jobPriority = jobPriority;
    }

    @Column(name = "JOB_ASSETS", nullable = false, length = 4000)
    public String getJobAssets() {
        return this.jobAssets;
    }

    public void setJobAssets(String jobAssets) {
        this.jobAssets = jobAssets;
    }   

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "JOB_DEPT_ID",referencedColumnName="MT_DP_IT",nullable = false)
    public MaintainanceDepartment getMaintainanceDepartment() {
        return this.departmentName;
    }

    public void setMaintainanceDepartment(
            MaintainanceDepartment departmentName) {
        this.departmentName = departmentName;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "JOB_TYPE_ID",referencedColumnName="JT_ID",nullable = false)
    public JobTypes getJobTypes() {
        return this.jobTypes;
    }

    public void setJobTypes(JobTypes jobTypes) {
        this.jobTypes = jobTypes;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "MAINTAINANCE_TYPE_ID",nullable = false)
    public MaintainanceTypes getMaintainanceTypes() {
        return this.maintainanceTypes;
    }

    public void setMaintainanceTypes(MaintainanceTypes maintainanceTypes) {
        this.maintainanceTypes = maintainanceTypes;
    }

}

父表是

package com.bcits.bfm.model;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * MaintainanceTypes entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "MAINTAINANCE_TYPES")
public class MaintainanceTypes implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    // Fields

    private int mtId;
    private String maintainanceType;
    private String description;
    private Timestamp mtDt;
    private String createdBy;
    private String lastUpdatedBy;
    private Timestamp lastUpdatedDt;
    private Set<JobCards> jobCardses = new HashSet<JobCards>(0);
    private Set<JobCalender> jobCalenders = new HashSet<JobCalender>(0);


    // Constructors

    /** default constructor */
    public MaintainanceTypes() {
    }

    /** minimal constructor */
    public MaintainanceTypes(int mtId, String maintainanceType, Timestamp mtDt) {
        this.mtId = mtId;
        this.maintainanceType = maintainanceType;
        this.mtDt = mtDt;
    }

    /** full constructor */
    public MaintainanceTypes(int mtId, String maintainanceType,
            String description, Timestamp mtDt, String createdBy,
            String lastUpdatedBy, Timestamp lastUpdatedDt,
            Set<JobCards> jobCardses) {
        this.mtId = mtId;
        this.maintainanceType = maintainanceType;
        this.description = description;
        this.mtDt = mtDt;
        this.createdBy = createdBy;
        this.lastUpdatedBy = lastUpdatedBy;
        this.lastUpdatedDt = lastUpdatedDt;
        this.jobCardses = jobCardses;
    }

    // Property accessors
    @Id
    @Column(name = "MT_ID", unique = true, nullable = false, precision = 11, scale = 0)
    @SequenceGenerator(name = "MAINTENANCE_TYPES_SEQ", sequenceName = "MAINTENANCE_TYPES_SEQ")
    @GeneratedValue(generator = "MAINTENANCE_TYPES_SEQ")
    public int getMtId() {
        return this.mtId;
    }

    public void setMtId(int mtId) {
        this.mtId = mtId;
    }

    @Column(name = "MAINTAINANCE_TYPE", nullable = false, length = 20)
    public String getMaintainanceType() {
        return this.maintainanceType;
    }

    public void setMaintainanceType(String maintainanceType) {
        this.maintainanceType = maintainanceType;
    }

    @Column(name = "DESCRIPTION", length = 200)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "MT_DT", nullable = false, length = 11)
    public Timestamp getMtDt() {
        return this.mtDt;
    }

    public void setMtDt(Timestamp mtDt) {
        this.mtDt = mtDt;
    }

    @Column(name = "CREATED_BY", length = 45)
    public String getCreatedBy() {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name = "LAST_UPDATED_BY", length = 45)
    public String getLastUpdatedBy() {
        return this.lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    @Column(name = "LAST_UPDATED_DT", length = 11)
    public Timestamp getLastUpdatedDt() {
        return this.lastUpdatedDt;
    }

    public void setLastUpdatedDt(Timestamp lastUpdatedDt) {
        this.lastUpdatedDt = lastUpdatedDt;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "maintainanceTypes")
    public Set<JobCards> getJobCardses() {
        return this.jobCardses;
    }

    public void setJobCardses(Set<JobCards> jobCardses) {
        this.jobCardses = jobCardses;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "maintainanceTypes")
    public Set<JobCalender> getJobCalenders() {
        return this.jobCalenders;
    }

    public void setJobCalenders(Set<JobCalender> jobCalenders) {
        this.jobCalenders = jobCalenders;
    }   

}

我在下面调用方法来更新

public void Update(JobCalender jobCalender) {
    update(jobCalender); //update() is generic method

}

public T update(final T t) {

    BfmLogger.logger.info("updating "+t+" instance");
    try {
        T result = this.entityManager.merge(t);
        BfmLogger.logger.info("update successful");
        return result;
    } catch (RuntimeException re) {
        BfmLogger.logger.error("update failed", re);
        throw re;
    }
}

我的控制器代码

@RequestMapping(value = "/index/create", method = RequestMethod.POST)
 public @ResponseBody List<?> create(@RequestBody Map<String, Object>model,@ModelAttribute JobCalender jobCalender,BindingResult bindingResult, HttpServletRequest request) throws ParseException {

            jobCalender.setDescription((String)model.get("description"));
            jobCalender.setTitle((String)model.get("title"));

            SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-M-dd'T'HH:mm:ss.SSS'Z'");
            iso8601.setTimeZone(TimeZone.getTimeZone("UTC"));

            jobCalender.setStart(iso8601.parse((String)model.get("start")));
            jobCalender.setEnd(iso8601.parse((String)model.get("end")));
            jobCalender.setIsAllDay((boolean) model.get("isAllDay"));
            jobCalender.setRecurrenceRule((String)model.get("recurrenceRule"));
            jobCalender.setRecurrenceException((String)model.get("recurrenceException"));
            if(model.get("recurrenceId")!=null)
                jobCalender.setRecurrenceId((Integer)model.get("recurrenceId"));
           jobCalender.setScheduleType(Integer.parseInt(model.get("scheduleType").toString()));

            jobCalender.setJobNumber((String) model.get("jobNumber"));
            jobCalender.setDescription((String) model.get("description"));
            jobCalender.setJobGroup((String) model.get("jobGroup"));
            jobCalender.setMaintainanceDepartment(maintainanceDepartmentService.find(Integer.parseInt(model.get("departmentName").toString())));
            jobCalender.setMaintainanceTypes(maintainanceTypesService.find(Integer.parseInt(model.get("maintainanceTypes").toString())));
            jobCalender.setJobTypes(jobTypesService.find(Integer.parseInt(model.get("jobTypes").toString())));             
            jobCalender.setJobPriority((String) model.get("jobPriority"));
            String ids="";
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> listAssets = (ArrayList<Map<String, Object>>) model.get("assetName");// this is what you have already
            for (Map<String, Object> map1 :listAssets) {
                for (Map.Entry<String, Object> entry : map1.entrySet()) {               
                    if(entry.getKey().equalsIgnoreCase("assetId")){
                        ids+=entry.getValue()+",";
                    }
                }
            }

            jobCalender.setJobAssets(ids);
                    jobCalenderService.Update(jobCalender);
}

更新JOB_CALENDER表时,它为MAINTAINANCE_TYPE_ID列添加NULL值

请帮帮我

谢谢

此致 Manjunath Kotagi

0 个答案:

没有答案