关于JPA报错详解

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();
    }
}


 

运行效果图:

数据库中添加的数据

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单点了

谢谢大佬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值