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.
分享到:
相关推荐
NULL 博文链接:https://webqq.iteye.com/blog/1017529
spring-data-jpa-study
jpa-study
jpa_study
Java ORM标准JPA编程学习项目 教科书: ://www.yes24.com/24/Goods/19040233?Acode = 教科书示例: : 1.项目说明 JDK 1.8标准 嵌入式Tomcat 8.0 Spring Data JPA 2.学习方法 注意:请使用三色笔学习方法( ) ...
Study-JPA-Web
1.枚举类应用 2.主键自增 3.日期 4.二进制 5.复杂查询 6.事务 8.建立索引
SLiPP-JPA-研究 博客 JPA 1. 2. 3. 4. @ 5. 6. 7. Spring Data JPA 1.使用 2。
日本父母项目设置npm install编译和热重装以进行开发npm run serve编译并最小化生产npm run build自定义配置请参阅。
JPA研究 配置 ch02〜ch12 + clean.sh:Java 徒劳无益 H2数据库安装 如果安装H2数据库并尝试直接连接到JDBC URL: jdbc:h2:tcp://localhost/~/test ,则可能会遇到各种错误。 Database "/User/.../test" not found, ...
JH_JAVA_Study:JAVA和JPA学习
Alura-Java和JPA课程:使用JPA2和Hibernate持久化对象 这是我在课程之后开发的代码。 我已经对原始版本进行了一些更改。 内容 JPA和Hibernate简介 使用数据库 面向对象范式 在Java代码中避免使用SQL JDBC和SQL维护...
Springboot + JPA게시판 스프링(게시판) 그인로그인(구글,네이버)을이용하여로그인후사게시판입니다。기능목록 리리스트 。다。 관리 ,수정,있습니제등을있습니다。보정보 版本:JAVA 8,JavaScript(ES5),...
Mac: : MYSQL_USER="githubstudy" \MYSQL_DATABASE="jpa-study" \MYSQL_CONTAINER_NAME="mysql" \MYSQL_ROOT_PASSWORD="githubstudy" \MYSQL_PASSWORD="githubstudy"$ echo $MYSQL_USERdocker \ run \ --detach \...
学习申请SpringBoot Jpa공부참고자료 자바ORM표준JPA프로그래밍책-김영한 개발환경 개발일지
springBoot研究springBoot-study是本人在学习SpringBoot的一些工程!下载地址: : : : 使用说明下载git clone https://github.com/xuwujing/springBoot-study.git使用秉着开箱即用的原则,这些工程只需下载之后,...
学习笔记及代码 1. 笔记 类型 笔记 Java Java基础笔记Java多线程笔记JUC笔记Java集合笔记JavaIO...springboot-demo01 [SpringBoot + Mybatis]springboot-demo02 [SpringBoot + Spring Data JPA]springboot-demo03 [Sp
개발2팀JPA스터디 JPA스시작! JPA스시작!
* jpa (admin) / mybatis (site) * Front (Vue.js) 资料夹设定 * 주차별 과제 - just_subject (피드백 및 발표용) * 주차별 공부+ 테스트 (이슈 생성 및 최종본 완성시 삭제) - just_study (공부용 = 정리 및 비교...
JHipster-脚手架工具(Spring + JPA + Angular + Bootstrap): : 如何确定HTTP状态代码: : 在intellij上的Spring Boot Hot Reload: ://jojoldu.tistory.com/48 Spring Initializr: : 码头工人: : 亚马逊...