A unidirectional many-to-one association is the most common kind of unidirectional association. <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="person_address_id" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class> create table Person ( personId bigint not null primary key, person_address_id bigint not null, CONSTRAINT fk_person_address FOREIGN KEY (persion_address_id) REFERENCES address (addressId)create table Address ( addressId bigint not null primary key ) A unidirectional one-to-one association on a foreign key is almost identical. The only difference is the column unique constraint. <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class> create table Person ( personId bigint not null primary key, addressId bigint not null unique ) create table Address ( addressId bigint not null primary key ) A unidirectional one-to-one association on a primary key usually uses a special id generator. (Notice that we've reversed the direction of the association in this example.) <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> </class> <class name="Address"> <id name="id" column="personId"> <generator class="foreign"> <param name="property">person</param> </generator> </id> <one-to-one name="person" constrained="true"/> </class> create table Person ( personId bigint not null primary key ) create table Address ( personId bigint not null primary key ) A unidirectional one-to-many association on a foreign key is a very unusual case, and is not really recommended. <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses"> <key column="address_person_id" not-null="true"/> <one-to-many class="Address"/> </set> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class> create table Person ( personId bigint not null primary key ) We think it's better to use a join table for this kind of association. |
|
来自: moonboat > 《hibernate》