Each EntityManager instance is associated with a persistence context. A persistence context defines the scope under which particular entity instances are created, persisted, and removed.
To obtain an EntityManager instance, inject the entity manager into the application component:
@PersistenceContext
EntityManager em;
Applications create EntityManager instances in this case by using the createEntityManager method of javax.persistence.EntityManagerFactory.
To obtain an EntityManager instance, you first must obtain an EntityManagerFactory instance by injecting it into the application component by means of the javax.persistence.PersistenceUnit annotation:
@PersistenceUnit
EntityManagerFactory emf;Then, obtain an EntityManager from the EntityManagerFactory instance:
EntityManager em = emf.createEntityManager();
The createNamedQuery method is used to create static queries, queries that are defined in metadata using the javax.persistence.NamedQuery annotation. The name element of @NamedQuery specifies the name of the query that will be used with the createNamedQuery method. The query element of @NamedQuery is the query.
@NamedQuery(
name="findAllCustomersWithName",
query="SELECT c FROM Customer c WHERE c.name LIKE :custName"
)
You may alternately use positional parameters in queries, instead of named parameters. Positional parameters are prefixed with a question mark (?) followed the numeric position of the parameter in the query. The Query.setParameter(integer position, Object value) method is used to set the parameter values.
public List findWithName(String name) {
return em.createQuery(
“SELECT c FROM Customer c WHERE c.name LIKE ?1”)
.setParameter(1, name)
.getResultList();
}
type BLOB. BLOB stands for binary large objects, which are used for storing binary data such as an image.
The @Lob annotation is used to denote that the field is large object.
CLOB stands for character large objects, which are used to store string data too large to be stored in a VARCHAR column.
The @Lob annotation is also used here to denote that the field is a large object.
To ensure the proper mapping between sql date types, you must use the @Temporal annotation with the proper temporal type specified in @Temporal’s element. @Temporal’s elements are of type javax.persistence.TemporalType. The possible values are:
DATE, which maps to java.sql.Date | TIME, which maps to java.sql.Time | TIMESTAMP, which maps to java.sql.Timestamp
@Temporal(TIMESTAMP)
public Date getLastUpdate() {
return lastUpdate;
}
A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.
Persistence units are defined by the persistence.xml configuration file. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root.
persistence.xml defines one or more persistence units. The following is an example persistence.xml file.
<persistence>
<persistence-unit name="OrderManagement">
<description>This unit manages orders and customers.
It does not rely on any vendor-specific features and can
therefore be deployed to any persistence provider.
</description>
<jta-data-source>jdbc/MyOrderDB</jta-data-source>
<jar-file>MyOrderApp.jar</jar-file>
<class>com.widgets.Order</class>
<class>com.widgets.Customer</class>
</persistence-unit>
</persistence>
Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in an WAR or EAR file.
If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory.
If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.
If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the JAR file should be located:
In the WEB-INF/lib directory of a WAR.
In the top-level of an EAR file.
In the EAR file’s library directory.
sample
<persistence>
<persistence-unit name="manager1">
<jta-data-source>java:/DefaultDS</jta-data-source>
<jar-file>../MyApp.jar</jar-file>
<class>org.acme.Employee</class>
<class>org.acme.Person</class>
<class>org.acme.Address</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
name
You are required to name your persistence unit. If your persistence classes are within a EJB JAR archive, and it is the only persistence unit defined, then you do not have to reference this name explicitly in your @PersistenceContext and @PersistenceUnit annotations.
jta-data-source, non-jta-data-source
This is the JNDI name of where the javax.sql.DataSource is located. This is ignored when *not* used within an application server. When running outside of an application server, you must specify JDBC connections with Hibernate specific properties (see below). If you're running inside JBoss, put the jndi name of the datasource you defined in the earlier section. Remember to put the "java:/" in front of the jndi name you selected for your datasource.
jar-file and class
The class element specifies a fully qualified classname that you will belong to the persistence unit. The jar-file element specifies another jar you want automatically scanned for @Entity classes. When using jar-file, you must specify a path relative to the jar file the persistence.xml file is in. By default also, the jar the persistence.xml file is placed in is scanned for @Entity classes as well.
properties
The properties element is used to specify vendor specific properties. This is where you will define your JBoss and Hibernate specific configurations.
分享到:
相关推荐
**JPAStudy** Java Persistence API(JPA)是Java平台上的一个标准,用于管理关系数据库中的数据。它是Java EE和Java SE应用程序中的ORM(对象关系映射)解决方案,旨在简化数据库交互并允许开发者以面向对象的方式...
在IT行业中,数据库操作是开发工作中的重要环节。Ibatis3 是一款优秀的持久层框架,它允许开发者将SQL语句直接写在配置文件中,从而...通过阅读和实践《IBatis3Study》中的内容,你将能够更好地理解和应用这些技术。
SLiPP-JPA-Study是针对Java持久层框架Java Persistence API (JPA)进行的深入研究,特别是结合了Spring Data JPA的实践应用。本文将详细介绍JPA的基本概念、核心特性,以及如何在Spring Data JPA中使用这些特性,帮助...
【标题】"jpa-study-0723"是一个关于Java持久层框架JPA(Java Persistence API)的学习项目,创建于2022年7月23日。这个项目旨在帮助开发者深入理解和掌握JPA在实际应用中的使用,通过实践来提升技能。 【描述】...
`.idea`目录是IntelliJ IDEA等IDE的工作区配置,`spring-boot-study000.iml`是项目配置文件。 综上所述,"spring+jpa+全局异常+单元测试"这个主题涵盖了Java后端开发中的核心技术和最佳实践。通过有效地整合这些...
本项目"spring-data-jpa-study"是一个深入学习和实践Spring Data JPA的资源集合,旨在帮助开发者掌握其核心概念和实用技巧。 首先,Spring Data JPA是Spring框架的一部分,它的主要目标是提供一个统一的接口来访问...
Java ORM标准JPA编程学习项目 教科书: ://www.yes24.com/24/Goods/19040233?Acode = 教科书示例: : 1.项目说明 JDK 1.8标准 嵌入式Tomcat 8.0 Spring Data JPA 2.学习方法 注意:请使用三色笔学习方法( ) ...
【标题】"Study-JPA-Web" 指的是一项学习项目,主要关注Java中的Java Persistence API(JPA)在Web开发中的应用。这个项目可能是为了帮助开发者深入了解如何在Web环境中利用JPA来管理和操作数据库。 【描述】"Study...
1.枚举类应用 2.主键自增 3.日期 4.二进制 5.复杂查询 6.事务 8.建立索引
**JPA(Java Persistence API)学习指南** Java Persistence API,简称JPA,是Java平台上的一个标准,用于处理对象关系映射(ORM),使得开发者可以用Java对象来操作数据库记录,而无需关注底层SQL语句。JPA的目标...
**Java Persistence API (JPA)** 是Java平台上的一个标准,用于管理关系数据库中的数据。它为Java开发者提供了一种对象/关系映射工具,将Java类与数据库表关联起来,使得开发者可以使用面向对象的方式来操作数据库。...
总的来说,"JH_JAVA_Study"这个资源包将帮助你系统地掌握Java编程和JPA的使用,无论是对初学者还是有一定经验的开发者,都是非常有价值的参考资料。通过深入学习和实践,你将能够熟练地在Java应用中实现高效的数据...
JPA研究 配置 ch02〜ch12 + clean.sh:Java 徒劳无益 H2数据库安装 如果安装H2数据库并尝试直接连接到JDBC URL: jdbc:h2:tcp://localhost/~/test ,则可能会遇到各种错误。 Database "/User/.../test" not found, ...
SpringBoot-Study--master.zip 是一个包含Bilibili狂神老师关于SpringBoot教程的压缩文件。这个资源提供了全面的SpringBoot代码示例和学习笔记,旨在帮助开发者深入理解和掌握SpringBoot框架,提升Java编程技能。...
此外,还可以学习如何使用SpringBoot实现RESTful API,以及集成SpringData JPA进行数据操作。 5. 进阶学习 除了基础的SpringBoot应用开发,你还可以探索更多高级主题,如: - Spring Cloud:用于构建分布式系统的...
在Spring Boot中,Mybatis可以与Spring Data JPA或JDBC一起使用,提供灵活的数据库操作方案。 6. **Mybatis Generator**: Mybatis Generator是一个代码生成工具,能够根据数据库表结构自动生成对应的Model、...
《스프링과 JPA 기애플리케이션》是关于Spring Boot与Java Persistence API (JPA) 应用的学习资源,由백기선(Baek Gi Seon)编写。本教程旨在帮助开发者深入理解如何在Spring Boot框架中有效使用JPA进行数据持久化...
5. **实体Bean与JPA**:重点讲解实体Bean(现多使用JPA,Java Persistence API)的基础,包括数据持久化的概念、ORM(Object-Relational Mapping)映射策略以及如何使用JPA进行数据库操作。 6. **消息服务(JMS、...
Java持久化API(Java Persistence API,简称JPA)是Java平台上的一个标准,用于管理和持久...在“JPA-Study-master”这个项目中,你可能找到了各种示例代码和测试用例,这些都是加深理解JPA特性和最佳实践的宝贵资源。
Alura-Java和JPA课程:使用JPA2和Hibernate持久化对象 这是我在课程之后开发的代码。 我已经对原始版本进行了一些更改。 内容 JPA和Hibernate简介 使用数据库 面向对象范式 在Java代码中避免使用SQL JDBC和SQL维护...