1

我是新来的冬眠。我正在尝试从带注释的实体类生成数据库表。

Tag班级:

package playground.data.entities;

import java.time.Instant;
import javax.persistence.*;

@Entity
public class Tag extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long Id;

    @Column(unique = true, nullable = false)
    public String Name;

    @Column(nullable = false)
    public Instant CreatedOn;
}

这是我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.password">1234</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost;database=HibernatePlayground;SelectMethod=cursor</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.default_schema">dbo</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
    
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping class="playground.data.entities.Tag" />
    </session-factory>
</hibernate-configuration>

这是我的HibernateUtil课(参考:http ://www.mkyong.com/hibernate/maven-3-hibernate-3-6-oracle-11g-example-xml-mapping/,http : //www.mkyong.com/ hibernate/maven-3-hibernate-3-6-oracle-11g-example-annotation/):

package util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import playground.data.entities.Tag;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration config = new Configuration();
            //config.addAnnotatedClass(Tag.class); //<-- tried this too
            return config.configure().buildSessionFactory();

            // Also tried the following.
            // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();

            // return new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();

        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}

最后,使用 Main 中的这些:

package playground;

import java.time.Instant;

import org.hibernate.Session;

import playground.data.entities.Tag;
import util.HibernateUtil;

public class Main {

    public static void main(String[] args) {
    
        Tag tag1 = new Tag() {
            {
                Name = "Tag1";
                CreatedOn = Instant.now();
            }
        };
        Tag tag2 = new Tag(){
            {
                Name = "Tag2";
                CreatedOn = Instant.now();
            }
        };
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();
        // saving objects to session
        ss.save(tag1); //<-- getting error here.
        ss.save(tag2);
        ss.getTransaction().commit();
        HibernateUtil.shutdown();
    }
}

但是在saveingtag1对象时,我得到了Exception in thread "main" org.hibernate.MappingException: Unknown entity: playground.Main$1. 下面是完整的堆栈跟踪以及来自休眠的记录信息:


Nov 15, 2016 11:42:59 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.4.Final}
Nov 15, 2016 11:42:59 AM org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
Nov 15, 2016 11:42:59 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 15, 2016 11:43:00 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Nov 15, 2016 11:43:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Nov 15, 2016 11:43:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost;database=HibernatePlayground;SelectMethod=cursor]
Nov 15, 2016 11:43:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa, password=****}
Nov 15, 2016 11:43:00 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Nov 15, 2016 11:43:00 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Nov 15, 2016 11:43:00 AM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Nov 15, 2016 11:43:02 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@10c626be] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: playground.Main$1
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1605)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:674)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:661)
    at playground.Main.main(Main.java:85)

我在用:

  • Java 1.8,休眠:5.2
  • 数据库服务器:SQL Server 2016
  • 操作系统:Windows 10
  • IDE:Eclipse IDE Neon (4.6.0)

我的项目结构如下:

项目结构

我不确定它为什么会抛出MappingException. Main请建议。

4

1 回答 1

4

当您执行以下操作时,您正在使用实例初始化程序创建实体类的匿名子类Tag来初始化成员变量。这不是创建和初始化Tag实体的正确方法:

Tag tag1 = new Tag() {
    {
        Name = "Tag1";
        CreatedOn = Instant.now();
    }
};

相反,创建一个类Tag本身的实例并设置值:

Tag tag1 = new Tag();
tag1.Name = "Tag1";
tag1.CreatedOn = Instant.now();

但这段代码还有更多错误。类中的字段Tag不应该是public, but private,并且应该在类 Tag 中添加 getter 和 setter 方法。此外,Java 中的字段名称通常应该以小写字母开头。

@Entity
public class Tag extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

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

    @Column(nullable = false)
    private Instant createdOn;

    public String getName() {
        return name;
    }

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

    public Instant getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Instant createdOn) {
        this.createdOn = createdOn;
    }
}

并初始化它:

Tag tag1 = new Tag();
tag1.setName("Tag1");
tag1.setCreatedOn(Instant.now());
于 2016-11-15T11:34:27.927 回答