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.*; 016import static java.lang.annotation.RetentionPolicy.RUNTIME; 017 018/** 019 * Specifies a primary key column that is used as a foreign key to join to another table. 020 * <p> 021 * It is used to join the primary table of an entity subclass in the {@link InheritanceType#JOINED JOINED} 022 * mapping strategy to the primary table of its superclass; it is used within a {@link SecondaryTable} 023 * annotation to join a secondary table to a primary table; and it may be used in a {@link OneToOne} mapping 024 * in which the primary key of the referencing entity is used as a foreign key to the referenced entity. 025 * <p> 026 * If no <code>PrimaryKeyJoinColumn</code> annotation is specified for a subclass in the <code>JOINED</code> 027 * mapping strategy, the foreign key columns are assumed to have the same names as the primary key columns of 028 * the primary table of the superclass. 029 * <p> 030 * <pre> 031 * 032 * Example: Customer and ValuedCustomer subclass 033 * 034 * @Entity 035 * @Table(name="CUST") 036 * @Inheritance(strategy=JOINED) 037 * @DiscriminatorValue("CUST") 038 * public class Customer { ... } 039 * 040 * @Entity 041 * @Table(name="VCUST") 042 * @DiscriminatorValue("VCUST") 043 * @PrimaryKeyJoinColumn(name="CUST_ID") 044 * public class ValuedCustomer extends Customer { ... } 045 * </pre> 046 * 047 * @see SecondaryTable 048 * @see Inheritance 049 * @see OneToOne 050 * @since Java Persistence 1.0 051 */ 052@Target({TYPE, METHOD, FIELD}) 053@Retention(RUNTIME) 054public @interface PrimaryKeyJoinColumn { 055 056 /** 057 * (Optional) The name of the primary key column of the current table. Defaults to the same name as the 058 * primary key column of the primary table of the superclass (<code>JOINED</code> mapping strategy); the 059 * same name as the primary key column of the primary table (<code>SecondaryTable</code> mapping); or the 060 * same name as the primary key column for the table for the referencing entity (<code>OneToOne</code> 061 * mapping). 062 * 063 * @return name 064 */ 065 String name() default ""; 066 067 /** 068 * (Optional) The name of the primary key column of the table being joined to. Defaults to the same name 069 * as the primary key column of the primary table of the superclass (<code>JOINED</code> mapping 070 * strategy); the same name as the primary key column of the primary table (<code>SecondaryTable</code> 071 * mapping); or the same name as the primary key column for the table for the referencing entity ( 072 * <code>OneToOne</code> mapping). 073 * 074 * @return ref col name 075 */ 076 String referencedColumnName() default ""; 077 078 /** 079 * (Optional) The SQL fragment that is used when generating the DDL for the column. This should not be 080 * specified for a <code>OneToOne</code> primary key association. Defaults to the generated SQL to create 081 * a column of the inferred type. 082 * 083 * @return column def 084 */ 085 String columnDefinition() default ""; 086 087 /** 088 * (Optional) The foreign key constraint specification for the join column. This is used only if table 089 * generation is in effect. Default is provider defined. 090 * 091 * @return The foreign key specification 092 */ 093 ForeignKey foreignKey() default @ForeignKey(); 094}