为什么我无法实例化id generator:org.hibernate.MappingException?

时间:2019-11-29 01:38:43

标签: java mysql hibernate

我是新来的休眠者,正在尝试构建示例程序。当我使用注释时,它工作正常,但是当我使用XML文件映射实体类时,它将引发以下异常:

Exception in thread "main" org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.hibernate.sachin.entiry.InstructorDetail]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:344)
    at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:287)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1628)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:485)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at com.sachin.tutorialandexample.Simple.main(Simple.java:17)
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [AUTO]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:144)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:115)
    ... 16 more

这是该程序的代码

表结构

instructor_detail

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| youtube_channel | varchar(128) | YES  |     | NULL    |                |
| hobby           | varchar(45)  | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

InstructorDetail.java(实体类)

package com.hibernate.sachin.entiry;

public class InstructorDetail {


    private int id;
    private String youtubeChannel;
    private String hobby;

    public InstructorDetail() {

    }

    public InstructorDetail(String youtubeChannel, String hobby) {
        this.youtubeChannel = youtubeChannel;
        this.hobby = hobby;
    }

    public String getYoutubeChannel() {
        return youtubeChannel;
    }

    public void setYoutubeChannel(String youtubeChannel) {
        this.youtubeChannel = youtubeChannel;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "InstructorDetail [id=" + id + ", youtubeChannel=" + youtubeChannel + ", hobby=" + hobby + "]";
    }
}

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.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/telusko?useSSL=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">password</property>

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

        <!-- Select our SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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>

        <mapping resource ="InstructorDetail.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

InstructorDetail.hbm.xml(映射文件)

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

 <hibernate-mapping>
    <class name="com.hibernate.sachin.entiry.InstructorDetail" table="instructor_detail">
        <id name="id" column="id">
            <generator class="AUTO"></generator>
        </id>
        <property name="youtubeChannel" column="youtube_channel" type="string" />
        <property name="hobby" column="hobby" type="string" />
    </class>

 </hibernate-mapping>

Main App.java

package com.sachin.tutorialandexample;

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

import com.hibernate.sachin.entiry.InstructorDetail;

import org.hibernate.Session;

public class Simple {

    public static void main(String[] args) {

        Configuration config = new Configuration();
        config.configure("hibernate.cfg.xml");

        SessionFactory sf = config.buildSessionFactory();
        Session session = sf.getCurrentSession();
        session.beginTransaction();

        InstructorDetail instructor = new InstructorDetail();

        instructor.setHobby("Coder");
        instructor.setYoutubeChannel("XYZ.com");

        session.save(instructor);

        session.getTransaction().commit();

        session.close();

   }

}

0 个答案:

没有答案