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)