HIbernate无法运行会话

时间:2015-01-01 13:42:11

标签: java hibernate sql-server-2014-express

运行此文件时遇到的错误

run:
Going to run Session!
Jan 01, 2015 3:38:25 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Jan 01, 2015 3:38:25 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jan 01, 2015 3:38:25 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Classes/Data.hbm.xml
Jan 01, 2015 3:38:25 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: hibernate.hbm.xml
Jan 01, 2015 3:38:25 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" org.hibernate.MappingException: <mapping> element in configuration specifies no known attributes
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2286)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
    at org.hibernate.util.HibernateUtil.getSessionFactory(HibernateUtil.java:14)
    at test.Test.main(Test.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我的代码中的其他文件,其中Test.java是我的主文件

Test.java

package test;
import org.hibernate.Session;
import org.hibernate.Query;
import java.util.*;
import org.hibernate.util.HibernateUtil;


    public class Test {



  public static void main(String[] args) {
        System.out.println("Going to run Session!");
        Session session= HibernateUtil.getSessionFactory().openSession();
        System.out.println("Going to run Query!");
        Query query = session.createQuery("from Data");
        System.out.println("Going to run save query!");
        List list = query.list();
        System.out.println("Going to print list!");
        System.out.print(list);
    }
}

hibernateUtil.java

package org.hibernate.util;

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

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory getSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}
}

的hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!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.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost\I7\SQLEXPRESS:1433;databaseName=bulksms</property>
    <property name="hibernate.connection.username">connect</property>
    <property name="hibernate.connection.password">connection</property>
    <mapping resource="Classes/Data.hbm.xml"/>
    <mapping resource="hibernate.hbm.xml"/>
    <mapping/>
    <mapping/>
  </session-factory>
</hibernate-configuration>

hibernate.hbm.xml

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

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="Classes.Data" table="Data" schema="dbo" catalog="bulksms" optimistic-lock="version">
        <id name="name" type="string">
            <column name="Name" length="20" />
            <generator class="assigned" />
        </id>
        <property name="cell" type="string">
            <column name="Cell" length="12" />
        </property>
    </class>
</hibernate-mapping>

hibernate.reveng.xml中

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
  <schema-selection match-catalog="bulksms" match-schema="dbo"/>
  <table-filter match-name="Data"/>
</hibernate-reverse-engineering>

Data.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 30 Dec 2014 8:25:05 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="Classes.Data" table="Data" schema="dbo" catalog="bulksms" optimistic-lock="version">
        <id name="name" type="string">
            <column name="Name" length="20" />
            <generator class="assigned" />
        </id>
        <property name="cell" type="string">
            <column name="Cell" length="12" />
        </property>
    </class>
</hibernate-mapping>

Data.java

package Classes;
// Generated 30 Dec 2014 8:25:04 PM by Hibernate Tools 4.3.1

/**
 * Data generated by hbm2java
 */
public class Data  implements java.io.Serializable {


     private String name;
     private String cell;

    public Data() {
    }

    public Data(String name) {
        this.name = name;
    }
    public Data(String name, String cell) {
       this.name = name;
       this.cell = cell;
    }

    public String getName() {
        return this.name;
    }

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

    public void setCell(String cell) {
        this.cell = cell;
    }




}

1 个答案:

答案 0 :(得分:1)

基于此错误消息:

  配置中的

<mapping>元素指定没有已知属性

问题似乎出现在Hibernate配置文件中的空<mapping>元素中:

<mapping/>
<mapping/>

你应该删除它们。

此外,虽然它不是错误,但是日志中存在警告您使用过时的命名空间。理想情况下你也应该解决这个问题。