How to fix Hibernate using annotation?

donrollexx :

During testing CRUD I have used createQuery() method to generate the HQL query to return data from MySQL DB. My code have been underline with error message:

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

In my opinion there is some problem with @Entity annotation in the database model class.

Student.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>

Kamal Kumar :

I think your hibernate.cfg.xml missing <mapping class="your classname" />

-first of all change your table name in student class replace @Table(name="student") to @Table(name="Student")

  • Second use Session session = factory.openSession(); this instead of Session session = factory.getCurrentSession();

  • List studentList = session.createQuery("from Student").getResultList(); replace this with

Query query = session.createQuery(s); List studentList = query.list(); after that you can run

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162432&siteId=1