2012-11-22 27 views
1

解析映射文件當我嘗試使用Maven編譯我得到這個錯誤:Hibernate和行家:無法從資源

INFO: HHH000041: Configured SessionFactory: null 
Error creating Session: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/baskeitor/models/User.hbm.xml 

但我看不出哪裏是錯誤:

用戶。 java

package main.java.com.project.models;

公共類用戶實現java.io.Serializable {

private static final long serialVersionUID = 1L; 

private Long id; 

private String firstName; 
private String secondName; 
private String email; 
private String password; 
private Set<Artifact> artifact = new HashSet<Artifact>(); 

public User(){} 

public User(String firstName, String secondName, String email, String password){ 
    this.firstName = firstName; 
    this.secondName = secondName; 
    this.email = email; 
    this.password = password; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public Set<Artifact> getArtifacts() { 
    return teams; 
} 

public void setArtifacts(Set<Artifact> artifacts) { 
    this.teams = team; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getSecondName() { 
    return secondName; 
} 

public void setSecondName(String secondName) { 
    this.secondName = secondName; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

}

User.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 package="com.project.models"> 
    <class name="User" table="USER"> 
     <id name="id" column="USER_ID"> 
      <generator class="native"/> 
     </id> 
     <property name="firstName"/> 
     <property name="secondName"/> 
     <property name="email"/> 
     <property name="password"/> 
    </class> 
</hibernate-mapping>   

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<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/db</property> 
     <property name="connection.username">user</property> 
     <property name="connection.password">pass</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 
     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</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.NoCacheProvider</property> 
     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property>   
     <property name="hbm2ddl.auto">create</property> 

     <mapping resource="com/project/models/User.hbm.xml"/> 

    </session-factory> 
</hibernate-configuration> 

當maven運行一個測試,我嘗試獲取HibernateUtil.getSessionFactory();

回答

1

這裏好像有幾個問題,首先,找不到hibernate映射文件。有什麼特別的是你在maven輸出中給出的位置與你在hibernate配置中指定的位置不匹配。

你有

<mapping resource="com/project/models/User.hbm.xml"/> 

但是Hibernate是抱怨不能夠找到com/baskeitor/models/User.hbm.xml。這個值顯然是由hibernate組成的,所以它必須設置在某個地方。嘗試使用任何你喜歡搜索文件的工具搜索它。

此外,您的hibernate-mapping元素的包屬性存在問題,它在hbm文件中設置爲com.project.models,但實際包爲main.java.com.project.models。因此,由於您在hibernate-mapping元素中指定的包名稱在映射文檔中的非限定類名前面加前綴,因此類元素中的類名將是com.project.models.User,它不會與實際的課程名稱不符。

(在一個側面節點上,爲什麼不看看使用註釋?他們覺得與imho一起工作更加自然)