簡體   English   中英

Hibernate無法運行會話

[英]Hibernate unable to run session

我在運行此文件時遇到的錯誤

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




}

根據此錯誤消息:

配置中的<mapping>元素未指定任何已知屬性

問題似乎出在您的Hibernate配置文件中的空<mapping>元素中:

<mapping/>
<mapping/>

您應該刪除它們。

同樣,雖然這不是錯誤,但是日志中有警告,表明您正在使用過時的名稱空間。 理想情況下,您也應該修復該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM