JPA HelloWorld
POM 依赖:https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" 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>org.example</groupId> <artifactId>jpa</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.2.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <!-- <dependency> --> <!-- <groupId>com.h2database</groupId> --> <!-- <artifactId>h2</artifactId> --> <!-- </dependency> --> <!-- <dependency> --> <!-- <groupId>org.projectlombok</groupId> --> <!-- <artifactId>lombok</artifactId> --> <!-- <scope>provided</scope> --> <!-- </dependency> --> </dependencies> <repositories> <repository> <id>public</id> <url>https://maven.aliyun.com/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <url>https://maven.aliyun.com/repository/public</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
Book
@Setter @Entity public class Book { @Id String isbn; String title; }
src/main/resources/META-INF/persistence.xml
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd" version="3.0"> <persistence-unit name="persistenceUnitName"> <!-- <class>jpa.entity.Book</class> --> <properties><!-- org.hibernate.cfg.JdbcSettings --> <property name="jakarta.persistence.jdbc.user" value="sa"/> <property name="jakarta.persistence.jdbc.password" value=""/> <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:jpa"/> <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.highlight_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
Main
// 1. 创建 EntitymanagerFactory String persistenceUnitName = "persistenceUnitName"; // Map<String, Object> properites = new HashMap<>(); // properites.put(SHOW_SQL, true); // properites.put(FORMAT_SQL, true); // properites.put(HIGHLIGHT_SQL, TRUE.toString()); // EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properites); EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); // 2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory EntityManager entityManager = entityManagerFactory.createEntityManager(); // 3. 开启事务 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // 4. 进行持久化操作 Book book = new Book(); book.setIsbn("1"); book.setTitle("2"); entityManager.persist(book); // 5. 提交事务 transaction.commit(); // 6. 关闭 EntityManager entityManager.close(); // 7. 关闭 EntityManagerFactory entityManagerFactory.close();
结果
[Hibernate] create table Book ( isbn varchar(255) not null, title varchar(255), primary key (isbn) ) engine=InnoDB [Hibernate] insert into Book (title, isbn) values (?, ?)
https://docs.jboss.org/hibernate/orm/current/introduction/html_single/Hibernate_Introduction.html
https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html
https://hibernate.org/orm/documentation