线程" main"中的例外情况org.hibernate.boot.MappingNotFoundException:找不到映射(RESOURCE):allinone.hbm.xml:origin(allinone.hbm.xml)

时间:2017-07-18 11:12:16

标签: java hibernate maven

主要课程,DAO

package news.hib.Single;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        Configuration cfg = new Configuration().configure("src2/hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session ses = sf.openSession();
        Transaction t =ses.beginTransaction();
        Employee e = new Employee();
        e.setId(101);
        e.setName("bhanu");
        e.setMail("bp.com");
        Hardware h = new Hardware();
        h.setId(101);
        h.setName("bhanu");
        h.setMail("bp.com");
        h.setSkills("idk");
        Admin a = new  Admin();
        a.setId(101);
        a.setName("bhanu");
        a.setMail("bp.com");
        a.setSalary(199999);
        ses.save(e);
        ses.save(h);
        ses.save(a);
        t.commit();
        ses.close();
        System.out.println("success");
    }
}

员工类,Bean类

包news.hib.Single;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="Bubble")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="emp")
public class Employee {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private String mail;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }

}

Admin类扩展了Employee ,Bean类

包news.hib.Single;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="admin")
public class Admin extends Employee{
    private double salary;
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

硬件类扩展了Employee ,Bean类

包news.hib.Single;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="hard")
public class Hardware extends Employee{
    private String skills;
    public String getSkills() {
        return skills;
    }
    public void setSkills(String skills) {
        this.skills = skills;
    }    
}

Hibernate-cfg文件,配置文件。

                               com.mysql.jdbc.Driver             根             JDBC:MySQL的://本地主机:3306 /数据库1             根             org.hibernate.dialect.MySQLDialect             创建             真正                           

映射文件,我正在尝试为每个类的方法编写一个代码。

                                                                                                                                                                                    

我有这个例外,有什么错误吗?

  

Hello World!           2017年7月18日下午4:20:07 org.hibernate.Version logVersion           信息:HHH000412:Hibernate Core {5.2.10.Final}           2017年7月18日下午4:20:07 org.hibernate.cfg.Environment           信息:HHH000206:找不到hibernate.properties           2017年7月18日下午4:20:08 org.hibernate.annotations.common.reflection.java.JavaReflectionManager              信息:HCANN000001:Hibernate Commons Annotations {5.0.1.Final}           线程" main"中的例外情况org.hibernate.boot.MappingNotFoundException:映射(RESOURCE)没有   发现:allinone.hbm.xml:origin(allinone.hbm.xml)             在org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)             在org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)             在org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)             在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413)             在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)             在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)             在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)             在news.hib.Single.App.main(App.java:18)

2 个答案:

答案 0 :(得分:0)

您需要添加hbm文件的完整路径。所以需要添加“src2”

<mapping resource="src2/allinone.hbm.xml"/>

注意:您的包名称格式不正确:

  

news.hib.Single

所有包名都应该是小写的。

来源here

答案 1 :(得分:0)

我建议您将映射 xml 保留在与 hibernate 配置文件相同的路径中,并且只在 hibernate 配置文件的标签中提及映射文件名。

它对我有用。