我是基于弹簧的项目的新手, 我需要使用OneToMany在订单和发票之间创建实体关系映射,并在下面的映射中尝试,但最终会出现映射错误, 你能否指出我解决这个问题。
@Entity
@Table(name="Customers")
public class Customers implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "CustomerId", nullable = false)
private Long CustomerId;
@OneToMany(cascade=CascadeType.ALL, mappedBy="Customers")
private Set<Orders> Orders = new HashSet<Orders>();
}
@Entity
@Table(name="Orders")
public class Orders implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "orderId", nullable = false)
private Long orderId;
@JoinColumn(name="CustomerId")
@ManyToOne
private Customers customers;
@OneToOne (optional=false,cascade=CascadeType.ALL, mappedBy="orders",targetEntity=Invoices.class)
private Invoices invoices;
}
@Entity
@Table(name="Invoices")
public class Invoices implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "invoiceId", nullable = false)
private Long invoiceId;
@OneToOne(optional=false,cascade=CascadeType.ALL, mappedBy="invoices",targetEntity=Orders.class)
private Orders orders;
}
错误讯息: 引起:org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性:com.mycompany.myproject.persist.entity.Customers.Orders中的com.mycompany.myproject.persist.entity.Orders.Customers
答案 0 :(得分:6)
可能是因为Orders有一个属性'customers'而不是'Customers'(由'mappedBy'属性指定)。
你应该整理你的班级名称和字段,如下所示:
@Entity
@Table(name="Customers")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "CustomerId", nullable = false)
private Long customerId;
@OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
private Set<Order> orders = new HashSet<Order>();
}
@Entity
@Table(name="Orders")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "orderId", nullable = false)
private Long orderId;
@ManyToOne
@JoinColumn(name="CustomerId")
private Customer customer;
@OneToOne(optional=false, cascade=CascadeType.ALL, mappedBy="order")
private Invoice invoice;
}
@Entity
@Table(name="Invoices")
public class Invoice implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name = "invoiceId", nullable = false)
private Long invoiceId;
@OneToOne(optional=false,cascade=CascadeType.ALL)
@JoinColumn(name = "InvoiceId")
private Order order;
}