码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate Component keys

时间:2015-10-24 07:51:26      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:


Wife is a component of Husband class and thus they are stored in one table in DB.

1.Annotation

Husband: put @Embedded before component Wife.

Only need one <mapping class="xxxx.xxx.xxx.Husband"> since wife is part of husband‘s table

 

@Entity
public class Husband {
        private int id;
        private String name;
        private Wife wife;
        
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        
        @Embedded
        public Wife getWife() {
            return wife;
        }
        public void setWife(Wife wife) {
            this.wife = wife;
        }
        
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
}

public class Wife {
    private String wifeName;
    private int age;

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getWifeName() {
        return wifeName;
    }
    public void setWifeName(String name) {
        wifeName = name;
    }
}

 

2.XML

Only Husband needs xml.  

<Component name="xx of getXX()"> and list <property name="">

<hibernate-mapping package="com.hibernate.model">
<class name="Husband">
   <id name="id"></id>
    <property name="name"></property>
    <component name="wife" >
       <property name ="wifeName"></property>
       <property name ="age"></property>
   </component>
</class>

 

 

 

  

Hibernate Component keys

标签:

原文地址:http://www.cnblogs.com/fifi043/p/4906113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!