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. Eclipse 在线安装fat jar插件

    在线安装步骤: eclipse菜单栏 help >software updates >Search for new features to install>new update si ...

  2. OpenStack_Swift源代码分析——Ring基本原理及一致性Hash算法

    1.Ring的基本概念 Ring是swfit中最重要的组件.用于记录存储对象与物理位置之间的映射关系,当用户须要对Account.Container.Object操作时,就须要查询相应的Ring文件( ...

  3. OS调度算法常用摘要

    一.常见的批处理作业调度 1.先来先服务调度算法(FCFS):就是依照各个作业进入系统的自然次序来调度作业.这样的调度算法的长处是实现简单,公平. 其缺点是没有考虑到系统中各种资源的综合使用情况,往往 ...

  4. 使用SVM对于许多类型的多维数据分类

    最近,我做了一件小事,使用SVM正确8三维级数据分类,在线搜索,我们发现二分的问题大家都在讨论二维数据,一些决定自己的研究.我首先参考opencvtutorial.这也是二维数据的二分类问题.然后通过 ...

  5. Source Insight 3.X 插件支持utf8,完美解决中国乱码,连接到美丽的轮廓

    上次SI多标签插件之后,由于公司内部编码改为utf8编码,因此特意做了这个Source Insight 3.X utf8插件. 下载地址:http://pan.baidu.com/s/1mgyZous ...

  6. 乐在其中设计模式(C#) - 状态模式(State Pattern)

    原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...

  7. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  8. perl操作sqlserver实现BCP

    #!C:\Perl64\bin #由BCP备份和恢复SQLSERVER指定表 use 5.014; #加载用户和password型材 my $username ; my $passwd; ##得到us ...

  9. 【C语言探索之旅】 第二部分第五课:预处理

    内容简介 1.课程大纲 2.第二部分第五课: 预处理 3.第二部分第六课预告:   创建你自己的变量类型 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语 ...

  10. Visual Studio 使用及调试必知必会

    原文:Visual Studio 使用及调试必知必会   一:C# CODING 技巧 1:TODO 然后 CTRL + W + T,打开任务列表,选中 Comments,就会显示所有待做的任务 2: ...