gpt4 book ai didi

java - 如何使用 java 模型正确设置 hibernate 持久性

转载 作者:行者123 更新时间:2023-12-02 00:05:01 26 4
gpt4 key购买 nike

我正在尝试编写一个 REST API 来与 MariaDB 交互。我觉得我已经完成了大部分工作,但是我收到了以下错误:

    java.lang.IllegalArgumentException: Unknown entity: Models.User
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:808)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
at test.Application.addUser(Application.java:40)
at test.Application.main(Application.java:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:542)
at java.base/java.lang.Thread.run(Thread.java:834)

我想这是因为我在 resources/META-INF 的 persistence.xml 文件中错误地配置了用户类。以下是相关的相互关联文件:

持久性.xml

     <?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0"
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">
<persistence-unit name="potholeAPI" transaction-
type="RESOURCE_LOCAL">



<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>Models.User</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/PotholeDB?

useUnicode=true&amp;useJDBCCompliantTimezoneShift
=true&amp;useLegacyDate
timeCode=false&amp;serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="admin"/>
<property name="javax.persistence.jdbc.password" value="admin"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

</properties>
</persistence-unit>

用户.java

    package Models;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Table(appliesTo = "Users")
public class User implements Serializable{

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", unique = true)
private int id;

@Column(name = "name", unique = true)
private String name;

@Column(name = "created_date")
private int created_date;

getters and setters....

以及我测试的主要部分和驱动程序应用程序.java

    package test;

import Models.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@SpringBootApplication
public class Application {
private static EntityManagerFactory ENTITY_MANAGER_FACTORY =
Persistence.createEntityManagerFactory("potholeAPI");

public static void main(String[] args) {
SpringApplication.run(Application.class, args);

addUser(1, "jim");
addUser(2, "bob");

getUser(1);
getUsers();


ENTITY_MANAGER_FACTORY.close();
}

public static void addUser(int id, String name) {
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
EntityTransaction et = null;
Date date = new Date();
try {
et = em.getTransaction();
et.begin();
User user = new User();
user.setId(id);
user.setName(name);
user.setCreated_date(date.getDate());
em.persist(user);
} catch (Exception ex) {
if (et != null) {
et.rollback();
}
ex.printStackTrace();
} finally {
em.close();
}
}

public static void getUser(int id) {
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
String query = "SELECT u FROM User u WHERE u.id = :userID";

TypedQuery<User> tq = em.createQuery(query, User.class);
tq.setParameter("userID", id);
User user = null;
try {
user = tq.getSingleResult();
System.out.println(user.getName() + " ");
} catch (NoResultException e) {
System.out.println("ex");
e.printStackTrace();
} finally {
em.close();
}
}

public static void getUsers() {
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();
String query = "SELECT u FROM User u WHERE u.id IS NOT NULL";

TypedQuery<User> tq = em.createQuery(query, User.class);
List<User> users;
try {
users = tq.getResultList();
users.forEach(user-> System.out.println(user.getName()+ " "));
} catch (NoResultException e) {
System.out.println("ex");
e.printStackTrace();
} finally {
em.close();
}

}

}

我怀疑我搞砸了 xml 文件,但我对构建这样的 API 还很陌生,所以我并不完全有信心。

最佳答案

表名是'User',所以User.java->@Table(appliesTo = "Users")应该是@Table(appliesTo = "User")

关于java - 如何使用 java 模型正确设置 hibernate 持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58160914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com