簡體   English   中英

請幫我找到這個錯誤。 我正在使用 hibernate 5.6.4 和 MySQL 8.0.29

[英]Please help me to Find this error. I'm using hibernate 5.6.4 with MySQL 8.0.29

錯誤

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) at org.hibernate.internal.ExceptionConverterImpl. convert(ExceptionConverterImpl.java:181) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1406) at org.hibernate.internal.SessionImpl .managedFlush(SessionImpl.java:493) 在 org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.Z93F725A072423FE1C886ZD43) at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2420) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:449) at org.hibernate.resource.transaction.backend.jdbc .internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40) at org.hibernate.resource.transaction.backend. jdbc.internal.JdbcResourceLocalT ransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281) at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101) at com.tapacad.application.Program2.main(Program2.java:34) Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java :37) 在 org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.Z93F725A07423FE1 C889F448B33D21F46Z:113) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200) at org.hibernate .engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3375) at org.hibernate.persister.entity.AbstractEntityPersister.insert (AbstractEntityPersister.java:3908) 在 org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.Z93F725A07423FE1C88 9F448B33D21F46Z:107) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478) at java.base/ java.util.LinkedHashMap.forEach(LinkedHashMap.java:721) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java: 344) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40) at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(Ev entListenerGroupImpl.java:107) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1402)... 9 more Caused by: java.sql.SQLSyntaxErrorException: Table 'hibernatedb.student' doesn't exist at com. mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.在 Z4D236D9A2D102C5F 執行內部(ClientPreparedStatement.java:916) E6AD1C50DA4BEC50Z.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009) at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate( ClientPreparedStatement.java:1320) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:994) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnIm pl.java:197)... 21 更多

學生.java

package com.tapacad.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {
    
    // Attributes
    @Id
    @Column(name = "roll")
    private int roll;
    
    @Column(name = "name")
    private String name;
    
    @Column(name = "email")
    private String email;
    
    @Column(name = "marks")
    private int marks;
    
    // Constructor
    public Student() {
        
    }

    public Student(int roll, String name, String email, int marks) {
        super();
        this.roll = roll;
        this.name = name;
        this.email = email;
        this.marks = marks;
    }

    // Setters and Getters
    
    public int getRoll() {
        return roll;
    }

    public void setRoll(int roll) {
        this.roll = roll;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }
    
}

程序2.java

package com.tapacad.application;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.tapacad.model.Student;

public class Program2 {

    public static void main(String[] args) {
        
        Session session = null;
        SessionFactory sessionFactory = null;
        
        try {
            Configuration config = new Configuration();
            config.configure();
            config.addAnnotatedClass(Student.class);
            
            sessionFactory = config.buildSessionFactory();
            session = sessionFactory.openSession();
            
            // Create Transaction
            Transaction transaction = session.beginTransaction();
            
            // CRUD Operation
            Student s = new Student(1,"Alex","alex@gmail.com",97);
            session.save(s);
            
            transaction.commit();
        }
        finally{
            session.close();
            sessionFactory.close();
        }
        
    }

}

hibernate.cfg.xml

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

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    
    
    <hibernate-configuration>
        <session-factory>
            <property name = "hibernate.connection.driver.class">com.mysql.cj.jdbc.Driver</property>
            <property name = "hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
            <property name = "hibernate.connection.username">root</property>
            <property name = "hibernate.connection.password">mysql@123</property>
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <property name = "hibernate.hbm2dll.auto">update</property>
            <property name = "show_sql">update</property>
        
        </session-factory>
        
        
    </hibernate-configuration>

錯字警告!

Hibernate 屬性名稱hbm2dll拼寫錯誤。

hbm2ddl是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM