简体   繁体   English

Hibernate无法运行会话

[英]Hibernate unable to run session

The errors that i encountered while running this file 我在运行此文件时遇到的错误

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)

The other files in my code where Test.java is my main file 我代码中的其他文件,其中Test.java是我的主文件

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 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 的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 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 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 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 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;
    }




}

Based on this error message: 根据此错误消息:

<mapping> element in configuration specifies no known attributes 配置中的<mapping>元素未指定任何已知属性

the problem seems to be in the empty <mapping> elements in your Hibernate config file: 问题似乎出在您的Hibernate配置文件中的空<mapping>元素中:

<mapping/>
<mapping/>

You should remove them. 您应该删除它们。

Also while it is not an error, there are warnings in logs that you are using obsolete namespace. 同样,虽然这不是错误,但是日志中有警告,表明您正在使用过时的名称空间。 Ideally you should fix that too. 理想情况下,您也应该修复该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM