如何在 Spring Boot JPA 中加入两个表,我的代码出错了
Posted
技术标签:
【中文标题】如何在 Spring Boot JPA 中加入两个表,我的代码出错了【英文标题】:how to join two table in spring boot JPA , iam getting error with my code 【发布时间】:2018-12-13 04:06:13 【问题描述】:您好,我想从 oracle 加入我的表,如何加入这个表?我正在使用 jpa 在 spring boot 上加入我的表。这是我的代码:
package com.emerio.rnd.bali.oraclemongoservice.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.emerio.rnd.bali.oraclemongoservice.entity.TblmFormManagement;
@Table(name = "TBLM_FORM")
@Entity
public class TblmForm
// @Column(name = "PKID")
@Id
private Long pkid;
// @Column(name = "FORMCODE")
private String formcode;
@OneToOne(cascade=CascadeType.ALL, mappedBy="TblmFormManagement")
private TblmFormManagement tblmFormManagement;
public TblmFormManagement getTblmFormManagement()
return tblmFormManagement;
public void setMapping(TblmFormManagement tblmFormManagement)
this.tblmFormManagement=tblmFormManagement;
// @Column(name = "REPORTITEM")
// private String reportitem;
// @Column(name = "REPORTFIELD")
// private String reportfield;
public TblmForm()
public Long getPkid()
return this.pkid;
public void setPkid(Long pkid)
this.pkid = pkid;
public String getFormcode()
return this.formcode;
public void setFormcode(String formcode)
this.formcode = formcode;
// public String getReportitem()
// return this.reportitem;
//
// public void setReportitem(String reportitem)
// this.reportitem = reportitem;
//
// public String getReportfield()
// return this.reportfield;
//
// public void setReportfield(String reportfield)
// this.reportfield = reportfield;
//
// @OneToOne(mappedBy = "tblmForm")
// public Mapping getMapping()
// return mapping;
//
// public void setBook(Mapping mapping)
// this.mapping = mapping;
//
我的第二个实体:
package com.emerio.rnd.bali.oraclemongoservice.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Table(name = "TBLM_FORMMANAGEMENT")
@Entity
public class TblmFormManagement
// @OneToOne(mappedBy = "mapping")
@Id
private Long pkid;
private String formcode;
private String reportitem;
private String reportfield;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pkid", nullable=true)
private TblmForm tblmForm;
public TblmForm getTblmForm()
return tblmForm;
public void setTblmForm(TblmForm tblmForm)
this.tblmForm = tblmForm;
public TblmFormManagement()
public Long getPkid()
return this.pkid;
public void setPkid(Long pkid)
this.pkid = pkid;
public String getFormcode()
return this.formcode;
public void setFormcode(String formcode)
this.formcode = formcode;
public String getReportitem()
return this.reportitem;
public void setReportitem(String reportitem)
this.reportitem = reportitem;
public String getReportfield()
return this.reportfield;
public void setReportfield(String reportfield)
this.reportfield = reportfield;
// @OneToOne(cascade = CascadeType.ALL)
// @JoinColumn(name = "pkid")
我收到错误:
在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration 中定义名称为“entityManagerFactory”的 bean 创建时出错.class]: init 方法调用失败;嵌套异常是 org.hibernate.AnnotationException: Unknown mappedBy in: com.emerio.rnd.bali.oraclemongoservice.entity.TblmForm.tblmFormManagement,引用属性未知:com.emerio.rnd.bali.oraclemongoservice.entity.TblmFormManagement.Mapping
有什么办法可以解决我的问题吗?谢谢
【问题讨论】:
只需将您的 mappedBy 设置为tblmForm
,就像所有在线 JPA 文档会告诉您的那样。
【参考方案1】:
@OneToOne 映射错误,以下代码适用于 H2 测试数据库:
@Table(name = "TBLM_FORM")
@Entity
public class TblmForm
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long pkid;
// TAKE NOTE that form points to a property in the second entity TblmFormManagement
@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
mappedBy = "form")
private TblmFormManagement tblmFormManagement;
因此第二个实体将是这样的:
public class TblmFormManagement
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long pkid;
private String formcode;
private String reportitem;
private String reportfield;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "form_id", nullable=true)
private TblmForm form;
注意事项:
TblmForm.tblmFormManagement 中的 mappedBy 指向 TblmFormManagement 中的 Java 属性 TblmFormManagement.form 中@JoinColumn 的名称指向 TblmFormManagement 中定义的表的数据库列希望以上几点对你有用。
【讨论】:
在我的数据库中,我没有 form_id 列,form_id 取自哪里? 以上只是一个例子,请确保您有一个用于加入目的的数据库列。【参考方案2】:试试这个。
package com.emerio.rnd.bali.oraclemongoservice.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.emerio.rnd.bali.oraclemongoservice.entity.TblmFormManagement;
@Table(name = "TBLM_FORM")
@Entity
public class TblmForm
@Id
private Long pkid;
@Column(name = "FORMCODE")
private String formcode;
@OneToOne(cascade=CascadeType.ALL, mappedBy="tblmForm")
private TblmFormManagement tblmFormManagement;
public TblmFormManagement getTblmFormManagement()
return tblmFormManagement;
public void setMapping(TblmFormManagement tblmFormManagement)
this.tblmFormManagement=tblmFormManagement;
public Long getPkid()
return this.pkid;
public void setPkid(Long pkid)
this.pkid = pkid;
public String getFormcode()
return this.formcode;
public void setFormcode(String formcode)
this.formcode = formcode;
.
package com.emerio.rnd.bali.oraclemongoservice.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Table(name = "TBLM_FORMMANAGEMENT")
@Entity
public class TblmFormManagement
@Id
private Long pkid;
private String formcode;
private String reportitem;
private String reportfield;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pkid", nullable=true)
private TblmForm tblmForm;
public TblmForm getTblmForm()
return tblmForm;
public void setTblmForm(TblmForm tblmForm)
this.tblmForm = tblmForm;
public TblmFormManagement()
public Long getPkid()
return this.pkid;
public void setPkid(Long pkid)
this.pkid = pkid;
public String getFormcode()
return this.formcode;
public void setFormcode(String formcode)
this.formcode = formcode;
public String getReportitem()
return this.reportitem;
public void setReportitem(String reportitem)
this.reportitem = reportitem;
public String getReportfield()
return this.reportfield;
public void setReportfield(String reportfield)
this.reportfield = reportfield;
// @OneToOne(cascade = CascadeType.ALL)
// @JoinColumn(name = "pkid")
【讨论】:
以上是关于如何在 Spring Boot JPA 中加入两个表,我的代码出错了的主要内容,如果未能解决你的问题,请参考以下文章