Hibernate和Maven:无法从资源分析映射文档 [英] Hibernate and maven: Could not parse mapping document from resource

查看:94
本文介绍了Hibernate和Maven:无法从资源分析映射文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INFO:HHH000041:配置SessionFactory:null 
当我尝试使用Maven进行编译时,出现此错误:创建会话时出错:org.hibernate.InvalidMappingException:无法从资源com / baskeitor / models / User.hbm.xml中解析映射文档

但我不明白错误在哪里:

User.java



package main.java.com.project.models;

public class User实现java.io.Serializable {

  private static final long serialVersionUID = 1L; 

私人长ID;

private String firstName;
private String secondName;
私人字符串电子邮件;
私人字符串密码;
private Set< Artifact> artifact = new HashSet< Artifact>();
$ b $ public User(){}
$ b $ public User(String firstName,String secondName,String email,String password){
this.firstName = firstName;
this.secondName = secondName;
this.email =电子邮件;
this.password =密码;
}

public Long getId(){
return id;
}

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

public Set< Artifact> getArtifacts(){
返回团队;


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映射DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-mapping -3.0.dtd>

< hibernate-mapping package =com.project.models>
< class name =Usertable =USER>
< id name =idcolumn =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配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd>
< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>
<! - SQL方言 - >
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>
<! - 禁用第二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>
<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>
< property name =hbm2ddl.auto>建立< / property>

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

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

当maven运行一个测试,我试着获取HibernateUtil.getSessionFactory()解决方案


解决方案

看起来这里有几个问题,首先,找不到hibernate映射文件。特别的是你在maven输出中给出的位置与你在hibernate配置中指定的位置不符。



你有

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

但hibernate抱怨无法找到 com / baskeitor / models /User.hbm.xml 。这个值显然是由hibernate组成的,所以它必须设置在某个地方。使用你喜欢搜索的工具来搜索它,然后搜索它。



另外,你的hibernate-mapping元素的package属性有一个问题,它的在你的hbm文件中设置为 com.project.models ,但实际的包是 main.java.com.project.models 。因此,由于您在hibernate-mapping元素中指定的包名称在映射文档中的非限定类名的前面加上了前缀,因此类元素中的类名将会是com.project.models.User,它与实际的类名不匹配。



(在一个侧面节点上,为什么不使用注释?他们觉得这样与imho合作更自然)


When I try compile with Maven I get this error:

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

But I don't see where is the error:

User.java

package main.java.com.project.models;

public class User implements 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>  

I get this error when maven runs a test where I try get HibernateUtil.getSessionFactory();

解决方案

It seems like there is a couple of problems here, first of all, the hibernate mapping file can not be found. What's peculiar is that the location you are given in the maven output doesn't match the location you have specified in your hibernate config.

you have

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

but hibernate is complaining about not being able to find com/baskeitor/models/User.hbm.xml. This value is obviously arbitrarily being made up made up by hibernate, so it must be set somewhere. Try and search around for it using whichever tool you like to search though files with.

Also, there is a problem with the package attribute of your hibernate-mapping element, its set as com.project.models in your hbm file but the actual package is main.java.com.project.models.

So, since the package name you specify in the hibernate-mapping element is prefixed in-front of unqualified class names in the mapping document, class name in the class element will be com.project.models.User, which doesn't match the actual class name.

(On a side node, why not look using the annotations? They feel so much more natural to work with imho)

这篇关于Hibernate和Maven:无法从资源分析映射文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆