jpa: persistence.xml 配置文件案例

本文介绍了在Java工程中使用JPA时,如何在src/META-INF目录下创建并配置persistence.xml文件。该配置文件用于定义持久化单元,包括设置持久化单元名称、事务管理类型以及指定JPA实现方式。此外,还详细说明了如何配置数据库连接信息,如用户名、密码、驱动和URL,并提供了Hibernate相关的配置选项,如显示SQL和自动创建数据库表的策略。

java工程中, jpa的注配置文件位置:src路径下一个名为META-INF的文件夹,在此文件夹下创建一个名为 persistence.xml 的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <!--需要配置persistence-unit节点
        持久化单元:
            name:持久化单元名称
            transaction-type:事务管理的方式
                    JTA:分布式事务管理
                    RESOURCE_LOCAL:本地事务管理
    -->
    <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">
        <!--jpa的实现方式 -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <!--可选配置:配置jpa实现方的配置信息-->
        <properties>
            <!-- 数据库信息
                用户名,javax.persistence.jdbc.user
                密码,  javax.persistence.jdbc.password
                驱动,  javax.persistence.jdbc.driver
                数据库地址   javax.persistence.jdbc.url
            -->
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="111111"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>

            <!--配置jpa实现方(hibernate)的配置信息
                显示sql           :   false|true
                自动创建数据库表    :  hibernate.hbm2ddl.auto
                        create      : 程序运行时创建数据库表(如果有表,先删除表再创建)
                        update      :程序运行时创建表(如果有表,不会创建表)
                        none        :不会创建表

            -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>
2025-10-27T16:48:58.446+08:00 INFO 17548 --- [chapter06] [ main] book.chapter06.Chapter06Application : Starting Chapter06Application using Java 21.0.1 with PID 17548 (D:\Dev-Cpp\IDER\jebra\daima_java\chapter06\target\classes started by 廾匸 in D:\Dev-Cpp\IDER\jebra\daima_java\chapter06) 2025-10-27T16:48:58.447+08:00 INFO 17548 --- [chapter06] [ main] book.chapter06.Chapter06Application : No active profile set, falling back to 1 default profile: "default" 2025-10-27T16:48:58.777+08:00 INFO 17548 --- [chapter06] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-10-27T16:48:58.807+08:00 INFO 17548 --- [chapter06] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 25 ms. Found 1 JPA repository interface. 2025-10-27T16:48:59.060+08:00 INFO 17548 --- [chapter06] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8081 (http) 2025-10-27T16:48:59.069+08:00 INFO 17548 --- [chapter06] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-10-27T16:48:59.070+08:00 INFO 17548 --- [chapter06] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.48] 2025-10-27T16:48:59.098+08:00 INFO 17548 --- [chapter06] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-10-27T16:48:59.098+08:00 INFO 17548 --- [chapter06] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 619 ms 2025-10-27T16:48:59.192+08:00 INFO 17548 --- [chapter06] [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2025-10-27T16:48:59.216+08:00 INFO 17548 --- [chapter06] [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.6.33.Final 2025-10-27T16:48:59.232+08:00 INFO 17548 --- [chapter06] [ main] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled 2025-10-27T16:48:59.378+08:00 INFO 17548 --- [chapter06] [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer 2025-10-27T16:48:59.394+08:00 INFO 17548 --- [chapter06] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2025-10-27T16:48:59.472+08:00 INFO 17548 --- [chapter06] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@2729bb71 2025-10-27T16:48:59.473+08:00 INFO 17548 --- [chapter06] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2025-10-27T16:48:59.498+08:00 WARN 17548 --- [chapter06] [ main] org.hibernate.orm.deprecation : HHH90000025: MySQL8Dialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default) 2025-10-27T16:48:59.499+08:00 WARN 17548 --- [chapter06] [ main] org.hibernate.orm.deprecation : HHH90000026: MySQL8Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead 2025-10-27T16:48:59.508+08:00 INFO 17548 --- [chapter06] [ main] org.hibernate.orm.connections.pooling : HHH10001005: Database info: Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)'] Database driver: undefined/unknown Database version: 8.0 Autocommit mode: undefined/unknown Isolation level: undefined/unknown Minimum pool size: undefined/unknown Maximum pool size: undefined/unknown 2025-10-27T16:48:59.920+08:00 INFO 17548 --- [chapter06] [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration) 2025-10-27T16:48:59.947+08:00 INFO 17548 --- [chapter06] [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2025-10-27T16:48:59.952+08:00 WARN 17548 --- [chapter06] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService': No qualifying bean of type 'book.chapter06.service.BookService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2025-10-27T16:48:59.952+08:00 INFO 17548 --- [chapter06] [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2025-10-27T16:48:59.953+08:00 INFO 17548 --- [chapter06] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2025-10-27T16:48:59.957+08:00 INFO 17548 --- [chapter06] [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. 2025-10-27T16:48:59.958+08:00 INFO 17548 --- [chapter06] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2025-10-27T16:48:59.964+08:00 INFO 17548 --- [chapter06] [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-10-27T16:48:59.974+08:00 ERROR 17548 --- [chapter06] [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field bookService in book.chapter06.controller.BookController required a bean of type 'book.chapter06.service.BookService' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'book.chapter06.service.BookService' in your configuration.
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值