1.META-INF位置放错
正确的位置应该是在src目录下,而不是和src同级,不然会出现如下错误。
No Persistence provider for EntityManager named firstJPA21
2.数据库方言错误
报错:错误: 找不到或无法加载主类 entity.UserTest
错误原因:数据库方言配置错误。
1.MySQL5的配置方式
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
2.MySQL8的配置方式
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
3.正确代码
3.1数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`birthday` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
3.2项目结构
实体类:User
package entity;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name="User")
@Table(name="user")
public class User {
@Id
private int id;
private String name;
private String password;
private int age;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
META-INF的配置文件代码
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="firstJPA21" transaction-type="RESOURCE_LOCAL">
<class>entity.User</class>
<properties>
<!-- 标准配置方法,适用性高 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value=" jdbc:mysql://localhost:3306/testdb1?useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!-- hibernate 的配置方法-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/> <!--create,create-drop,update,validate -->
</properties>
</persistence-unit>
</persistence>
测试类代码:
package entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class UserTest {
public static void main(String[] args) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("firstJPA21");
EntityManager em = emfactory.createEntityManager();
em.getTransaction().begin();
User user = new User();
user.setName("张三");
em.persist(user);
em.getTransaction().commit();
em.close();
emfactory.close();
}
}
运行效果图:
数据库中添加的数据