2015-07-13 102 views
0

我在这个关于hibernate映射的问题上工作了好几个小时。我认为错误可能是一个简单的语法错误,但我找不到它!未找到Hibernate映射异常类

我跑进了以下异常,当我执行我的代码:

Initial SessionFactory creation failed.org.hibernate.MappingException: entity class not found: LegalFee 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at com.plm.dao.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:33) 
    at com.plm.dao.util.HibernateUtil.getSessionFactory(HibernateUtil.java:39) 
    at com.plm.dao.AppTest.main(AppTest.java:15) 

首先,我使用Hibernate 4.2和Java 8和MariaDB的10

请参见下面的所有配置。

我的hibernate.cfg.xml,我删除C3P0配置:

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name='connection.driver_class'>com.mysql.jdbc.Driver</property> 
     <property name='connection.url'>jdbc:mysql://localhost:3306/PokerLeagueManager</property> 
     <property name='connection.username'>root</property> 
     <property name='connection.password'>mypassword</property> 
     <property name="show_sql">true</property> 

     <!-- SQL dialect --> 
     <property name='dialect'>org.hibernate.dialect.MySQL5InnoDBDialect</property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 
     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 


     <!-- Echo all executed SQL to stdout --> 
     <property name='show_sql'>true</property> 

     <mapping resource="mappings/BlindStructure.hbm.xml"/> 
     <mapping resource="mappings/Tournament.hbm.xml"/> 
     <mapping resource="mappings/LegalFee.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

错误是LegalFee豆所以重点关注一下。 见legalFee的SQL创建表:

CREATE TABLE `legalFee` (
    `idFee` int(11) NOT NULL, 
    `shortName` varchar(50) NOT NULL, 
    `description` varchar(200) DEFAULT NULL, 
    `feePercent` int(11) DEFAULT NULL, 
    `feeFixed` int(11) DEFAULT NULL, 
    PRIMARY KEY (`idFee`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

类:

public class LegalFee implements Serializable { 

/** 
* generated serial uid 
*/ 
private static final long serialVersionUID = -2259355205530727294L; 

private int idFee; 
private String shortName; 
private String description; 
private Integer feePercent; 
private Integer feeFixed; 
private Set<Tournament> tournaments = new HashSet<Tournament>(0); 

public LegalFee() { 
} 

public LegalFee(int idFee, String shortName) { 
    this.idFee = idFee; 
    this.shortName = shortName; 
} 

public LegalFee(int idFee, String shortName, String description, 
     Integer feePercent, Integer feeFixed, Set<Tournament> tournaments) { 
    this.idFee = idFee; 
    this.shortName = shortName; 
    this.description = description; 
    this.feePercent = feePercent; 
    this.feeFixed = feeFixed; 
    this.tournaments = tournaments; 
} 

public int getIdFee() { 
    return this.idFee; 
} 

public void setIdFee(int idFee) { 
    this.idFee = idFee; 
} 

public String getShortName() { 
    return this.shortName; 
} 

public void setShortName(String shortName) { 
    this.shortName = shortName; 
} 

public String getDescription() { 
    return this.description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public Integer getFeePercent() { 
    return this.feePercent; 
} 

public void setFeePercent(Integer feePercent) { 
    this.feePercent = feePercent; 
} 

public Integer getFeeFixed() { 
    return this.feeFixed; 
} 

public void setFeeFixed(Integer feeFixed) { 
    this.feeFixed = feeFixed; 
} 

public Set<Tournament> getTournaments() { 
    return this.tournaments; 
} 

public void setTournaments(Set<Tournament> tournaments) { 
    this.tournaments = tournaments; 
} 

} 

最后是LegalFee.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"> 

<hibernate-mapping> 
    <class name="LegalFee" table="legalFee" catalog="PokerLeagueManager" optimistic-lock="version"> 
     <id name="idFee" type="int"> 
      <column name="idFee" /> 
      <generator class="identity" /> 
     </id> 
     <property name="shortName" type="string"> 
      <column name="shortName" length="50" not-null="true" /> 
     </property> 
     <property name="description" type="string"> 
      <column name="description" length="200" /> 
     </property> 
     <property name="feePercent" type="java.lang.Integer"> 
      <column name="feePercent" /> 
     </property> 
     <property name="feeFixed" type="java.lang.Integer"> 
      <column name="feeFixed" /> 
     </property> 
     <set name="tournaments" table="tournament" inverse="true" lazy="true" fetch="select"> 
      <key> 
       <column name="legalFee_feeId" /> 
      </key> 
      <one-to-many class="Tournament" /> 
     </set> 
    </class> 
</hibernate-mapping> 

感谢您的帮助。

+1

尝试在LegalFee.hbm.xml中设置您的类的完全限定名称,例如com.foocompany.LegalFee(类节点) –

+0

我已经试过这个解决方案。它使我所有的豆类都有类似的错误。像hibernate一样,根本找不到它们。在这种情况下,错误是:创建初始SessionFactory failed.org.hibernate.MappingException:来自表比赛的关联指的是未映射的类:LegalFee 线程“main”中的异常java.lang.ExceptionInInitializerError \t at com.plm。 dao.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:33) \t at com.plm.dao.util.HibernateUtil.getSessionFactory(HibernateUtil.java:39) \t at com.plm.dao.AppTest.main(AppTest。 java:15) – Wodric

+0

我明白了,有需要完全限定名称的名称和类参数 – Wodric

回答

0

如果您的LegalFee类不是默认包,您需要提供该类的完整分类名称。例如“class name =”com.abc.xyx.LegalFee“table =”legalFee“”

+0

我已经试过这个解决方案。它使我所有的豆类都有类似的错误。像休眠一样,根本找不到他们 – Wodric

+0

你可以尝试添加这个。 这将扫描包中的所有文件。不知道它是否会工作,但你可以尝试一次。 –

+0

我明白了,有需要完全限定名称的名称和类参数 – Wodric