gpt4 book ai didi

java - 如何使用注释修复 Hibernate?

转载 作者:行者123 更新时间:2023-11-30 12:04:24 24 4
gpt4 key购买 nike

在测试 CRUD 期间,我使用了 createQuery() 方法来生成 HQL 查询以从 MySQL 返回数据> 数据库。我的代码带有下划线和错误消息:

Cannot resolve symbol 'Student' This inspection controls whether the Persistence QL Queries are error-checked

在我看来,数据库模型类中的 @Entity 注释存在一些问题。

学生.java

import javax.persistence.*;

@Entity
@Table(name="student")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;

public Student() {

}



QueryStudentDemo.java

    public class QueryStudentDemo {
public static void main(String[] args) {

//create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();

//create session
Session session = factory.getCurrentSession();

try{

//use the session object to save Java object


//begin the transaction
session.beginTransaction();

//query students
List<Student> studentList = session.createQuery("from Student").getResultList();


//display the students
for (Student student: studentList){
System.out.println("Student: " + student);
}

//commit transaction
session.getTransaction().commit();

System.out.println("Transaction done!");


}finally {
factory.close();
}

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&amp;serverTimezone=UTC</property>
<property name="connection.username">hbstudent</property>
<property name="connection.password">hbstudent</property>

<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>

<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>

<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>

<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>


</session-factory>

</hibernate-configuration>

最佳答案

我认为你的hibernate.cfg.xml缺少 <mapping class="your classname" />

-首先更改学生类(class)中的表名,将@Table(name="student") 替换为@Table(name="Student")

  • 第二次使用Session session = factory.openSession();这而不是 Session session = factory.getCurrentSession();

  • List studentList = session.createQuery("from Student").getResultList();将其替换为

查询query = session.createQuery(s);列表 studentList = query.list();之后你可以运行

关于java - 如何使用注释修复 Hibernate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57155086/

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