简体   繁体   English

实体Bean中的单向关系(JPA)

[英]Unidirectional Relationship in Entity-Bean (JPA)

How to make Unidirectional Relationship in EJB 3.0 Entity-Beans (JPA)? 如何在EJB 3.0 Entity-Beans(JPA)中创建单向关系?

For example Customer know about Order but Order has not any method for Customer. 例如,客户了解订单,但订单没有客户的任何方法。 using (@OneToMany or @OneToOne or @ManyToMany) 使用(@OneToMany或@OneToOne或@ManyToMany)

Regards 问候

Here's how you'd make a unidirectional @OneToMany relationship using JPA 2.0: 以下是使用JPA 2.0创建单向@OneToMany关系的方法:

@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;
    ...
}

Relational database: 关系型数据库:

Customer: 顾客:

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

Order: 订购:

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

Leaving "uni-directional" aside for the moment, one could model a Customer-Order relationship as follows. 暂时搁置“单向”,可以如下建立客户 - 订单关系的模型。

@Entity
public class Customer {
  // ...
  @Id @GeneratedValue
  int id;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
  Set<Order> orders;
  // ...
}

@Entity
public class Order {
  // ...
  @ManyToOne(optional = false)
  Customer customer;
  // ...
}

Here I'm assuming that each Order has exactly one Customer. 在这里,我假设每个订单只有一个客户。 In the database, the Order table would have a "customer_id" column with a foreign key to the Customer table's "id" column. 在数据库中,Order表将具有“customer_id”列,其中包含Customer表的“id”列的外键。 The DDL would look something like the following. DDL看起来如下所示。

CREATE TABLE Customer (
  id INT NOT NULL,
  ...
  PRIMARY KEY (id)
);

CREATE TABLE Order (
  ...
  customer_id INT NOT NULL,
  ...
  FOREIGN KEY (customer_id) REFERENCES Customer (id)
);

Although the Customer class contains a collection of orders, this doesn't actually affect the database structure in any way; 尽管Customer类包含一组订单,但这实际上并不会以任何方式影响数据库结构。 it's just a convenient way of retrieving/managing orders that belong to the customer. 它只是检索/管理属于客户的订单的便捷方式。 For example, you can drop the "orders" member from Customer altogether and rely on queries to fetch these records instead. 例如,您可以完全从客户中删除“orders”成员,并依赖查询来获取这些记录。

The point I'm trying to make is that, from the perspective of the database , there really is no such thing as a "uni-directional" relationship. 我想说的是,从数据库的角度来看,确实没有“单向”关系。 The example reverendgreen provided will produce Java classes where there is no direct way to get a Customer object from an Order object, but the resulting database structure will be identical (ignoring minor differences in column names). 提供的示例reverendgreen将生成Java类,其中没有直接的方法从Order对象获取Customer对象,但生成的数据库结构将是相同的(忽略列名称的微小差异)。 You can always find the customer for a given order by means of a query. 您始终可以通过查询找到给定订单的客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM