- 浏览: 149260 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
hekuilove:
楼主 介意把你的代码放到<pre name=" ...
JPA一对多,多对多映射 -
308202251:
[size=medium][/size][align=cent ...
usionCharts中文乱码问题 -
勇敢的核桃:
我日,从传~智~播~客上听来的东西原封不动的就转了。。人才!但 ...
搭建JPA开发环境和全局事务介绍 -
w156445045:
怎么使用啊?和log4j一样 写个配置文件还是?
谢谢~
slf4j-1.6.1.zip -
JavaStudyEye:
啥也不说了 很详细,谢谢分享,太好了
Struts2 + JQuery + Json 实例
JPA(Java Persistence API)是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合ORM技术
总的来说,JPA包括以下3方面的技术:
ORM映射元数据
Java持久化API
查询语言
采用JPA的实现hibernate开发必须的包
hibernate核心包hibernate-distribution-3.3.1.GA
hibernate3.jar
lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar
lib\required\*.jar
Hiberante注解包hibernate-annotations-3.4.0.GA
hibernate-annotations.jar
lib\ejb3-persistence.jar、hibernate-commons-annotations.jar
Hibernate针对JPA的实现包hibernate-entitymanager-3.4.0.GA
hibernate-entitymanager.jar
lib\test\log4j.jar、slf4j-log4j12.jar
JPA的配置文件
JPA规范要求在类路径的META-INF目录下放置persistence.xml,文件的名称是固定的,配置模版如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- 供应商 -->
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
JPA事务
RESOURCE_LOCAL本地事务、JTA(全局事务)
持久化API
EntityManager
persist(Object) 持久化
remove(Object) 删除对象
find(Class entityClass,Object key) 根据主键查询
getReference(Class entityClass,Object key)根据主键查询
flush() 实体与底层同步,执行sql
createQuery() 创建JPQL查询对象
createNativeQuery() 根据普通SQL查询
createNamedQuery() 命名查询@NamedQuerie标注
merge(Object) 将一个detached的实体持久化到EntityManager中
close() 关闭管理器
find()与getReference()区别
find()不支持延迟加载,无记录返回NULL
getReference()支持延迟加载,getReference()不会返回null值,而是抛出EntityNotFoundException
javax.persistence.Query
int executeUpdate() 执行更新、删除、添加
Object getSingleResult() 执行查询(返回一条记录)
List getResultList() 执行查询(返回结果链表)
Query setParameter(int position,object value) 给Query对象设置参数
Query setMaxResults(int maxResult) 给Query对象设置返回数 分页查询
Query setFirstResult(int firstResult) 给Query对象设置返回偏移
JPA一对多双向
1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录.
维护端注解
拥有mappedBy注解的实体类为关系被维护端,另外的实体类为关系维护端的。顾名思意,关系的维护端对关系(在多对多为中间关联表)的CRUD做操作。关系的被维护端没有该操作,不能维护关系。
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "order_id")
被维护端注解
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE, CascadeType.REMOVE },
fetch = FetchType.EAGER,
mappedBy = "order")
@Entity
@Table(name = "order_info")
public class OrderInfo {
private Integer id;
private String name;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,
mappedBy = "order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem) {
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
@Entity
@Table(name = "order_item")
public class OrderItem {
private Integer Id;
private String name;
private OrderInfo order;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
@Column(length = 20, nullable = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "order_id")
public OrderInfo getOrder() {
return order;
}
public void setOrder(OrderInfo order) {
this.order = order;
}
}
测试
public class OneToMany {
@Test
public void save() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
OrderInfo o = new OrderInfo();
o.setName("订单一");
OrderItem oi1 = new OrderItem();
oi1.setName("产品一");
o.addOrderItem(oi1);
OrderItem oi2 = new OrderItem();
oi2.setName("产品一");
o.addOrderItem(oi2);
OrderItem oi3 = new OrderItem();
oi3.setName("产品一");
o.addOrderItem(oi3);
OrderItem oi4 = new OrderItem();
oi4.setName("产品一");
o.addOrderItem(oi4);
em.persist(o);
// UUID.randomUUID().toString();
em.getTransaction().commit();
emf.close();
}
}
JPA多对多双向
维护端注解
@ManyToMany (cascade = CascadeType.REFRESH)
@JoinTable (//关联表
name = "student_teacher" , //关联表名
inverseJoinColumns = @JoinColumn (name = "teacher_id" ),//被维护端外键
joinColumns = @JoinColumn (name = "student_id" ))//维护端外键
被维护端注解
@ManyToMany(
cascade = CascadeType.REFRESH,
mappedBy = "teachers",//通过维护端的属性关联
fetch = FetchType.LAZY)
@Entity
public class Student {
private Integer id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false, length = 16)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable(name = "student_teacher", inverseJoinColumns = @JoinColumn(name = "teacher_id"), joinColumns = @JoinColumn(name = "student_id"))
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
public void addTeacher(Teacher teacher) {
this.teachers.add(teacher);
}
public void removeTeachers(Teacher teacher) {
this.teachers.remove(teacher);
}
}
@Entity
public class Teacher {
private Integer id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false, length = 16)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "teachers", fetch = FetchType.LAZY)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
测试
public class ManyToMany {
@Test
public void save() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = new Student();
student.setName("小李");
Teacher teacher = new Teacher();
teacher.setName("大李");
em.persist(student);
em.persist(teacher);
em.getTransaction().commit();
emf.close();
}
@Test
public void bind() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
Teacher teacher = em.find(Teacher.class, 1);
student.addTeacher(teacher);
em.persist(student);
em.getTransaction().commit();
emf.close();
}
@Test
public void unbind() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
Teacher teacher = em.find(Teacher.class, 1);
student.removeTeachers(teacher);
em.persist(student);
em.getTransaction().commit();
emf.close();
}
@Test
public void removeTeacher() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 关系被维护端删除时,如果中间表存在些纪录的关联信息,则会删除失败
em.remove(em.getReference(Teacher.class, 1));
em.getTransaction().commit();
emf.close();
}
@Test
public void removeStudent() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 关系维护端删除时,如果中间表存在些纪录的关联信息,则会删除该关联信息
em.remove(em.getReference(Student.class, 1));
em.getTransaction().commit();
emf.close();
}
}
常见异常
1、异常信息:org.hibernate.hql.ast.QuerySyntaxException: person is not mapped
异常环境:查询
异常原因:查询语句中Person类没有大写
2、java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
异常环境:查询、遍历显示
异常原因:转型出错
3、javax.persistence.NonUniqueResultException: result returns more than one elements
异常环境:查询、getSingleResult
异常原因:getSingleResult只能获取一条数据,而查询语句返回的是多条数据
4、 org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sunyard.entities.Person.name
异常环境:数据插入
异常原因:JPA的Entity中一个属性定义为nullable=false,插入数据该字段为null
5、 执行添加没反应、没异常
异常原因:没有开启事务、没有提交事务
6、javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.sunyard.entities.Person
异常环境:OneToOne 共享主键关联
异常原因:一对一中,一个提供主键、另一个共享其主键,共享主键的对象可以set 提供主键的对象 然后添加到数据库中
方向弄反了 后果就是没人提供主键
7、org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
异常环境:多对一添加
异常原因:在多的一端维护 ,没有添加级联
8、javax.persistence.PersistenceException: [PersistenceUnit: JPA] Unable to configure EntityManagerFactory
异常原因:很多、实体管理器Factory没有成功创建,是注解的问题
9、org.hibernate.MappingException: Unable to find column with logical name: sid in org.hibernate.mapping.
异常环境:添加表做多对一关联映射
异常原因:表字段写反了,name添加表字段名referencedColumnName指向本表字段名
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LJHABC1982/archive/2011/06/20/6556580.aspx
总的来说,JPA包括以下3方面的技术:
ORM映射元数据
Java持久化API
查询语言
采用JPA的实现hibernate开发必须的包
hibernate核心包hibernate-distribution-3.3.1.GA
hibernate3.jar
lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar
lib\required\*.jar
Hiberante注解包hibernate-annotations-3.4.0.GA
hibernate-annotations.jar
lib\ejb3-persistence.jar、hibernate-commons-annotations.jar
Hibernate针对JPA的实现包hibernate-entitymanager-3.4.0.GA
hibernate-entitymanager.jar
lib\test\log4j.jar、slf4j-log4j12.jar
JPA的配置文件
JPA规范要求在类路径的META-INF目录下放置persistence.xml,文件的名称是固定的,配置模版如下:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- 供应商 -->
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
JPA事务
RESOURCE_LOCAL本地事务、JTA(全局事务)
持久化API
EntityManager
persist(Object) 持久化
remove(Object) 删除对象
find(Class entityClass,Object key) 根据主键查询
getReference(Class entityClass,Object key)根据主键查询
flush() 实体与底层同步,执行sql
createQuery() 创建JPQL查询对象
createNativeQuery() 根据普通SQL查询
createNamedQuery() 命名查询@NamedQuerie标注
merge(Object) 将一个detached的实体持久化到EntityManager中
close() 关闭管理器
find()与getReference()区别
find()不支持延迟加载,无记录返回NULL
getReference()支持延迟加载,getReference()不会返回null值,而是抛出EntityNotFoundException
javax.persistence.Query
int executeUpdate() 执行更新、删除、添加
Object getSingleResult() 执行查询(返回一条记录)
List getResultList() 执行查询(返回结果链表)
Query setParameter(int position,object value) 给Query对象设置参数
Query setMaxResults(int maxResult) 给Query对象设置返回数 分页查询
Query setFirstResult(int firstResult) 给Query对象设置返回偏移
JPA一对多双向
1-m:多的一方为关系维护端,关系维护端负责外键纪录的更新,关系被维护端没有权力更新外键纪录.
维护端注解
拥有mappedBy注解的实体类为关系被维护端,另外的实体类为关系维护端的。顾名思意,关系的维护端对关系(在多对多为中间关联表)的CRUD做操作。关系的被维护端没有该操作,不能维护关系。
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "order_id")
被维护端注解
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE, CascadeType.REMOVE },
fetch = FetchType.EAGER,
mappedBy = "order")
@Entity
@Table(name = "order_info")
public class OrderInfo {
private Integer id;
private String name;
private Set<OrderItem> items = new HashSet<OrderItem>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER,
mappedBy = "order")
public Set<OrderItem> getItems() {
return items;
}
public void setItems(Set<OrderItem> items) {
this.items = items;
}
public void addOrderItem(OrderItem orderItem) {
orderItem.setOrder(this);
this.items.add(orderItem);
}
}
@Entity
@Table(name = "order_item")
public class OrderItem {
private Integer Id;
private String name;
private OrderInfo order;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
@Column(length = 20, nullable = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "order_id")
public OrderInfo getOrder() {
return order;
}
public void setOrder(OrderInfo order) {
this.order = order;
}
}
测试
public class OneToMany {
@Test
public void save() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
OrderInfo o = new OrderInfo();
o.setName("订单一");
OrderItem oi1 = new OrderItem();
oi1.setName("产品一");
o.addOrderItem(oi1);
OrderItem oi2 = new OrderItem();
oi2.setName("产品一");
o.addOrderItem(oi2);
OrderItem oi3 = new OrderItem();
oi3.setName("产品一");
o.addOrderItem(oi3);
OrderItem oi4 = new OrderItem();
oi4.setName("产品一");
o.addOrderItem(oi4);
em.persist(o);
// UUID.randomUUID().toString();
em.getTransaction().commit();
emf.close();
}
}
JPA多对多双向
维护端注解
@ManyToMany (cascade = CascadeType.REFRESH)
@JoinTable (//关联表
name = "student_teacher" , //关联表名
inverseJoinColumns = @JoinColumn (name = "teacher_id" ),//被维护端外键
joinColumns = @JoinColumn (name = "student_id" ))//维护端外键
被维护端注解
@ManyToMany(
cascade = CascadeType.REFRESH,
mappedBy = "teachers",//通过维护端的属性关联
fetch = FetchType.LAZY)
@Entity
public class Student {
private Integer id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false, length = 16)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable(name = "student_teacher", inverseJoinColumns = @JoinColumn(name = "teacher_id"), joinColumns = @JoinColumn(name = "student_id"))
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
public void addTeacher(Teacher teacher) {
this.teachers.add(teacher);
}
public void removeTeachers(Teacher teacher) {
this.teachers.remove(teacher);
}
}
@Entity
public class Teacher {
private Integer id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(nullable = false, length = 16)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "teachers", fetch = FetchType.LAZY)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
测试
public class ManyToMany {
@Test
public void save() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = new Student();
student.setName("小李");
Teacher teacher = new Teacher();
teacher.setName("大李");
em.persist(student);
em.persist(teacher);
em.getTransaction().commit();
emf.close();
}
@Test
public void bind() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
Teacher teacher = em.find(Teacher.class, 1);
student.addTeacher(teacher);
em.persist(student);
em.getTransaction().commit();
emf.close();
}
@Test
public void unbind() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
Teacher teacher = em.find(Teacher.class, 1);
student.removeTeachers(teacher);
em.persist(student);
em.getTransaction().commit();
emf.close();
}
@Test
public void removeTeacher() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 关系被维护端删除时,如果中间表存在些纪录的关联信息,则会删除失败
em.remove(em.getReference(Teacher.class, 1));
em.getTransaction().commit();
emf.close();
}
@Test
public void removeStudent() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("JPAPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 关系维护端删除时,如果中间表存在些纪录的关联信息,则会删除该关联信息
em.remove(em.getReference(Student.class, 1));
em.getTransaction().commit();
emf.close();
}
}
常见异常
1、异常信息:org.hibernate.hql.ast.QuerySyntaxException: person is not mapped
异常环境:查询
异常原因:查询语句中Person类没有大写
2、java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
异常环境:查询、遍历显示
异常原因:转型出错
3、javax.persistence.NonUniqueResultException: result returns more than one elements
异常环境:查询、getSingleResult
异常原因:getSingleResult只能获取一条数据,而查询语句返回的是多条数据
4、 org.hibernate.PropertyValueException: not-null property references a null or transient value: com.sunyard.entities.Person.name
异常环境:数据插入
异常原因:JPA的Entity中一个属性定义为nullable=false,插入数据该字段为null
5、 执行添加没反应、没异常
异常原因:没有开启事务、没有提交事务
6、javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.sunyard.entities.Person
异常环境:OneToOne 共享主键关联
异常原因:一对一中,一个提供主键、另一个共享其主键,共享主键的对象可以set 提供主键的对象 然后添加到数据库中
方向弄反了 后果就是没人提供主键
7、org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing:
异常环境:多对一添加
异常原因:在多的一端维护 ,没有添加级联
8、javax.persistence.PersistenceException: [PersistenceUnit: JPA] Unable to configure EntityManagerFactory
异常原因:很多、实体管理器Factory没有成功创建,是注解的问题
9、org.hibernate.MappingException: Unable to find column with logical name: sid in org.hibernate.mapping.
异常环境:添加表做多对一关联映射
异常原因:表字段写反了,name添加表字段名referencedColumnName指向本表字段名
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LJHABC1982/archive/2011/06/20/6556580.aspx
发表评论
-
Persistence.xml 配置说明
2011-06-17 08:25 1673<?xml version="1.0" ... -
hibernate.doc 基础的介绍
2011-05-11 13:12 1000001---Hibernate简介(开源O/R映射框架) 1 ... -
hibernate二级缓存攻略(转)
2011-05-11 12:03 839很多人对二级缓存都不 ... -
Hibernate相关jar包简介
2011-05-11 10:49 872hibernate 的jar包说明 Hibe ... -
hibernate load()和get()完整区别
2011-05-10 11:48 717hibernate中get方法和load方 ... -
hibernate 3.5文档下载
2011-05-09 09:54 790这个是 hibernate 3.5文档下载 。我没有仔细看看。 ... -
hibernate中inverse作用
2011-04-27 23:55 902inverse常用于一对多双向 ... -
hibernate 主键策略
2011-04-23 12:07 711“assigned” 主键由外部程序负责生成,在 sav ... -
hibernate 如何实现延迟加载
2011-01-07 12:14 894延迟加载机制是为了避 ... -
hibernate 延迟加载问题探讨
2011-01-07 11:52 919但就此说一下关于lazy机制: 延迟初始化错误是运用Hibe ...
相关推荐
本篇将深入探讨JPA中的一对一(OneToOne)、一对多(OneToMany)以及多对多(ManyToMany)关系映射。 ### 1. JPA 一对一(OneToOne)关系映射 **概念**:一对一关系意味着两个实体之间存在唯一的关联,一个实体...
在本主题中,我们将深入探讨使用Hibernate注解来实现JPA的一对多、多对多和多对一的关系映射。 1. **一对多关系**: 在现实世界中,一个老师可以教多个学生,或者一个班级可以包含多个学生,这就是典型的一对多...
本文将详细介绍 JPA 中四种常见的映射关系:一对一(One-to-One)、一对多(One-to-Many)、多对一(Many-to-One)以及多对多(Many-to-Many)。 #### 1. OneToOne 关系建立 一对一关系是指两个实体之间的关系,...
**JPA一对多和多对一关系详解** Java Persistence API(JPA)是Java平台上的一个标准,用于处理对象关系映射(ORM),使得开发者可以使用面向对象的方式来操作数据库。在JPA中,实体间的关系映射是核心概念之一,...
双向一对多关系中,一方实体可以拥有多个另一方实体,而另一方实体也知晓这个关系。双方都需要使用`@OneToMany`和`@ManyToOne`注解来定义关系,且`mappedBy`应设置在`@OneToMany`的一侧。 5. **双向多对一**: 相...
为了方便广大开发朋友的需要特整理一下有关jpa映射关系,希望可以帮助到一些朋友。
本示例中的“JPA多对多Demo”是一个具体的实践案例,展示了如何在JPA中实现多对多关联关系,以及对应的增删改查方法。 多对多关联是现实世界中常见的一种关系类型,例如教师和学生之间的关系,一个教师可以教多个...
一、JPA一对多关系 在数据库中,一对多关系意味着一个实体可以与多个其他实体关联。例如,一个学生可以属于多个班级(多对一),而一个班级可以包含多个学生(一对多)。在JPA中,我们通过在实体类上使用`@...
刚学完了hibernate和ejb,自己又自学了下jpa,看了黎活明老师的视频,自己做了个多对多的例子,希望可以对你学习有所帮助,有心人可以联系我QQ770256061!
本资料主要探讨的是JPA中的一对多关系以及延迟加载机制,这两部分是JPA使用中的关键概念。 **一对多关系** 在关系数据库中,一对多关系是最常见的一种关联类型。例如,一个部门可以有多名员工,而每个员工只能属于...
2. **一对多(OneToMany)映射**:一个实体对象可以对应多个子对象。使用`@OneToMany`注解,可以设置`mappedBy`属性,表示关联的反向引用。还可以设置`fetch`策略(EAGER或LAZY),控制关联数据是否在加载主体对象时...
在Java Persistence API (JPA) 中,多对多(ManyToMany)关系是表示两个实体之间复杂关联的一种方式。这种关联允许一个实体实例可以与多个其他实体实例相关联,反之亦然。例如,一个学生可以选修多门课程,一门课程...
JPA支持一对一、一对多、多对一和多对多的关系映射,通过`@OneToOne`、`@OneToMany`、`@ManyToOne`和`@ManyToMany`注解实现。关联关系可以是懒加载或即时加载,通过`fetch`属性配置。 6. **查询语言JPQL**: JPA...
JPA提供了多种关系映射方式,如一对一(@OneToOne)、一对多(@OneToMany)、多对一(@ManyToOne)和多对多(@ManyToMany)。这些关系可以通过`@JoinColumn`和`@JoinTable`注解进行配置。 ### 7. 查询语言 JPA提供...
4. **注解细节**:`@JoinColumn`用于单向一对一或一对多关系,而在多对多关系中,通常使用`@JoinTable`来指定中间表的信息,包括表名、连接列等。 5. **操作关联**:在JPA中,可以通过添加或移除对象到集合属性来...
4. **关系映射(Relationship Mapping)**: JPA也支持处理对象之间的关系,如一对一(OneToOne)、一对多(OneToMany)、多对一(ManyToOne)和多对多(ManyToMany)。例如,一个User可能有多个Order,可以这样映射...
**JPA 2 一对多双向关联关系** Java Persistence API(JPA)是Java平台上的一个标准,用于处理对象关系映射(ORM),使得开发者可以使用面向对象的方式操作数据库。在JPA中,一对多关联关系是常见的实体间关系类型...
本篇文章将深入探讨JPA中的一对多和多对多关系的实现,通过具体的代码示例来帮助理解。 ### 1. 一对多关系 在数据库中,一对多关系意味着一个父实体可以关联多个子实体,而每个子实体只能属于一个父实体。在JPA中...
JPA 提供了多种方式来处理不同类型的关联关系,包括单向多对一、单向一对多、双向一对一、双向多对一和双向多对多。下面我们将详细探讨这些关联关系的实现方法。 1. **单向多对一关联关系(SingleManyToOne)** 在...