001/* 002 * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved. 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 006 * which accompanies this distribution. The Eclipse Public License is available 007 * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License 008 * is available at http://www.eclipse.org/org/documents/edl-v10.php. 009 */ 010package javax.persistence; 011 012import java.lang.annotation.Retention; 013import java.lang.annotation.Target; 014 015import static java.lang.annotation.ElementType.FIELD; 016import static java.lang.annotation.RetentionPolicy.RUNTIME; 017import static javax.persistence.FetchType.EAGER; 018 019/** 020 * Defines a single-valued association to another entity class that has many-to-one multiplicity. It is not 021 * normally necessary to specify the target entity explicitly since it can usually be inferred from the type 022 * of the object being referenced. If the relationship is bidirectional, the non-owning <code>OneToMany</code> 023 * entity side must used the <code>mappedBy</code> element to specify the relationship field or property of 024 * the entity that is the owner of the relationship. 025 * <p> 026 * The <code>ManyToOne</code> annotation may be used within an embeddable class to specify a relationship from 027 * the embeddable class to an entity class. If the relationship is bidirectional, the non-owning 028 * <code>OneToMany</code> entity side must use the <code>mappedBy</code> element of the <code>OneToMany</code> 029 * annotation to specify the relationship field or property of the embeddable field or property on the owning 030 * side of the relationship. The dot (".") notation syntax must be used in the <code>mappedBy</code> element 031 * to indicate the relationship attribute within the embedded attribute. The value of each identifier used 032 * with the dot notation is the name of the respective embedded field or property. 033 * <p> 034 * <pre> 035 * 036 * Example 1: 037 * 038 * @ManyToOne(optional=false) 039 * @JoinColumn(name="CUST_ID", nullable=false, updatable=false) 040 * Customer customer; 041 * 042 * 043 * Example 2: 044 * 045 * @Entity 046 * public class Employee { 047 * @Id int id; 048 * @Embedded JobInfo jobInfo; 049 * ... 050 * } 051 * 052 * @Embeddable 053 * public class JobInfo { 054 * String jobDescription; 055 * @ManyToOne ProgramManager pm; // Bidirectional 056 * } 057 * 058 * @Entity 059 * public class ProgramManager { 060 * @Id int id; 061 * @OneToMany(mappedBy="jobInfo.pm") 062 * Collection<Employee> manages; 063 * } 064 * 065 * </pre> 066 * 067 * @since Java Persistence 1.0 068 */ 069@Target({FIELD}) 070@Retention(RUNTIME) 071public @interface ManyToOne { 072 073 /** 074 * (Optional) The entity class that is the target of the association. 075 * <p> 076 * Defaults to the type of the field or property that stores the association. 077 * 078 * @return The target entity 079 */ 080 Class targetEntity() default void.class; 081 082 /** 083 * (Optional) The operations that must be cascaded to the target of the association. 084 * <p> 085 * By default no operations are cascaded. 086 * 087 * @return The cascade type 088 */ 089 CascadeType[] cascade() default {}; 090 091 /** 092 * (Optional) Whether the association should be lazily loaded or must be eagerly fetched. The EAGER 093 * strategy is a requirement on the persistence provider runtime that the associated entity must be 094 * eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime. 095 * 096 * @return The fetch type 097 */ 098 FetchType fetch() default EAGER; 099 100 /** 101 * (Optional) Whether the association is optional. If set to false then a non-null relationship must 102 * always exist. 103 * 104 * @return Whether this is optional 105 */ 106 boolean optional() default true; 107}