0
votes

Main class,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

package 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

package 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

package 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.

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.java:691) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) at news.hib.Single.App.main(App.java:18)

2
where are you stored the allione.hbm.xml file?cralfaro
@cralfaro,i have created 2 packages 1)news.hib.Single->Employee.java ,Admin.java ,Hardware.java and App.java 2)src2-> allinone.hbm.xml and hibernate.cfg.xml ,Bhanu Prakash H c
@cralfaro, i am getting same exception. I think problem is in hibernate.cfg.xml and mapping fileBhanu Prakash H c
@cralfaro, i have created table like this ->create table bubbles(id int,discriminator varchar(20),name varchar(20),mail varchar(20),salary double,skills varchar(20));Bhanu Prakash H c
But the problem is that your app is not able to find the hbm file, so doesnt know yet the table name, first you need to make accessible the hbm file from your main class. Modify the path to something is readable from your main classcralfaro

2 Answers

0
votes

You need to add the full path to your hbm files. So need to add "src2"

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

Note: Your package name have an incorrect format:

news.hib.Single

Should be all package names in lowercase.

Source here

0
votes

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.

It worked for me.