代码之家  ›  专栏  ›  技术社区  ›  Stefan Kendall

org.hibernate.MappingException:未知实体

  •  22
  • Stefan Kendall  · 技术社区  · 14 年前

    我正在尝试开始Hibernate第二版,我一直在尝试用HSQLDB组合简单的工作示例。

    当我跑的时候 ant populateMessages ,我明白了

    [java] org.hibernate.MappingException: Unknown entity: sample.entity.Message
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
    [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:747)
    ...
    

    我得到的是:

    package sample.entity;
    
    import org.hibernate.annotations.Entity;
    
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    public class Message
    {
        private String messageText;
        private Integer id;
    
        public Message( String messageText )
        {
            this.messageText = messageText;
        }
    
        public Message()
        {
        }
    
        public String getMessageText()
        {
            return messageText;
        }
    
        public void setMessageText(String messageText)
        {
            this.messageText = messageText;
        }
    
        @Id
        @GeneratedValue
        public Integer getId()
        {
            return id;
        }
    
        public void setId(Integer id)
        {
            this.id = id;
        }
    }
    

    PopulateMessages.java

    package sample;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import sample.entity.Message;
    
    import java.util.Date;
    
    public class PopulateMessages
    {
        public static void main(String[] args)
        {
            SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
            Session session = factory.openSession();
            session.beginTransaction();
    
            Message m1 = new Message("Hibernated a  messages on " + new Date());
            session.save(m1);
            session.getTransaction().commit();
            session.close();
        }
    }
    

    # Path to the hibernate install directory
    hibernate.home=C:/hibernate/hibernate-3.5.6
    # Path to the hibernate-tools install directory
    hibernate.tools.home=C:/hibernate/hibernate-tools
    # Path to hibernate-tools.jar relative to hibernate.tools.home
    hibernate.tools.path=/plugins/org.hibernate.eclipse_3.3.1.v201006011046R-H111-GA/lib/tools
    # Path to hibernate-tools hibernate libraries relative to hibernate.tools.home
    hibernate.tools.lib.path=/plugins/org.hibernate.eclipse_3.3.1.v201006011046R-H111-GA/lib/hibernate
    # Path to the SLF4J implementation JAR for the logging framework to use
    slf4j.implementation.jar=lib/slf4j-simple-1.6.1.jar
    # Path to the HSQL DB install directory
    hsql.home=C:/hsqldb
    

    hibernate.cfg.xml文件

    <!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.url">
    jdbc:hsqldb:file:testdb;shutdown=true
    </property>
    <property name="hibernate.connection.driver_class">
    org.hsqldb.jdbcDriver
    </property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">0</property>
    <property name="hibernate.dialect">
    org.hibernate.dialect.HSQLDialect
    </property>
    <property name="hibernate.show_sql">false</property>
    <!-- "Import" the mapping resources here -->
    <mapping class="sample.entity.Message"/>
    </session-factory>
    </hibernate-configuration>
    

    编译文件

    <project name="sample">
        <property file="build.properties"/>
        <property name="src" location="src"/>
        <property name="bin" location="bin"/>
        <property name="sql" location="sql"/>
        <property name="hibernate.tools"
                  value="${hibernate.tools.home}${hibernate.tools.path}"/>
        <path id="classpath.base">
            <pathelement location="${src}"/>
            <pathelement location="${bin}"/>
            <pathelement location="${hibernate.home}/hibernate3.jar"/>
            <pathelement location="${slf4j.implementation.jar}"/>
            <fileset dir="${hibernate.home}/lib" includes="**/*.jar"/>
            <pathelement location="${hsql.home}/lib/hsqldb.jar"/>
      <fileset dir="./lib" includes="**/*.jar"/>
        </path>
    <path id="classpath.tools">
        <path refid="classpath.base"/>
        <pathelement
                location="${hibernate.tools.home}/${hibernate.tools.lib.path}/commons-logging-1.0.4.jar"/>
        <pathelement
                location="${hibernate.tools}/freemarker.jar"/>
        <pathelement
                location="${hibernate.tools}/hibernate-tools.jar"/>
    </path>
    <taskdef name="htools"
             classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="classpath.tools"/>
    <target name="exportDDL" depends="compile">
        <mkdir dir="${sql}"/>
        <htools destdir="${sql}">
            <classpath refid="classpath.tools"/>
            <annotationconfiguration
                    configurationfile="${src}/hibernate.cfg.xml"/>
            <hbm2ddl drop="true" outputfilename="sample.sql"/>
        </htools>
    </target>
    <target name="compile">
        <javac srcdir="${src}" destdir="${bin}" classpathref="classpath.base"/>
    </target>
    <target name="populateMessages" depends="compile">
        <java classname="sample.PopulateMessages" classpathref="classpath.base"/>
    </target>
    <target name="listMessages" depends="compile">
        <java classname="sample.ListMessages" classpathref="classpath.base"/>
    </target>
    

    10 回复  |  直到 14 年前
        1
  •  34
  •   Pascal Thivent    14 年前

    您的实体没有正确注释,您 必须 使用 @javax.persistence.Entity 注解。您可以使用Hibernate扩展 @org.hibernate.annotations.Entity

    因此,请将代码更改为:

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    public class Message { 
        ...  
    }

        2
  •  12
  •   Bozho    12 年前

    你应该打电话 .addAnnotatedClass(Message.class) 关于你的 AnnotationConfiguration .

    EntityManager (日本)

    ( Reference )

    更新:您似乎已经在hibernate.cfg.xml中列出了该类。所以不需要自动发现。顺便说一句,试试 javax.persistence.Entity

        3
  •  0
  •   M.J.    14 年前

    如果需要自动发现类,则应在.addAnnotatedClass(类)方法中添加所有实体文件。

    使用此链接,可能会有帮助。。

    http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/cfg/AnnotationConfiguration.html

        4
  •  0
  •   Racil Hilan    9 年前

    当我切换到 AnnotationSessionFactoryBean . 我在用 entity.hbm.xml 以前。

    我发现我的类缺少以下注释,这些注释解决了我的问题:

    @Entity
    @Table(name = "MyTestEntity")
    @XmlRootElement
    
        5
  •  0
  •   Saravana    8 年前

    SpringBoot 应用程序,即使实体用 Entity 注释,可能是由于spring不知道在哪里扫描实体

    要显式指定包,请在下面添加

    @SpringBootApplication
    @EntityScan({"model.package.name"})
    public class SpringBootApp {...}
    

    注意:如果模型类位于 SpringBootApplication 带注释的类,不需要显式声明 EntityScan ,默认情况下它将扫描

        6
  •  0
  •   Akanksha    7 年前

    导入javax.persistence.Entity; 导入org.hibernate.annotations.Entity;

        7
  •  0
  •   ErSyyedS    7 年前

    在Spring Boot应用程序中使用下面的代码行 附加的Spring Boot主类

        8
  •  0
  •   ErSyyedS    7 年前

    在spring boot应用程序中使用下面的代码行。

        9
  •  0
  •   Santosh Reddy    6 年前

    我的问题在添加
    会话工厂.setPackagesToScan( 新字符串[]{“com.springhibernate.model”}); 在SpringBoot最新版本2.1.2中测试了此功能。

    完整方法:

     @Bean( name="sessionFactoryConfig")
        public LocalSessionFactoryBean sessionFactoryConfig() {
    
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    
            sessionFactory.setDataSource(dataSourceConfig());
            sessionFactory.setPackagesToScan(
                    new String[] { "com.springhibernate.model" });
    
            sessionFactory.setHibernateProperties(hibernatePropertiesConfig());
    
            return sessionFactory;
        }
    
        10
  •  -1
  •   Sachin Pawar    5 年前

    您可以通过在Application.java上添加下面的注释来启用实体扫描 @EntityScan(basePackageClasses=YourEntityClassName.class)

    或者可以在会话工厂中设置packageToScan。 sessionFactory.setPackagesToScan(com.all.entity);

        11
  •  -2
  •   Satya Johnny    9 年前

    在hibernate.cfg.xml中,请输入以下代码

    <mapping class="class/bo name"/>