hibernate_组件映射_annotation方式

hibernate_组件映射_annotation方式

package org.hibernate.tutorial.domain;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Husband {
    private int id;
    private String name;
    private Wife wife;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Embedded
    public Wife getWife() {
        return wife;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setWife(Wife wife) {
        this.wife = wife;
    }

}

package org.hibernate.tutorial.domain;

import javax.persistence.Column;


public class Wife {
    private int id;
    private String name;
    
    @Column(name="wife_id") //注意映射的组件内属性名不应相同
    public int getId() {
        return id;
    }
    @Column(name="wife_name")
    public String getName() {
        return name;
    }
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }

}

package org.hibernate.tutorial.test;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

/**
 * 测试
 * @author MyPC
 *
 */
public class TestHusband {
    @Test
    public void testHusband()
    {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
    }
}

你可能感兴趣的:(hibernate_组件映射_annotation方式)