我谷歌,但所有的例子只显示我一个Component
在一个对象,例如:
https://dzone.com/tutorials/java/hibernate/hibernate example/hibernate -映射组件-使用-注释- 1. - html
我想在一个对象中有两个Component
,例如:
public class PhoneNumber {
// Phone details
private int areaCode = 0;
private int phoneNumber = 0;
private String name = null;
}
public class Person {
private String firstName = null;
private String nickName = null;
private String lastName = null;
// Multiple phone details
private PhoneNumber homePhone = null;
private PhoneNumber mobilePhone = null;
}
Hibernate映射:
<class name="Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="firstName" column="FIRST_NAME" />
...
<component name="homePhone" class="PhoneNumber">
<property name="areaCode" column="HOME_AREA_CODE"/>
<property name="phoneNumber" column="HOME_PHONE_NUMBER"/>
<property name="name" column="HOME_NAME"/>
</component>
<component name="mobilePhone" class="PhoneNumber">
<property name="areaCode" column="MOBILE_AREA_CODE"/>
<property name="phoneNumber" column="MOBILE_PHONE_NUMBER"/>
<property name="name" column="MOBILE_NAME"/>
</component>
</class>
我上面有两个Component
, homePhone
和mobilePhone
。如何将上述Hibernate
映射转换为Java
注释?
@Embeddable
public class PhoneNumber {
// Phone details
private int areaCode = 0;
private int phoneNumber = 0;
private String name = null;
}
public class Person {
private String firstName = null;
private String nickName = null;
private String lastName = null;
// Multiple phone details
@Embedded
@AttributeOverrides({
@AttributeOverride(name="areaCode",column=@Column(name="HOME_AREA_CODE"),
@AttributeOverride(name="phoneNumber",column=@Column(name="HOME_PHONE_NUMBER"),
@AttributeOverride(name="name",column=@Column(name="HOME_NAME")})
private PhoneNumber homePhone = null;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="areaCode",column=@Column(name="MOBILE_AREA_CODE"),
@AttributeOverride(name="phoneNumber",column=@Column(name="MOBILE_PHONE_NUMBER"),
@AttributeOverride(name="name",column=@Column(name="MOBILE_NAME")})
private PhoneNumber mobilePhone = null;
}
用@Embeddable注释你的Phone类
对于每个电话组件,列名不同。使用@ attributeoverride并在您拥有的实体中指定适当的列名。这将把组件类映射到不同的列。