实体Bean中的单向关系

实体Bean中的单向关系

本文介绍了实体Bean中的单向关系(JPA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在EJB 3.0 Entity-Beans(JPA)中建立单向关系?

How to make Unidirectional Relationship in EJB 3.0 Entity-Beans (JPA)?

例如,客户了解订单但订单没有客户的任何方法。
使用(@OneToMany或@OneToOne或@ManyToMany)

For example Customer know about Order but Order has not any method for Customer.using (@OneToMany or @OneToOne or @ManyToMany)

问候

推荐答案

以下是使用JPA 2.0制作单向 @OneToMany 关系的方法:

Here's how you'd make a unidirectional @OneToMany relationship using JPA 2.0:

@Entity
public class Customer {
  @Id
  @Column(name="cust_id")
  private long id;
  ...
  @OneToMany
  @JoinColumn(name="owner_id", referencedColumnName="cust_id")
  private List<Order> order;
  ...
}

@Entity
public class Order {
    @Id
    @Column(name="order_id")
    private long id;
    ...
}

关系数据库:

客户:

+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| cust_id | int(11) | NO   | PRI | NULL    |       |
+---------+---------+------+-----+---------+-------+

订单:

+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| order_id | int(11) | NO   | PRI | NULL    |       |
| owner_id | int(11) | NO   | MUL | NULL    |       |
+----------+---------+------+-----+---------+-------+

这篇关于实体Bean中的单向关系(JPA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:28