1. 单向N-1关联映射

2. 单向1-1关联映射

3. 单向1-N关联映射

4. 单向N-N关联映射

5. 双向1-1关联映射

6. 双向1-N关联映射

7. 双向N-N关联映射

1. 单向N-1关联映射(Net Beans创建Java Project, 项目名称:UnidirectionalManyToOne)

Person.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "person_table")
public class Person { //标识属性
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; @ManyToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Person.class)
@JoinColumn(name = "person_id", nullable = false, updatable = false)
private Person person; public Address() {
} public Address(String detail) {
this.detail = detail;
} // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

2. 单向1-1关联映射(Net Beans创建Java Project, 项目名称:UnidirectionalOneToOne)

Person.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { //标识属性
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Person.class)
@JoinColumn(name = "person_id", nullable = false, updatable = false)
private Person person; // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

3. 单向1-N关联映射(Net Beans创建Java Project, 项目名称:UnidirectionalOneToMany)

Person.java

package org.crazyit.model;

import javax.persistence.*;
import java.util.*; @Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; //1-N关联关系,使用Set来保存关联实体
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Address.class)
@JoinColumn(name = "person_id", nullable = false)
private Set<Address> addresses = new HashSet<Address>(); // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

4. 单向N-N关联映射(Net Beans创建Java Project, 项目名称:UnidirectionalManyToMany)

Person.java

package org.crazyit.model;

import javax.persistence.*;
import java.util.*; @Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; //N-N关联关系,使用Set来保存关联实体
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Address.class)
@JoinTable(
name = "person_address",
joinColumns = @JoinColumn(name = "person_id"),
inverseJoinColumns = @JoinColumn(name = "address_id")
)
private Set<Address> addresses = new HashSet<Address>(); // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java

package lee;

import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) {
EntityManager em = emf.createEntityManager(); Person sun = new Person();
sun.setName("sunwukong");
sun.setAge(500); Person zhu = new Person();
zhu.setName("zhubajie");
zhu.setAge(380); Address road = new Address("The road to Indian."); try {
em.getTransaction().begin(); sun.getAddresses().add(new Address("The water hole to the mountain."));
sun.getAddresses().add(road); zhu.getAddresses().add(new Address("Gao Laozhuang."));
zhu.getAddresses().add(road); em.persist(sun);
em.persist(zhu); em.getTransaction().commit();
}
finally {
em.close();
}
}
}

5. 双向1-1关联映射(Net Beans创建Java Project, 项目名称:BidirectionalOneToOne)

Person.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; /* 设置了mappedBy属性表明Person实体不控制关联关系,*/
@OneToOne(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Address.class)
private Address address; // constructor // setter
// getter }

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Person.class)
@JoinColumn(name = "person_id", nullable = false, updatable = false)
private Person person; // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

package lee;

import java.util.List;
import javax.persistence.*; import org.crazyit.model.*; public class JpaQs {
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) {
EntityManager em = emf.createEntityManager(); Person person = new Person();
person.setName("Sun");
person.setAge(500); Address address = new Address();
address.setDetail("Hua Guoshan"); try {
em.getTransaction().begin(); //因为Person实体不控制关联关系,因此让Address实体控制关联关系
address.setPerson(person); em.persist(address);
em.getTransaction().commit();
}
finally {
em.close();
}
}
}

6. 双向1-N关联映射(Net Beans创建Java Project, 项目名称:BidirectionalOneToMany)

Person.java

package org.crazyit.model;

import javax.persistence.*;
import java.util.*; @Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; /* 设置了mappedBy属性表明Person实体不控制关联关系,
因此不能增加@JoinTable和@JoinColumn修饰*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "person", targetEntity = Address.class)
private Set<Address> addresses = new HashSet<Address>(); // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;

@Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; @ManyToOne(fetch = FetchType.EAGER, targetEntity = Person.class, cascade = CascadeType.ALL)
/* 使用@JoinColumn来配置外键列的信息 */
@JoinColumn(name = "person_id", nullable = true)
private Person person; // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

7. 双向N-N关联映射(Net Beans创建Java Project, 项目名称:BidirectionalManyToMany)

Person.java

package org.crazyit.model;

import javax.persistence.*;
import java.util.*; @Entity
@Table(name = "person_table")
public class Person { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personid; private String name;
private int age; @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Address.class)
@JoinTable(
name = "person_address",
joinColumns = @JoinColumn(name = "person_id"),
inverseJoinColumns = @JoinColumn(name = "address_id")
)
private Set<Address> addresses = new HashSet<Address>(); // constructor // setter
// getter
}

Address.java

package org.crazyit.model;

import javax.persistence.*;
import java.util.*; @Entity
@Table(name = "address_table")
public class Address { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int addressid; private String detail; /* 设置了mappedBy属性表明Address实体不控制关联关系,
因此不能增加@JoinTable和@JoinColumn修饰*/
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "addresses", fetch = FetchType.LAZY, targetEntity = Person.class)
private Set<Person> persons = new HashSet<Person>(); // constructor // setter
// getter
}

persistence.xml(参见12.2)

JpaQs.java(参见12.2)

JavaEE(14) - JPA关联映射的更多相关文章

  1. jpa关联映射

    参考:http://www.cnblogs.com/printN/p/6408818.html 官方文档:http://docs.jboss.org/hibernate/orm/5.2/usergui ...

  2. jpa关联映射(一)

    开发中常用到数据表的关联(其实很难遇到..),spring-data-jpa(其实是hibernate)提供了一整套十分方便的注解来供我们使用表关联功能. OneToOne OneToMany Man ...

  3. JavaEE(13) - JPA属性映射

    1. 映射实体的属性 #1. 使用@Transient修饰不想持久保存的Field #2. 使用@Enumerated修饰枚举类型的Field #3. 使用@Lob, @Basic修饰枚举类型的Fie ...

  4. Hibernate关联映射(转载)

    原文:http://www.cnblogs.com/huxi/archive/2009/12/15/1624988.html 以简单的两个类为例: User(int id, String name)  ...

  5. hibernate之关于使用连接表实现多对一关联映射

    [Hibernate]之关于使用连接表实现多对一关联映射 在我们项目使用中採用中间表最多的一般就是多对一,或者是多对多,当然一对一使用中间表也是能够的,可是这样的几率通常少之又少!所以这里重点介绍多对 ...

  6. Hibernate框架--关联映射,一对多,多对多 inverse cascade

    回顾Hibernate: 1. hibernate开发环境搭建 ----> 引入jar: hibernate.jar + required + jpa + 驱动包 ---> hiberna ...

  7. mybatis关联映射一对多

    实际项目中也存在很多的一对多的情况,下面看看这个简单的例子 table.sql CREATE TABLE tb_clazz( id INT PRIMARY KEY AUTO_INCREMENT, CO ...

  8. Hibernatel框架关联映射

    Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...

  9. hibernate多对多关联映射

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

随机推荐

  1. Just4Fun - Comparaison between const and readonly in C#

    /* By Dylan SUN */ Today let us talk about const and readonly. const is considered as compile-time c ...

  2. Oracle安装过程物理内存检查及临时temp空间不足解决办法

    物理内存 – 此先决条件将测试系统物理内存总量是否至少为 922MB (944128.0KB). 预期值 : N/A 实际值 : N/A 错误列表: – 可用物理内存 PRVF-7531 : 无法在节 ...

  3. HDOJ 2665 Kth number

    静态区间第K小....划分树裸题 Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. 深入理解计算机系统之旅(四)处理器(CPU)的体系结构

    1.前言 处理器是很复杂的系统,它不是一蹴而就的,它是经过不断的升级.更新.设计之后的产物,而且如今还在保持着不断的更新. 处理器仅仅能运行一系列的指令,每条指令都仅仅是运行某个简单的操作,比方数字相 ...

  5. Ubuntu 如何重新安裝 Unity ?

    阿舍在刪除 Qt 的時候下錯指令,結果,就把 Unity 給移除掉了,雖然,阿舍從此就知道 Unity 和 Qt 有不分離的關係,不過,就沒有Unity可以用了,這樣阿舍要試東西就不是很方便了哩 ! ...

  6. 修改easyui datebox默认日期格式

    问题描述: 根据jquery easyui datebox demo中给的示例,导入和使用datebox, 发现日期格式为: 6/22/2011, 其他的今天和关闭也是 Today, Close, 对 ...

  7. 使用 WPF 创建预加载控件

    Introduction At the time when WPF applications do a very long process like getting response from a w ...

  8. [Django] Base class in the model layer

    In the model layer, the Model class is the base class while the ModelBase class is metaclass.

  9. base 64 编解码器

    base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...

  10. BZOJ 1324 Exca神剑 最小割

    标题效果:给定一个n*m矩阵.所有的格宝石之子,人们可选择起始位置,后除去宝石的当前位置的周围消失,然后你就可以走两步,重复上述过程 easy发现格儿子把它周围格孩子不能拿 因此,党格访问问题 黑白染 ...