如何在注释中使用多个组件类型(嵌入,可嵌入)

时间:2016-07-06 08:18:16

标签: hibernate jpa annotations embeddable

我谷歌,但所有示例只在一个对象中显示一个Component,例如:

https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-component-using-annotations-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>

我上面有两个ComponenthomePhonemobilePhone。如何将Hibernate以上的映射转换为Java注释?

1 个答案:

答案 0 :(得分:2)

   include('databaseconfig.php');
   session_start();

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($db,"SELECT nombre FROM pacientes WHERE nombre = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['nombre'];

   if(!isset($_SESSION['login_user'])){
   header('location: www.google.co.uk');
   }

使用@Embeddable

注释您的电话课程

对于每个电话组件,列名称不同。使用@AttributeOverrride并在您实体中指定适当的列名。这会将组件类映射到不同的列。