Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate和maven:无法从资源解析映射文档_Hibernate_Hibernate Mapping - Fatal编程技术网

Hibernate和maven:无法从资源解析映射文档

Hibernate和maven:无法从资源解析映射文档,hibernate,hibernate-mapping,Hibernate,Hibernate Mapping,当我尝试使用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 但我不知道错误在哪里: User.java 包main.java.

当我尝试使用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
但我不知道错误在哪里:

User.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;
}
private static final long serialVersionUID=1L;
私人长id;
私有字符串名;
私有字符串secondName;
私人字符串电子邮件;
私有字符串密码;
私有集工件=新HashSet();
公共用户(){}
公共用户(字符串firstName、字符串secondName、字符串email、字符串密码){
this.firstName=firstName;
this.secondName=secondName;
this.email=电子邮件;
this.password=密码;
}   
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共集getArtifacts(){
返回队;
}
公共void setArtifacts(Set artifacts){
这个。团队=团队;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getSecondName(){
返回secondName;
}
public void setSecondName(字符串secondName){
this.secondName=secondName;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.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>         
<?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>  

hibernate.cfg.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>         
<?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>  

com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/db
用户
通过
1.
org.hibernate.dialogue.mysqldialogue
线
org.hibernate.cache.NoCacheProvider
真的
创造

当maven运行一个测试,试图获取HibernateUtil.getSessionFactory()时,我遇到了这个错误

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

你有

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

但是hibernate抱怨找不到
com/baskeitor/models/User.hbm.xml
。这个值显然是由hibernate任意设置的,所以必须在某个地方设置。试着用你喜欢的工具在文件中搜索它

此外,hibernate映射元素的package属性也有问题,它在hbm文件中设置为
com.project.models
,但实际的包是
main.java.com.project.models

因此,由于在hibernate映射元素中指定的包名在映射文档中的非限定类名前面加了前缀,因此class元素中的类名将是com.project.models.User,与实际类名不匹配


(在一个侧节点上,为什么不使用注释进行查看?使用imho会让它们感觉更自然)

这里似乎有几个问题,首先,无法找到hibernate映射文件。奇怪的是,maven输出中给出的位置与hibernate配置中指定的位置不匹配

你有

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

但是hibernate抱怨找不到
com/baskeitor/models/User.hbm.xml
。这个值显然是由hibernate任意设置的,所以必须在某个地方设置。试着用你喜欢的工具在文件中搜索它

此外,hibernate映射元素的package属性也有问题,它在hbm文件中设置为
com.project.models
,但实际的包是
main.java.com.project.models

因此,由于在hibernate映射元素中指定的包名在映射文档中的非限定类名前面加了前缀,因此class元素中的类名将是com.project.models.User,与实际类名不匹配

(在侧节点上,为什么不使用注释进行查看?使用imho时,它们会感觉更自然)