Annotation Interface ManyToOne


@Target({METHOD,FIELD}) @Retention(RUNTIME) public @interface ManyToOne
Specifies a single-valued association to another entity class that has many-to-one multiplicity. It is not usually necessary to specify the associated target entity explicitly since it can usually be inferred from the declared type of the annotated field or property.

A ManyToOne association usually maps a foreign key column or columns. This mapping may be specified using the JoinColumn annotation.

@ManyToOne(optional = false)
@JoinColumn(name = "CUST_ID")
Customer customer;

Alternatively, an optional ManyToOne association is sometimes mapped to a join table using the JoinTable annotation.

@ManyToOne
@JoinTable(name = "ORDER_CUSTOMER",
           joinColumns = @JoinColumn(name="ORD_ID"),
           inverseJoinColumns = @JoinColumn(name="CUST_ID"))
Customer customer;

The annotated field or property might represent one side of a bidirectional association. Every bidirectional association has an owning side and an inverse (alternatively, non-owning or unowned) side. Modifications to the owning side of an association determine the updates made to the relationship in the database. If the inverse side of an association is modified without a corresponding modification to the owning side, the behavior is undefined. The persistence provider is permitted to ignore any modification made only to the inverse side of a bidirectional association.

In a bidirectional association, a OneToMany must be the owning side. The inverse side of a bidirectional ManyToOne association must be a field or property annotated @OneToMany of the target entity, and the non-owning OneToMany side must specify the relationship field or property annotated @ManyToOne via mappedBy. The foreign key or join table must be specified on the owning side.

@Entity
public class Order {
    @Id
    @GeneratedValue
    int id;

    // owning side
    @ManyToOne(optional = false,
               fetch = LAZY)
    @JoinColumn(name = "CUST_ID")
    Customer customer;
    ...
}

@Entity
public class Customer {
    @Id
    @GeneratedValue
    long id;

    // inverse (unowned) side
    @OneToMany(mappedBy = Order_.CUSTOMER)
    @OrderBy
    List<Order> orders;
    ...
}

The ManyToOne annotation may be used within an embeddable class to specify a relationship from the embeddable class to an entity class. If the relationship is bidirectional, the non-owning OneToMany entity side must use the mappedBy element of the OneToMany annotation to specify the relationship field or property of the embeddable field or property on the owning side of the relationship. The dot (.) notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.

@Entity
public class Employee {
    @Id
    int id;

    @Embedded
    Job job;
    ...
}

@Embeddable
public class Job {
    String description;

    // owning side
    @ManyToOne
    ProgramManager manager;
}

@Entity
public class ProgramManager {
    @Id
    int id;

    // inverse (unowned) side
    @OneToMany(mappedBy = "job.manager")
    Collection<Employee> reports;
}
Since:
1.0
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    (Optional) The operations that must be cascaded to the target of the association.
    (Optional) Whether the association should be lazily loaded or must be eagerly fetched.
    boolean
    (Optional) Whether the association is optional.
    (Optional) The entity class that is the target of the association.
  • Element Details

    • targetEntity

      Class<?> targetEntity
      (Optional) The entity class that is the target of the association. Required if the target entity type cannot be inferred from the declared type of the annotated field or property.

      Defaults to the declared type of the annotated field or property.

      Default:
      void.class
    • cascade

      CascadeType[] cascade
      (Optional) The operations that must be cascaded to the target of the association.

      By default, no operations are cascaded.

      Default:
      {}
    • fetch

      FetchType fetch
      (Optional) Whether the association should be lazily loaded or must be eagerly fetched.
      • The EAGER policy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched.
      • The LAZY policy is a hint to the persistence provider runtime.

      When fetch is not explicitly specified, the fetching policy is determined by the default fetch type of the persistence unit for one-to-one and many-to-one associations.

      Default:
      DEFAULT
    • optional

      boolean optional
      (Optional) Whether the association is optional.
      • By default, the association is optional, and the annotated field or property may be set to a null value.
      • When optional=false, a non-null value must be assigned to the non-optional field or property before the persistence provider attempts to write the state of entity instance to the database.

      If the annotated field or property is also annotated @jakarta.annotation.Nonnull, then optional=false is implied, and the value of this annotation member is ignored.

      May be used in schema generation to infer that the mapped foreign key column is not null.

      Default:
      true