Spring 数据 JPA:相关实体值插入为 null

Spring Data JPA: related entity value inserted as null

我是 Spring Data JPA 的新手,所以提前感谢任何帮助。

我有一个 class/entity 请求,它与另一个实体分析 (ManyToOne) 相关。我目前正在使用 CrudRepository 插入记录,但是当我插入一个新请求并提供一个分析 ID 时,数据库中的 AnalysisID 列为空。谁能解释为什么?下面是我的代码:

Request.java

@Entity
@Table(name="Requests")
@SequenceGenerator(name="req_seq",sequenceName="req_seq",allocationSize=1)
public class Request {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="req_seq")
    @Column(name="RequestID")
    public long id;
    public String pharmacometrician;
    public String programmer;
    public String project;
    public String study;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="AnalysisID")
    public Analysis analysisType;

    @Column(name="WORKINGFOLDERPATH")
    public String folder;

    protected Request() {
    }

    public Request(String pharmacometrician,String programmer,String project, String study,String folderpath,Analysis analysis) {
        Request request = new Request();
        request.pharmacometrician = pharmacometrician;
        request.programmer = programmer;
        request.project = project;
        request.study = study;
        request.folder = folderpath;
        request.analysisType = analysis;
    }

Analysis.java

@Entity
@Table(name="ANALYSISTYPES")
@SequenceGenerator(name="analysisid_seq",sequenceName="analysisid_seq",allocationSize=1)
public class Analysis {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="analysisid_seq")
    @Column(name="AnalysisID")
    public long id;
    public String label;
    public String description;

    protected Analysis() {
    }


    public Analysis(String label,String description) {
        this.label = label;
        this.description = description;
    }

RequestController.java

@RestController
@RequestMapping("request")
public class RequestController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    RequestRepository repository;
    @Autowired
    AnalysisRepository analysisrepo;

    @RequestMapping("newrequest")
    //@RequestMapping(method=RequestMethod.GET)
    public void newRequest(@RequestParam(value="analysis",required=false) long analysisid,
            @RequestParam(value="pm",required=false) String pharmacometrician,
            @RequestParam(value="programer",required=false) String programmer,
            @RequestParam(value="ta",required=false) long taid,
            @RequestParam(value="purpose",required=false) long purposeid,
            @RequestParam(value="project",required=false) String project,
            @RequestParam(value="study",required=false) String study,
            @RequestParam(value="folder",required=false) String folderpath) {
        log.info("New request");
        repository.save(new Request(pharmacometrician,programmer,project,study,folderpath,analysisrepo.findOne(analysisid)));
        return;
        //return repository.findOne(id);
    }

}

我传入分析 ID,然后使用 AnalysisRepository 获取一个 Analysis 对象,然后我使用它创建新的 Request 并使用 RequestRepository(扩展 CrudRepository)保存到 DB。但是,当我查看数据库时,所有字段都已填充,但 analysisid 列为空。

这些实体是否映射不正确?有什么想法可能是错误的吗?

感谢帮助!

是的,似乎 analysisrepo.findOne 没有返回实例 - 谢谢 Chris。

所以,这有效:

Analysis a = analysisrepo.findOne(analysisid);
repository.save(new Request(pharmacometrician,programmer,project,study,folderpath,a));

不知道为什么这是必要的,但至少它现在起作用了。

谢谢!