1

当我尝试使用 Maven 编译时,出现此错误:

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

包 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;
}

}

用户.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>         

休眠.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();

4

1 回答 1

1

这里似乎有几个问题,首先,找不到休眠映射文件。奇怪的是,您在 Maven 输出中给出的位置与您在休眠配置中指定的位置不匹配。

你有

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

但是hibernate抱怨找不到com/baskeitor/models/User.hbm.xml。这个值显然是hibernate随意编造的,所以一定要在某个地方设置。尝试使用您喜欢搜索文件的任何工具搜索它。

此外,您的 hibernate-mapping 元素的 package 属性存在问题,它com.project.models在您的 hbm 文件中设置,但实际包是main.java.com.project.models.

因此,由于您在 hibernate-mapping 元素中指定的包名在映射文档中的非限定类名前面加上前缀,因此类元素中的类名将是 com.project.models.User,这与实际类名。

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

于 2012-11-22T13:12:40.017 回答