使用Hibernate进行无效映射

时间:2015-08-27 17:41:37

标签: java hibernate

我尝试在netbeans上使用Hibernate 4.3.x和java时遇到了一个exeption,注意事项是:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at teste.ConectaJavaDB.main(ConectaJavaDB.java:17)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource model/Equipe.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3764)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3753)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3741)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at conexao.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
    at conexao.HibernateUtil.<clinit>(HibernateUtil.java:14)
    ... 1 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping model.Equipe
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2837)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:178)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3761)
    ... 7 more

(第二个错误说明重复映射,但我dindt映射了两次) 通过阅读该例程,可以看到问题出现在Equipe类的映射xml中,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 package="model">
  <class name="model.Equipe" table="equipe">
    <id name="id" type="int" column="ID" >
      <generator class="increment"/>
    </id>
    <property name="nome"/>
    <property name="dataNascimento"/>
    <property name="email"/>
    <property name="enderecoWeb"/>
    <property name="fone"/> 
  </class>
</hibernate-mapping>

上课:

    package model;
    import java.util.Date;

    public class Equipe {

        private int id;
        private String nome;
        private Date dataNascimento;
        private String email;
        private String enderecoWeb;
        private String fone;

   [..]getters and setters[..]

Hibernate.cgf

    <?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/mysql?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">app</property>
    <property name="hibernate.connection.password">app</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <mapping class="model.Jogador"/>
    <mapping resource="model/Equipe.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

我似乎无法找到错误,孔代码贴在Bitbucket上面,谢谢!

孔代码:https://bitbucket.org/angelorodem/progapl_ii_272278_2015/src

1 个答案:

答案 0 :(得分:1)

您曾两次调用configuration.configure()。删除第二次调用,或者只复制粘贴下面的代码:

    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;