Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unidirectional Relationship in Entity-Bean (JPA)

Tags:

java

orm

jpa

How to make Unidirectional Relationship in 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)

Regards

like image 744
Nav Avatar asked Aug 18 '10 19:08

Nav


People also ask

What are the different types of relationships in JPA?

JPA Relationship TypesEditManyToOne - A reference from one object to another, inverse of a OneToMany . OneToMany - A Collection or Map of objects, inverse of a ManyToOne . ManyToMany - A Collection or Map of objects, inverse of a ManyToMany . Embedded - A reference to a object that shares the same table of the parent.

Can one-to-many be unidirectional?

Solution: As I explained in great details in my article about best practices for one-to-many association mappings, Hibernate handles unidirectional one-to-many mappings very inefficiently. You should, therefore, try to avoid them and use a bidirectional one-to-many/many-to-one mapping instead.

What is unidirectional and bidirectional mapping in hibernate?

If you have a unidirectional @OneToMany association, it means you can only access the relationship from the parent side which manages the foreign key. For the bidirectional @OneToMany association, you can navigate the association in both ways, either from the parent or from the child side.

What is unidirectional mapping in hibernate?

In Many-To-One Unidirectional mapping, one table has a foreign key column that references the primary key of associated table.By Unidirectional relationship means only one side navigation is possible (STUDENT to UNIVERSITY in this example). We are discussing an example of Student and University relationship.


2 Answers

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

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    |       |
+----------+---------+------+-----+---------+-------+
like image 89
Jordan Allan Avatar answered Sep 21 '22 02:09

Jordan Allan


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. The DDL would look something like the following.

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

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). You can always find the customer for a given order by means of a query.

like image 22
Jeff Caulfield Avatar answered Sep 19 '22 02:09

Jeff Caulfield