Java Persistence/ManyToOne

=ManyToOne=

A  relationship in Java is where the source object has an attribute that references another object, the target object. I.e. the rather typical Java case that one object holds a reference to another object. A  relationship can be specified unidirectional. However, it is typical that the target object has the inverse relationship specified back to the source object. This would be a  relationship specification in the target object. All relationships in Java and JPA are unidirectional, in that if a source object references a target object there is no guarantee that the target object also has a relationship to the source object. This is different than a relational database, in which relationships are defined through foreign keys and querying such that the inverse query always exists.

In JPA a  relationship is specified through the   annotation or the   element. A  annotation is typically accompanied by a   annotation. The  annotation specifies how the relationship should be mapped to (expressed in) the database. The  defines the name of the foreign key column  in the source object that should be used to find (join) the target object.

If the reverse  relationship is specified in the target object, then the   annotation in the target object must contain a   attribute to define this inverse relation.

JPA also defines a  relationship, which is similar to a   relationship, except that the inverse relationship (if it were defined) is a   relationship. The main difference between a  and a   relationship in JPA is that a   always contains a foreign key from the source object's table to the target object's table, whereas a   relationship the foreign key may either be in the source object's table or the target object's table.

Example of a ManyToOne relationship database
EMPLOYEE (table)

PHONE (table)

Foreign key is also part of the primary key.

 * See Primary Keys through OneToOne Relationships.

Foreign key is also mapped as a basic.

 * If you use the same field in two different mappings, you typically require to make one of them read-only using.
 * See Target Foreign Keys, Primary Key Join Columns, Cascade Primary Keys.

Constraint error on insert.

 * This typically occurs because you have incorrectly mapped the foreign key in a  relationship.
 * See Target Foreign Keys, Primary Key Join Columns, Cascade Primary Keys.


 * It can also occur if your JPA provider does not support referential integrity, or does not resolve bi-directional constraints. In this case you may either need to remove the constraint, or use     to ensure the order your objects are written in.

Foreign key value is null

 * Ensure you set the value of the object's, if the   is part of a bi-directional   relationship, ensure you set the object's   when adding an object to the  , JPA does not maintain bi-directional relationships for you.
 * Also check that you defined the  correctly, ensure you did not set   or use a.

=Advanced=

Target Foreign Keys, Primary Key Join Columns, Cascade Primary Keys
In complex data models it may be required to use a target foreign key, or read-only  in mapping a   if the foreign key/  is shared with other   or   mappings.

See, Target Foreign Keys, Primary Key Join Columns, Cascade Primary Keys

/Mapping