简体   繁体   English

线程“main” org.hibernate.boot.MappingNotFoundException 中的异常:未找到映射(资源):allinone.hbm.xml:起源(allinone.hbm.xml)

[英]Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : allinone.hbm.xml : origin(allinone.hbm.xml)

Main class ,DAO主类,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");
    }
}

Employee class ,Bean class员工类,Bean类

package news.hib.Single;包 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 class extends Employee , Bean class Admin 类扩展了 Employee , Bean 类

package news.hib.Single;包 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;
    }
}

Hardware class extends Employee ,Bean class硬件类扩展了 Employee ,Bean 类

package news.hib.Single;包 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 file ,configuration file. Hibernate-cfg 文件,配置文件。

com.mysql.jdbc.Driver root jdbc:mysql://localhost:3306/database1 root org.hibernate.dialect.MySQLDialect create true com.mysql.jdbc.Driver root jdbc:mysql://localhost:3306/database1 root org.hibernate.dialect.MySQLDialect create true

mapping-file ,i am trying to write a code for table per class approach.映射文件,我正在尝试为每个类的表编写代码。

I got this exception ,any mistakes?我得到了这个例外,有什么错误吗?

Hello World!你好,世界! Jul 18, 2017 4:20:07 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.10.Final} Jul 18, 2017 4:20:07 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Jul 18, 2017 4:20:08 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : allinone.hbm.xml : origin(allinone.hbm.xml) at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56) at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav 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 INFO:HHH6找不到 hibernate.properties 2017 年 7 月 18 日下午 4:20:08 org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO:HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 线程“main”组织中的异常。 hibernate.boot.MappingNotFoundException:未找到映射(资源):allinone.hbm.xml:org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56) 处的 origin(allinone.hbm.xml) at org.hibernate .boot.MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java) :413) 在 org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.jav a:691) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) at news.hib.Single.App.main(App.java:18) a:691) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) at news.hib.Single.App.main(App.java:18)

You need to add the full path to your hbm files.您需要添加 hbm 文件的完整路径。 So need to add "src2"所以需要添加“src2”

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

Note: Your package name have an incorrect format:注意:您的包名称格式不正确:

news.hib.Single新闻.hib.Single

Should be all package names in lowercase.应该是小写的所有包名称。

Source here来源在这里

I would recommend you to keep the mapping xml in the same path as the hibernate configuration file and only mention the mapping file name in your tag in the hibernate configuration file.我建议您将映射 xml 保留在与 hibernate 配置文件相同的路径中,并且只在 hibernate 配置文件的标记中提及映射文件名。

It worked for me.它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠给我-“主要” org.hibernate.MappingNotFoundException:资源:找不到hibernate_hbm.xml.UserDetails.hbm.xml - hibernate is giving me - “main” org.hibernate.MappingNotFoundException: resource: hibernate_hbm.xml.UserDetails.hbm.xml not found org.hibernate.MappingNotFoundException:resource:*找不到hbm.xml - org.hibernate.MappingNotFoundException: resource: *hbm.xml not found org.hibernate.boot.MappingNotFoundException:未找到映射(RESOURCE) - org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found 无法解析在休眠中找不到MappingNotFoundException:resource:/login.hbm.xml - unable to resolve MappingNotFoundException :resource:/login.hbm.xml not found in hibernate 错误org.hibernate.MappingNotFoundException:资源:找不到name.hbm.xml - Error org.hibernate.MappingNotFoundException: resource: name.hbm.xml not found org.hibernate.MappingNotFoundException:资源:找不到com / ypd / a / entity / Employee.hbm.xml - org.hibernate.MappingNotFoundException: resource: com/ypd/a/entity/Employee.hbm.xml not found org.hibernate.MappingNotFoundException:资源:找不到com / corp / dept / proj / FooTbl.hbm.xml - org.hibernate.MappingNotFoundException: resource: com/corp/dept/proj/FooTbl.hbm.xml not found MappingNotFoundException:资源:找不到user.hbm.xml - MappingNotFoundException: resource: user.hbm.xml not found 线程“主” org.hibernate.InvalidMappingException中的异常:无法从资源userdata.hbm.xml中解析映射文档 - Exception in thread “main” org.hibernate.InvalidMappingException: Could not parse mapping document from resource userdata.hbm.xml hibernate.hbm.xml中的无效映射异常 - Invalid Mapping Exception in hibernate.hbm.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM