jpa的学习和理解

本文探讨了JPA与Hibernate的关系,指出JPA是ORM规范,Hibernate是其实现。JPA与JDBC的主要区别在于JPA提供了面向对象的操作简便性及缓存机制,而JDBC更底层、性能可控。接着介绍了JPA的配置、CRUD操作、持久化策略、主键生成策略以及外键策略。文章还详细阐述了JPA的一级和二级缓存工作原理及其使用场景。
摘要由CSDN通过智能技术生成

JPA的学习和理解

1.jpa和hibernate的关系

jpa:对象关系映射规范
jpa是规范,hibernate是实现,都是为了解决传统jdbc操作复杂,开发效率低而提供的操作数据库的框架

2.jpa和jdbc的区别

jdbc:
本质:处理Java对象和关系型数据库表之间的转换
优点:操作数据库最底层,性能可控
缺点:开发效率低。移植数据库麻烦,没有提供存储数据的缓存,面向SQL语句操作。
jpa:
本质:处理Java对象和关系型数据库表之间的转换,只是对JDBC再次做了一层封装
优点:操作简便,面向持久对象操作,提供缓存(一级缓存、二级缓存、查询缓存),数据库移植性强(数据库方言确定)
缺点:不能干预SQL语句的生成,上亿数据量,不适用jpa,运行效率相对于jdbc更低

3.jpa的配置和CRUD

1.pom.xml配置

     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.itsource</groupId>
<artifactId>jpa-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>jpaday01</module>
</modules>

<dependencies>
    <!-- hibernate的包 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.8.Final</version>
    </dependency>
    <!-- hibernate对于jpa的支持包 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.8.Final</version>
    </dependency>
    <!-- mysql的驱动包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!-- junit的测试包 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
<build>
    <finalName>pss</finalName>
    <plugins>
        <plugin>
            <!-- Maven的编译插件-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

在这里插入图片描述注意事项:
hibernate的核心包与hibernate对jpa的支持包需要版本一致

2.persistence.xml配置

<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_2_0.xsd"
	version="2.0">
	<persistence-unit name="cn.itsource.jpa" transaction-type="RESOURCE_LOCAL">
		<properties>
			<!-- 必须配置4个连接数据库属性 -->
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值