Hibernate的組件(Component)映射

來源:互聯網
上載者:User

標籤:

  在學習Hibernate架構時碰到了許多實際的問題,比如一個Name是由FirstName和LastName組成,或者說一個Address是有Province,City,County和The detailed address組成,那麼這麼複雜的屬性作為實際設計中是不應該出現在一個類裡,那麼組件映射就出現了

  下面展示組件映射的執行個體,User對象中的Name屬性

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.dao">    <class name="User" table="User">        <id name="id">            <generator class="native" />        </id>         <component name="name">            <property name="firstname" column="first_name"/>            <property name="lastname" column="last_name" />        </component>                <property name="birthday" />    </class></hibernate-mapping>

 

User.java代碼

 1 package com.dao; 2  3 import java.util.Date; 4  5 public class User { 6     private int id; 7     private Name name; 8     private Date birthday; 9 10     public void setName(Name name) {11         this.name = name;12     }13 14     public int getId() {15         return id;16     }17 18     public void setId(int id) {19         this.id = id;20     }21 22     public Name getName() {23         return name;24     }25 26     public Date getBirthday() {27         return birthday;28     }29 30     public void setBirthday(Date birthday) {31         this.birthday = birthday;32     }33 }

 

Name的代碼

package com.dao;public class Name {    private String firstname;    private String lastname;    public String getFirstname() {        return firstname;    }    public void setFirstname(String firstname) {        this.firstname = firstname;    }    public String getLastname() {        return lastname;    }    public void setLastname(String lastname) {        this.lastname = lastname;    }}

 

Hibernate的組件(Component)映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.