2012-03-29 34 views

Odpowiedz

19

Oto ładne wyjaśnienie od Object DB.

oznacza ManyToOne lub OneToOne atrybut związek, który zapewnia mapowanie dla klucza podstawowego EmbeddedId, atrybut w klucza pierwotnego EmbeddedId lub prosty podstawowego klucza jednostki macierzystej. Element value określa atrybut w kluczu złożonym, któremu odpowiada atrybut relationship. Jeśli klucz podstawowy jednostki jest tego samego typu Java, co klucz podstawowy encji przywoływanej przez relację, atrybut value nie jest określony.

// parent entity has simple primary key 

@Entity 
public class Employee { 
    @Id long empId; 
    String name; 
    ... 
} 

// dependent entity uses EmbeddedId for composite key 

@Embeddable 
public class DependentId { 
    String name; 
    long empid; // corresponds to primary key type of Employee 
} 

@Entity 
public class Dependent { 
    @EmbeddedId DependentId id; 
    ... 
    @MapsId("empid") // maps the empid attribute of embedded id 
    @ManyToOne Employee emp; 
} 

Przeczytaj API Docs tutaj.

1

Znalazłem tę notatkę również użyteczną: @MapsId w adnotacji hibernacji mapuje kolumnę z inną kolumną tabeli.

Może być również używany do udostępniania tego samego klucza głównego między 2 tabelami.

przykład:

@Entity 
@Table(name = "TRANSACTION_CANCEL") 
public class CancelledTransaction { 
    @Id 
    private Long id; // the value in this pk will be the same as the 
        // transaction line from transaction table to which 
        // this cancelled transaction is related 

    @OneToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "ID_TRANSACTION", nullable = false) 
    @MapsId 
    private Transaction transaction; 
    .... 
} 

@Entity 
@Table(name = "TRANSACTION") 
@SequenceGenerator(name = "SQ_TRAN_ID", sequenceName = "SQ_TRAN_ID") 
public class Transaction { 
    @Id 
    @GeneratedValue(generator = "SQ_TRAN_ID", strategy = GenerationType.SEQUENCE) 
    @Column(name = "ID_TRANSACTION", nullable = false) 
    private Long id; 
    ... 
} 
Powiązane problemy