某些父实体值无法保存到子实体

时间:2015-06-25 08:54:36

标签: java spring-boot spring-data-jpa

当我保存工作实体时,我期待工作中的孩子也应该反映在数据库中。问题是ref_id和revision不包含db的任何值。

这是mysql db(删除机密数据)的结果

enter image description here

这是我的Job实体类

@Entity
@Table(name = "job")
public class Job implements Serializable {

private static final long serialVersionUID = -2075866246194059832L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;

@Column(name = "ref_id")
private String refId;

@Column(name = "revision")
private int revision;

@Column(name = "appname")
private String appName;

@Column(name = "vendor")

private String vendor;

@Column(name = "version")
private String version;

@Column(name = "locale")
private String locale;

@Column(name = "platform")
private String platform;

@Column(name = "tier")
private String tier;

@Column(name = "category")
private String category;

@Column(name = "functional_tag")
private String functional;

@Column(name = "job_start_date")
private Date jobStartDate;

@Column(name = "author")
private String author;

@Enumerated(EnumType.STRING)
private Status status;

@Column(name = "release_version")
private String releaseVersion;

@OneToMany(mappedBy = "job", cascade = CascadeType.PERSIST)
private List<Task> tasks;
}

这是孩子

@Entity
@Table(name = "task")
public class Task implements Serializable {

private static final long serialVersionUID = -7395753611385528546L;

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;


@Column(name = "module_name")
private String moduleName;

@Column(name = "start_time")
private Date startTime;

@Column(name = "end_time")
private Date endTime;

@Enumerated(EnumType.STRING)
private Status status;

@Column(name = "machine_ip")
private String machineIp;

@Column(name = "data_center")
private String dataCenter;

@Column(name = "description")
private String description;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumns({@JoinColumn(name = "ref_id", referencedColumnName = "ref_id"),
        @JoinColumn(name = "revision", referencedColumnName = "revision")})
private Job job;

}

以下是持久化实体的代码

@Service
public class QReaderService {

@Autowired
private JobRepository jobRepository;
@Autowired
private TaskRepository taskRepository;


public boolean addJob(Job job) {

    Job previousJob = jobRepository.findByJobRefId(job.getRefId());
    if (previousJob == null) {
        **jobRepository.save(job);**
        return true;
    } else {
        switch (job.getStatus()) {
            case FAILED:
            case EXCEPTION:
                int revision = 0;
                revision += previousJob.getRevision();
                previousJob.setStatus(Status.FAILED);
                jobRepository.save(previousJob);
                break;
        }
    }
    return true;
}

}

这就是我构建作业实体的方式

    Job job = new Job();
    job.setRefId("f78d9as7f98dsa7f97a97f98sda9f7");
    job.setAppName("appname");
    job.setLocale("locale");
    job.setPlatform("platform");
    job.setCategory("category");
    job.setReleaseVersion("1.1");
    job.setStatus(Status.PROCESSING);
    job.setAuthor("author");
    job.setFunctional("functional");
    job.setJobStartDate(new Date());
    job.setVersion("1.1");
    job.setTier("tier1");
    job.setVendor("vendor");

    Task task = new Task();
    task.setDescription("description");
    task.setDataCenter("dataCenter");
    task.setStartTime(new Date());
    task.setStatus(Status.PROCESSING);
    task.setMachineIp("ip");
    task.setModuleName("module");
    job.setTasks(new ArrayList<Task>(Arrays.asList(task)));

    jobRepository.save(job);

1 个答案:

答案 0 :(得分:0)

要在父实体上调用save时保持子实体,应设置关系的两端。

job.setTasks(job.setTasks(new ArrayList<Task>(Arrays.asList(task)));

//Missing line //Important for persistence of child
task.setJob(job);

希望这有帮助。