Hibernate 의 관계 와 설정
public class Address {
private Integer addressid;
private String addressDetail;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
private Address address; // (N:1) (1:1)
//set get...
}
#
DROP TABLE IF EXISTS mytest.address;
CREATE TABLE mytest.address
(
aid INTEGER NOT NULL,
addressdesc VARCHAR (400),
PRIMARY KEY (aid)
);
#
DROP TABLE IF EXISTS mytest.person;
CREATE TABLE mytest.person
(
pid INTEGER NOT NULL,
name VARCHAR (50) NOT NULL,
age TINYINT NOT NULL DEFAULT 0,
address_id INTEGER,
PRIMARY KEY (pid),
KEY address_id (address_id),
CONSTRAINT person_ibfk_1 FOREIGN KEY (address_id) REFERENCES address (aid)
);
<!-- 1. ( )-->
<!-- Person.hbm.xml many:Person one:Address -->
<many-to-one name="address" cascade="all" class="Address" column="address_id"/>
<!-- 2. ( :person_address(person_id,address_id))-->
<!-- Person.hbm.xml many:Person one:Address -->
<join table="person_address">
<key column="person_id"/>
<many-to-one name="address" cascade="all" class="Address" column="address_id"/>
</join>
2. 단 방향 1: 1
public class Address {
private Integer addressid;
private String addressDetail;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
private Address address; // (N:1) (1:1)
//set get...
}
외부 키 기반 단 방향 1: 1
<!-- 1. ( )-->
<!-- Person.hbm.xml many:Person one:Address -->
<many-to-one name="address" unique="true" cascade="all" class="Address" column="address_id"/>
<!-- 2. ( :person_address(person_id,address_id))-->
<!-- Person.hbm.xml many:Person one:Address -->
<join table="person_address">
<key column="person_id"/>
<many-to-one name="address" unique="true" cascade="all" class="Address" column="address_id"/>
</join>
홈 키 기반 단 방향 1: 1
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.Person" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- <generator class="native" /> -->
<generator class="foreign">
<!-- property address -->
<param name="property">address</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!-- ( , , , Address -->
<one-to-one name="address"/>
</class>
</hibernate-mapping>
3. 단 방향 1: N 예 를 들 어 한 사람 이 여러 개 사 는 주소 (부자)
public class Address {
private Integer addressid;
private String addressDetail;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
// , , , N:N 1:N (N:N)
private Set<Address> addresses=new HashSet<Address>();//1:N( N:N)
//set get...
}
<!-- : -->
<!-- Person.hbm.xml -->
<set name="addresses">
<key column="person_id"/>
<one-to-many class="Address" not-found="ignore"/>
</set>
<!-- : (person_address(person_id,address_id))-->
<!-- Person.hbm.xml -->
<set name="addresses" table="person_addesses" cascade="all">
<key column="person_id"/>
<many-to-many class="Address" column="address_id" unique="true"/>
</set>
4. 단 방향 N: N 단 방향 N: N 의 연결 은 중간 표를 사용 해 야 합 니 다.
public class Address {
private Integer addressid;
private String addressDetail;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
// , , , N:N 1:N (N:N)
private Set<Address> addresses=new HashSet<Address>();//1:N( N:N)
//set get...
}
<!-- : (person_address(person_id,address_id))-->
<!-- Person.hbm.xml -->
<set name="addresses" table="person_addesses" cascade="all">
<key column="person_id"/>
<many-to-many class="Address" column="address_id"/>
</set>
5. 양 방향 1: N
public class Address {
private Integer addressid;
private String addressDetail;
private Person person;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
private Set<Address> addresses=new HashSet<Address>();
//set get...
}
<!-- : -->
<!-- Person.hbm.xml -->
<set name="addresses" inverse="true">
<key column="person_id"/>
<one-to-many class="Address"/>
</set>
<!-- Address.hbm.xml -->
<many-to-one name="person" class="Person" column="person_id" not-null="true"/>
<!-- : (person_address(person_id,address_id))-->
<!-- Person.hbm.xml -->
<set name="addresses" inverse="true" table="person_address">
<key column="person_id"/>
<many-to-many column="address_id" class="Address" unique="true"/>
</set>
<!-- Address.hbm.xml -->
<join table="person_address">
<key column="address_id"/>
<many-to-one name="person" column="person_id" not-null="true"></many-to-one>
</join>
</set>
6. 양 방향 N: N 양 방향 N: N 의 연결 은 중간 표를 사용 해 야 합 니 다.
public class Address {
private Integer addressid;
private String addressDetail;
private Set<Person> persons=new HashSet<Person>();
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
private Set<Address> addresses=new HashSet<Address>();
//set get...
}
<!-- Person.hbm.xml -->
<set name="addresses" inverse="true" table="person_address">
<key column="person_id"/>
<many-to-many column="address_id" class="Address"/>
</set>
<!-- Address.hbm.xml -->
<set name="persons" table="peson_addresses">
<key column="address_id"/>
<many-to-many column="person_id" class="Person"/>
</set>
6. 양 방향 1: 1
public class Address {
private Integer addressid;
private String addressDetail;
private Person person;
//set get ...
}
public class Person {
private Integer id;
private String name;
private int age;
private Address address;
//set get...
}
//외부 키 기반 양 방향 1: 1
<!-- 1: -->
<!-- Person.hbm.xml -->
<one-to-one name="address" property/>
<!-- Address.hbm.xml -->
<many-to-one name="person" class="Person" unique="true" column="person_id" not-null="true"/>
<!-- 2: -->
<!-- Person.hbm.xml -->
<join table="person_address" inverse="true">
<key column="person_id" unique="true"/>
<many-to-one name="address" unique="true" class="Address" column="address_id"/>
</join>
<!-- Address.hbm.xml -->
<join table="person_address" optional="true">
<key column="address_id" unique="true"/>
<many-to-one name="person" unique="true" column="person_id" not-null="true"/>
</join>
/ / 메 인 키 기반 양 방향 1: 1
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.Person" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- <generator class="native" /> -->
<generator class="foreign">
<!-- property address -->
<param name="property">address</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<!-- ( , , , Address -->
<one-to-one name="address"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.Address" table="ADDRESS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="addressDetail" type="java.lang.String">
<column name="ADDRESSDETAIL" />
</property>
<one-to-one name="person"/>
</class>
</hibernate-mapping>
, , person , address
.
7. 속성 구성 관련 실체 포함
//
public class Address {
private Integer addressid;
private String addressDetail;
private Set<School> schools=new HashSet<School>();
//set get ...
}
public class School {
private Integer schoolId;
private String schoolname;
//get set
}
public class Person {
private Integer id;
private String name;
private int age;
private Address address; // (N:1) (1:1)
//set get...
}
// person
<!--Person address -->
<component name="address" class="Address">
<parent name="person"/> <!--Address pseron -->
<property name="addressDetail"/>
<set name="schools">
<key column="address_id"/>
<one-to-many class="School"/>
</set>
</component>
8. 복합 키 의 상관 관계
public class Person {
//
private String first;
private String last;
private String name;
private int age;
private Set<Address> addresses=new HashSet<Address>();//1:N( N:N)
//get set
}
public class Address {
private Integer id;
private String addressDetail;
private Person person;
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.Person" table="PERSON">
<composite-id>
<key-property name="first" type="string"/>
<key-property name="last" type="string"/>
</composite-id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<set name="addresses" inverse="true" cascade="all">
<key>
<column name="fisrt"/>
<column name="last"/>
</key>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.Address" table="PERSON">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="addressDetail" type="java.lang.String">
<column name="ADDRESSDETAIL" />
</property>
<many-to-one name="person" class="Person" not-null="true">
<column name="fisrt"/>
<column name="last"/>
</many-to-one>
</class>
</hibernate-mapping>
9. 복합 키 의 멤버 속성 은 관련 실체
public class Order {
private Integer orderId;
private Date orderDate;
private Set<OrderItem> items=new HashSet<OrderItem>();
//get set
}
public class OrderItem {
private Order order;
private Product product;
private int count;
//get set
}
public class Product {
private Integer productId;
private String name;
//get set
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-12-2 22:01:23 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.mapping.OrderItem" table="order_item_info">
<composite-id>
<key-many-to-one name="product" class="Product" column="product_id"/>
<key-many-to-one name="order" class="Order" column="order_id"/>
<key-property name="count" type="int"/>
</composite-id>
</class>
</hibernate-mapping>
10. subclass 로 계승 맵 진행
public class Person {
private Integer id;
private String name;
private char gender;
private Address address;
//get set
}
public class Address {
private String detail;
private String zip;
private String country;
//get set
}
public class Employee extends Person {
private String title;
private double salary;
private Set<Customer> customers=new HashSet<Customer>();
private Manager manager;
//get set
}
public class Customer extends Person {
private String commnets;
private Employee employee;
//get set
}
public class Manager extends Employee {
private String department;
private Set<Employee> employees=new HashSet<Employee>();
//get set
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-1-12 13:35:14 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.extenr.Person" table="PERSON" discriminator-value=" ">
<id name="id" type="java.lang.Integer" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<discriminator column="wawa" type="string"/>
<property name="name" type="java.lang.String" access="field">
<column name="NAME" />
</property>
<property name="gender" type="char" access="field">
<column name="GENDER" />
</property>
<component name="address">
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<subclass name="Employee" discriminator-value=" ">
<property name="title"/>
<property name="salary"/>
<many-to-one name="manager" column="manager_id"/>
<set name="customer" inverse="true">
<key column="employee_id"/>
<one-to-many class="Customer"/>
</set>
<subclass name="Manager" discriminator-value=" ">
<property name="department"/>
<set name="employees" inverse="false">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</subclass>
</subclass>
<subclass name="Customer" discriminator-value=" ">
<property name="comments"/>
<many-to-one name="employee" column="employee_id"/>
</subclass>
<!-- : ,
: ,
-->
</class>
</hibernate-mapping>
11. joined - subclass 로 계승 맵 진행
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-1-12 13:35:14 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.extenr.Person" table="PERSON" discriminator-value=" ">
<id name="id" type="java.lang.Integer" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<discriminator column="wawa" type="string"/>
<property name="name" type="java.lang.String" access="field">
<column name="NAME" />
</property>
<property name="gender" type="char" access="field">
<column name="GENDER" />
</property>
<component name="address">
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<joined-subclass name="Employee">
<key column="employee_id"/>
<property name="title" not-null="true"/>
<property name="salary" not-null="true"/>
<many-to-one name="manager" column="manager_id"/>
<set name="customers" inverse="true">
<key column="employee_id"/>
<one-to-many class="Customer"/>
</set>
<joined-subclass name="Manager">
<key column="manager_id"/>
<property name="department"/>
<set name="employees" inverse="true">
<key column="manager_id"/>
<one-to-many class="Employee"/>
</set>
</joined-subclass>
</joined-subclass>
<joined-subclass name="Customer">
<key column="customer_id"/>
<property name="commnets" not-null="true"/>
<many-to-one name="employee" column="employee_id" not-null="true"/>
</joined-subclass>
</class>
</hibernate-mapping>
<!-- : , , -->
21. 유 니 온 - subclass 를 이용 하여 계승 맵 을 진행한다.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-1-12 13:35:14 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.crazyit.app.domain.extenr.Person" table="PERSON" discriminator-value=" ">
<id name="id" type="java.lang.Integer" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<discriminator column="wawa" type="string"/>
<property name="name" type="java.lang.String" access="field">
<column name="NAME" />
</property>
<property name="gender" type="char" access="field">
<column name="GENDER" />
</property>
<component name="address">
<property name="detail"/>
<property name="zip"/>
<property name="country"/>
</component>
<union-subclass name="Employee" table="employee_inf">
<property name="title" not-null="true"/>
<property name="salary" not-null="true"/>
<many-to-one name="manager" column="manager_id"/>
<set name="customers" inverse="true">
<key column="employee_id"/>
<one-to-many class="Customer"/>
</set>
<union-subclass name="Manager" table="manager_inf">
<property name="department"/>
<set name="employees" inverse="true">
<key column="manage_id"/>
<one-to-many class="Employee"/>
</set>
</union-subclass>
</union-subclass>
<union-subclass name="Customer" table="customer_inf">
<property name="commnents" not-null="true"/>
<many-to-one name="employee" column="employee_id" not-null="true"/>
</union-subclass>
</class>
</hibernate-mapping>
<!--
, , joined-subclass .
union-subclass identity ,sequence,native.
-->
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[JPA] 즉시로딩(EAGER)과 지연로딩(LAZY) (왜 LAZY 로딩을 써야할까?) (1)Proxy는 이 글의 주제인 즉시로딩과 지연로딩을 구현하는데 중요한 개념인데, 일단 원리는 미뤄두고 즉시로딩과 지연로딩이 무엇인지에 대해 먼저 알아보자. 눈 여겨 볼 곳은 'fetch = FetchType.EAGER...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.