org.hibernate.MappingNotFoundException:resource:找不到hibernate.hbm.xml

时间:2018-05-27 07:07:10

标签: hibernate

帮我解决这个问题。它没有连接到数据库,而是使用wamp服务器作为数据库

[文件层次结构] [1]

>

public boolean login() {

    try{
      SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
      Session session=sessionFactory.openSession();
      session.beginTransaction();
      Query query=session.createQuery("from Login where username=:username and password=:password");
      query.setString("username", username);
      query.setString("password", password);
      List list=query.list();

      System.out.println("list size"+list.size());

       if(list.size()==1){
       return true;
       }else{
       return false;
               }
         }

    catch(Exception e)
    {
        System.out.println(e);
    }
    return false;
}

日志文件,我正在使用玻璃鱼服务器

HHH000043: Configuring from resource: /hibernate.cfg.xml
Info:   HHH000040: Configuration resource: /hibernate.cfg.xml
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!
Info:   HHH000221: Reading mappings from resource: hibernate.hbm.xml
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!
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!
ERROR:   HHH000196: Error parsing XML (2) : The content of element type "class" is incomplete, it must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
Info:   org.hibernate.InvalidMappingException: Unable to read XML
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
    at org.hibernate.cfg.Configuration.add(Configuration.java:488)

hiberanate配置文件:hibernate.config.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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jsfformdb</property>
    <property name="hibernate.connection.username">root</property>
    <!--  <property name="password" /> 
    <property name="connection.pool_size">1</property> --> 
    <property name="hbm2ddl.auto">update</property>
    <!--<mapping class="Login.Login"></mapping>-->
    <mapping resource="hibernate.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

- &GT;

Hiberanet映射文件: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="Login.Login" table="login1"/>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

  

错误:HHH000196:解析XML时出错(2):元素类型“class”的内容不完整,它必须匹配“(meta *,subselect?,cache?,synchronize *,comment ?,的tuplizer *,的?(ID |复合-ID)下,鉴别器,天然-ID,(版本|时间戳),(财产|?多到一个|一到一个|分量|动态分量|特性|任何|地图|设为|列表|袋| idbag |阵列|原始阵列)的,((合并,子类*)|接合子类* |联合子类?*),装载机,SQL插入,SQL更新,SQL-删除,滤波器*,取轮廓*,结果集*,(查询|????。SQL查询)*)”

class中的hibernate.hbm.xml映射需要定义id(或composite-id),以及类中的属性,例如

<?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 = "Login.Login" table = "login1">

      <id name = "username" type = "string" column = "username">
         <generator class="assigned"/>
      </id>

      <property name = "password" column = "password" type = "string"/>
      ...
      other properties...
      ...
   </class>
</hibernate-mapping>