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)