Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
93
Добавлен:
18.03.2015
Размер:
2 Мб
Скачать
☆

Chapter 5. Basic O/R Mapping

</key-many-to-one>

<key-property name="customerNumber"/> </composite-id>

<property name="preferredCustomer"/> </class>

<class name="User">

<composite-id name="id" class="UserId"> <key-property name="firstName"/> <key-property name="lastName"/>

</composite-id>

<property name="age"/> </class>

This is the recommended approach to map composite identifier. The following options should not be considered unless some constraint are present.

5.1.2.1.2. Multiple id properties without identifier type

Another, arguably more natural, approach is to place @Id on multiple properties of your entity. This approach is only supported by Hibernate (not JPA compliant) but does not require an extra embeddable component.

@Entity

class Customer implements Serializable { @Id @OneToOne

@JoinColumns({

@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"), @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")

})

User user;

@Id String customerNumber;

boolean preferredCustomer;

//implements equals and hashCode

}

@Entity

class User {

@EmbeddedId UserId id; Integer age;

}

@Embeddable

class UserId implements Serializable { String firstName;

String lastName;

//implements equals and hashCode

}

78

Identifiers

In this case Customer is its own identifier representation: it must implement Serializable and must implement equals() and hashCode().

In hbm.xml, the same mapping is:

<class name="Customer"> <composite-id>

<key-many-to-one name="user">

<column name="userfirstname_fk"/>

<column name="userlastname_fk"/>

</key-many-to-one>

<key-property name="customerNumber"/> </composite-id>

<property name="preferredCustomer"/> </class>

<class name="User">

<composite-id name="id" class="UserId"> <key-property name="firstName"/> <key-property name="lastName"/>

</composite-id>

<property name="age"/> </class>

5.1.2.1.3. Multiple id properties with with a dedicated identifier type

@IdClass on an entity points to the class (component) representing the identifier of the class. The properties marked @Id on the entity must have their corresponding property on the @IdClass. The return type of search twin property must be either identical for basic properties or must correspond to the identifier class of the associated entity for an association.

Warning

This approach is inherited from the EJB 2 days and we recommend against its use. But, after all it's your application and Hibernate supports it.

@Entity

@IdClass(CustomerId.class)

class Customer implements Serializable { @Id @OneToOne

@JoinColumns({

@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"), @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")

})

User user;

@Id String customerNumber;

79

Chapter 5. Basic O/R Mapping

boolean preferredCustomer;

}

class CustomerId implements Serializable { UserId user;

String customerNumber;

//implements equals and hashCode

}

@Entity

class User {

@EmbeddedId UserId id; Integer age;

//implements equals and hashCode

}

@Embeddable

class UserId implements Serializable { String firstName;

String lastName;

//implements equals and hashCode

}

Customer and CustomerId do have the same properties customerNumber as well as user.

CustomerId must be Serializable and implement equals() and hashCode().

While not JPA standard, Hibernate let's you declare the vanilla associated property in the

@IdClass.

@Entity

@IdClass(CustomerId.class)

class Customer implements Serializable { @Id @OneToOne

@JoinColumns({

@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"), @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")

})

User user;

@Id String customerNumber;

boolean preferredCustomer;

}

class CustomerId implements Serializable { @OneToOne User user;

String customerNumber;

//implements equals and hashCode

}

@Entity

class User {

80

Соседние файлы в папке more