Most of notes here coming from the book [ Apress Pro EJB 3 JPI]
http://developers.sun.com/learning/javaoneonline/2006/coreenterprise/TS-9056.pdf
?
Persistent Unit
- A set of managed classes that are mapped to a single database and their mapping metadata.
- An entity manager factory and all entity managers obtained from it
- Is defined in the persistence.xml file
-
<persistence xmlns="..." version="1.0">
-
<persistence-unit name="HumanResource"> ... </persistence-unit>
-
</persistence>
Persistent context
-
It represents a collection of entities managed by an EntityManager
-
For each persistent entity identity there is a unique entity instance in a given Persistence
Context
Each persistent context is associated with a persisent unit.
transient vs @Transient
- transient for java object
- @ is for hibernate persistent
ONE TO ONE
One to One Mapping
- unidirection
- use @OneToOne, source: @joinColumn, if need dualDirection, target used @mappedBy(Source Object propertyName)
One to One Mapping - bidirection
- use @OneToOne, @joinColumn + @mappedBy for both sides
One to One mapping - same ID
- source side (having foreight key) use @OneToOne + @PrimaryKeyJoinColumn
- target side use @OneToOne(mappedBy = "..."), as its NOT main side.
One To Many
bidirection
- Many side use @ManyToOne,@JoinColumn
- One Side is main side: @OneToMany(mappedBy="")
- using map, must define @mapKey which is attribute of entity in collection
Many to Many
- Both side has One-To-Many relationship.
- Only way to map is using join-table
- any side can be owner side, owned side must use @mappedBy
- Owner side: use @JoinTable(JoinColumns, inverseJoinColumn)
- Owned side: use @mappedBy
-
uniDirection
: use @OneToMany, similar as @ManToMany, no @mappedBy defined in owned side
Transaction Manager
transaction-scoped
use @PersistentContext(unitName=....), stateless
- ansaction scoped - stateless, container manage for us, use @PersistentContext(unitName=....)
- stateless check transaction and persistence context everytime invoke method
- created by transaction during transaction and closed after
extended
- it will exist with stateful bean, use @PersistenceContext(unitName=".." type="PersistenceContextType.Extened")
, untile the bean is removed
-
limitation
- must coexist with stateful bean
Transaction management -transaction attribute default to "Requried"
If persistent context passed to another EJB call, they can share context, otherwise, can not shared context initialized from caller.
transaction managment types
- resource-local transaction
- java transaction API
transaction synchronization
the process by which presistent context is registed with transaction so that persistence context is notified when transaction commit.
transaction association
act of binding context to a transaction
tranaction propagation
sharing persistence context between multi-container-managed entity manager in a single transaction
Persistence Context Collision
when a statelss bean call a method in stateful bean, container will check if there is active persistence context there, if so, it must be the same as the extended persistence context.
In this case, it finds the one passed from stateless bean, ERROR...
Exception
- when a stateful session bean with extended persistence context create another stateful session bean that also use extended persistence context, the child will inherit parent's context
Differernce application-managed and container managed entity manager
- There is no limit to number of application-managed persistence context associated with transaction
- Only one container-managed persistence context will be associated
Entity manager will joinTransaction if the persistence context was created earlier, NOT created within a transaction.
- Using joinTransaction() to synchronize itself with current JTA transaction
Good practice, never use two persistence context in one transaction, when using application-managed transaction
EntityTransaction is to imitate UserTransaction, difference is that EntityTransaction is implemented in term of transaction method on JDBC Connection interface.
Two things happen if transaction rolls back
- database transaction is rolled back
- persistence context is cleared, detached from managed entity instance, if persistence is transaction scoped, its removed.
As Java memory model is not transactional
, after transaction failed, there might be some changes to entity instances, we need pay attention to the case:
- If there is new entity that uses automatic primary key generation, primary key might be assigned to entity, need clear primary key
- if entity use version field for lock purpose which is automaticly maintained by persistence provider, it might has incorrect value
Thing to note when removing a entity
If the entity to be removed is target of foreign key in another table, the foreign key must be cleared.
Cascade usage
There must be a clear parent-child relationship.
remove only happen at parent side
cascade delete will remove the entity who has relationship with others, so Becareful
flush event
1. if related entity is not managed, it can be new, or relationship where entity to be persisted has foreign key to the detached entity.
分享到:
相关推荐
【标题】"notes_JPA_JSF:使用JSF和JPA实施来重建Notes项目"涉及的是在Java开发环境中,利用JavaServer Faces (JSF) 和 Java Persistence API (JPA) 技术重构建一个名为Notes的项目。JSF是Java EE平台上的一个用户...
openjpa-project / RELEASE-NOTES.html 有关文档和项目信息,请访问我们的项目站点: : 编译中 自己编译Apache OpenJPA的最佳方法是针对默认的derby数据库运行构建。 $> mvn clean install -Dsurefire.excludes....
Spring Boot,MySQL,JPA,Hibernate Rest API教程使用Spring Boot,Mysql,JPA和Hibernate为简单的记笔记应用程序构建Restful CRUD API。要求Java-1.8.x Maven-3.xx MySQL的-5.xx设定步骤1.克隆应用程序git clone ...
Java Enterprise Edition (Java EE) 是企业级应用开发的平台,包含了Servlet、JSP、EJB、JMS、JPA等技术,用于构建分布式、可扩展的应用系统。其中,Servlet和JSP是Web开发的基础,EJB则用于实现业务逻辑组件。 ...
【标题】"cccctyl-java-basic-notes-master_java_" 指的可能是一个关于Java基础学习的项目,其中包含了作者cccctyl对Java编程语言核心概念的深入理解和实践笔记。这个项目特别关注了权限设置和安全模块,这在任何...
标题和描述中提到的"enseignants et étudiants (gestion des notes, annonces)"可以理解为一个教育管理系统,主要用于教师和学生之间的交互,特别是关于成绩管理和公告发布方面。在这个系统中,教师能够方便地记录...
Hibernate是JPA(Java Persistence API)的一个实现,它提供了一套API和工具,用于将Java对象与关系数据库进行映射。这使得开发人员可以专注于业务逻辑,而无需关心底层数据库操作的细节。 二、Hibernate 安装与...
客户管理系统 Java Spring启动项目 系统要求 阿帕奇Tomcat MySQL的 Maven 目前正在进行中: 与Frontend集成 REST API端点 实体 ... " Notes " : String ... " details " : String , //like notes " r
检票口演示 特拉维斯: 一个技术演示,该演示使用 , , , 在Java中实现多用户注释应用程序关于该Webapp提供了按用户备忘的功能,这意味着不同的用户可以登录和创建,阅读,更新和删除备忘。 此外,注释可以按其...
4. **数据库存储(Database Storage)**:一般使用关系型数据库如MySQL或非关系型数据库如MongoDB存储笔记数据,通过JPA或MyBatis等持久层框架进行数据操作。 总结来说,"springboot-notes-app"是一个基于Spring ...
Spring Data JPA或MyBatis可以用来简化数据访问层的操作。你可能需要创建Repository接口,Spring Data会自动生成对应的实现,以执行CRUD操作。 总的来说,"notes_spring"项目将涵盖Web开发中的多个方面,包括但不...
如果对Web开发感兴趣,可以了解Servlet、JSP、EJB、JPA等Java EE技术,它们提供了构建企业级应用的框架和组件。 总之,Java JDK 7学习笔记涵盖了Java编程的基础到高级主题,通过深入学习,不仅可以掌握Java编程的...
Spring Data支持与各种数据存储的集成,如JPA(Java Persistence API)用于ORM(对象关系映射),而Spring MVC则是Web应用开发的强有力选择。 此外,"notes_javautils"可能还包括了对Java集合框架的深入理解,如...
StudyNotes [TOC] Java Java编程思想 Spring in action Java8实战 自己的Java学习笔记 为什么说HashMap是不安全的(即将开坑...) 什么是 APT(annotation processing tool) MARK 翻译 前端 Vue.js实战 HTTP 转载 JPA
【标题】Linux夏令营-Spring Notes-评估 在本次Linux夏令营中,我们重点关注了Spring框架,这是一个广泛使用的Java应用程序开发框架,特别适用于构建企业级应用。Spring以其模块化、灵活的设计以及对依赖注入的支持...
"spring-notes" 可能是一个项目或者教程,用于演示如何在 Spring Boot 中实现基本的 CRUD(创建、读取、更新、删除)操作。 在 Spring Boot 中,我们可以利用 Spring Data JPA 和 Hibernate 这样的 ORM(对象关系...
在本项目中,“android-notes-app-backend”是一个使用Java语言开发的Android笔记应用的后端部分,结合了Spring Boot框架来构建RESTful API服务。这个应用的主要目标是为Android客户端提供数据存储和检索功能,使...
10. **Java EE**:如果深入到企业级开发,Java EE(现为Jakarta EE)是必须了解的,包括Servlet、JSP、JDBC、JPA、EJB等技术,它们构建了现代Web应用程序的基础。 11. **框架**:如Spring、MyBatis、Hibernate等...
11. **Java EE**:如果"java-notes"涉及到Java企业版,那么还可能包含Servlet、JSP、EJB、JMS、JPA等Web开发和企业服务的相关知识。 12. **最新的Java版本特性**:随着Java的不断更新,如Java 8的Lambda表达式、...
11. **Spring框架**:理解Spring的核心特性,如IoC(控制反转)和AOP(面向切面编程),使用Spring Boot快速构建项目,熟悉Spring MVC和Spring Data JPA。 12. **数据库与SQL**:掌握SQL语言,了解关系型数据库理论...