Один к одному

Ассоцииции по первичному ключу (Primary key assotiations)

one-to-one

Таблица "person":

create table hibernate.person (
    id integer not null auto_increment,
    name varchar(100) not null,
    primary key(id)
);

Таблица "passport":

create table hibernate.passport (
    id integer not null,
    nationality varchar(45) not null,
    date_of_issue date not null
);

one-to-one-uml

Файл "Person.java":

package de.inger;

import java.util.Date;

public class Passport {

    private Integer id;
    private String nationality;
    private Date dateOfIssue;
    private Person person;

    public Passport() {
    }

    public Passport(String nationality, Date dateOfIssue, Person person) {
        setNationality(nationality);
        setDateOfIssue(dateOfIssue);
        setPerson(person);
    }

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

    public Integer getId() {
        return id;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getNationality() {
        return nationality;
    }

    public void setDateOfIssue(Date dateOfIssue) {
        this.dateOfIssue = dateOfIssue;
    }

    public Date getDateOfIssue() {
        return dateOfIssue;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }
}

Файл "Passport.java":

package de.inger;

import java.util.Date;

public class Passport {

    private Integer id;
    private String nationality;
    private Date dateOfIssue;
    private Person person;

    public Passport() {
    }

    public Passport(String nationality, Date dateOfIssue, Person person) {
        setNationality(nationality);
        setDateOfIssue(dateOfIssue);
        setPerson(person);
    }

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

    public Integer getId() {
        return id;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getNationality() {
        return nationality;
    }

    public void setDateOfIssue(Date dateOfIssue) {
        this.dateOfIssue = dateOfIssue;
    }

    public Date getDateOfIssue() {
        return dateOfIssue;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }
}

Файл "mapping.hbm.xml":

<?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>
    <class name="de.inger.dao.Person" table="person">
        <id name="id" type="integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="string">
            <column name="name" />
        </property>
        <one-to-one name="passport" cascade="all" />
    </class>    
    <class name="de.inger.dao.Passport" table="passport">
        <id name="id" type="integer">
            <column name="id" />
            <generator class="foreign">
                <param name="property">person</param>
            </generator>
        </id>
        <property name="nationality" type="string">
            <column name="nationality" />
        </property>
        <property name="dateOfIssue" type="date">
            <column name="date_of_issue" />
        </property>
        <one-to-one name="person" constrained="true" />
    </class>
</hibernate-mapping>

No TrackBacks

TrackBack URL: http://igor-inger.de/mt/mt-tb.cgi/3594

Leave a comment

About this Archive

Find recent content on the main index or look in the archives to find all content.