错误@OneToOne或@ManyToOne引用未知实体

时间:2018-01-28 02:12:41

标签: jpa

我收到如下错误,我不知道为什么?

  

引起:org.hibernate.AnnotationException:@OneToOne或@ManyToOne   on ##################。model.Orders.customerid引用一个未知的   entity:java.lang.Long

课程订单

@Entity
public class Orders {
  @Id
  @GeneratedValue
  private Long orderid;
  @JoinColumn(name = "customerid", referencedColumnName = "CustomerID")
  @ManyToOne
  private Long customerid;
  @JoinColumn(name = "employeeid", referencedColumnName = "EmployeeID")
  @ManyToOne
  private Long employeeid;
  private java.sql.Timestamp orderdate;
  private java.sql.Timestamp requireddate;
  private java.sql.Timestamp shippeddate;
  @JoinColumn(name = "shipperid", referencedColumnName = "ShipperID")
  @ManyToOne
  private Long shipvia;
  private Double freight;
  private String shipname;
  private String shipaddress;
  private String shipcity;
  private String shipregion;
  private String shippostalcode;
  private String shipcountry;

  @OneToMany(mappedBy="orderid")
  private List<OrderDetails> orderDetails;

类客户

@Entity
public class Customers {
  @Id
  @GeneratedValue
  private Long customerid;
  private String companyname;
  private String contactname;
  private String contacttitle;
  private String address;
  private String city;
  private String region;
  private String postalcode;
  private String country;
  private String phone;
  private String fax;

  @OneToMany(mappedBy="customerid")
  private List<Orders> orders;

此处描述了这两个实体之间的关系 enter image description here

请解释一下我做错了什么?

2 个答案:

答案 0 :(得分:0)

我在提出问题之后找到了原因,我为此给您带来的不便表示歉意。正如您所见,我忘记更改customerid的类型,它应该如下

课程订单

@Entity
public class Orders {
  @Id
  @GeneratedValue
  private Long orderid;
  @JoinColumn(name = "customerid", referencedColumnName = "CustomerID")
  @ManyToOne
  private Customers customerid;
  @JoinColumn(name = "employeeid", referencedColumnName = "EmployeeID")
  @ManyToOne
  private Employees employeeid;
  private java.sql.Timestamp orderdate;
  private java.sql.Timestamp requireddate;
  private java.sql.Timestamp shippeddate;
  @JoinColumn(name = "shipperid", referencedColumnName = "ShipperID")
  @ManyToOne
  private Shippers shipvia;
  private Double freight;
  private String shipname;
  private String shipaddress;
  private String shipcity;
  private String shipregion;
  private String shippostalcode;
  private String shipcountry;

  @OneToMany(mappedBy="orderid")
  private List<OrderDetails> orderDetails;

答案 1 :(得分:0)

我最近遇到此错误,但实体不明。

Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on XXXX.Terminal.location references an unknown entity: XXXX.dc.Location
Feb 06, 2020 11:51:38 AM org.apache.catalina.core.StandardContext listenerStart

就我而言,我必须将此实体的映射添加到“ hibernate.cfg.xml”

<mapping class="XXXX.dc.Location"/>