无法实例化persister org.hibernate.persister.entity.JoinedSubclassEntityPersister

时间:2016-12-05 02:15:06

标签: java mysql hibernate

尝试使用hibernate连接到mysql时出现以下错误。我有一个Super class Employee和三个子类:SoftwareEmployee,HardWareEmployee和Admin.I也有两个xml文件:hibernateisapersubclass.cfg.xml和employeeisapersubclass.hbm.xml。我正在尝试使用' joined-subclass'在4个不同的表中填充数据。在我的xml中标记。请帮忙。

我得到的错误如下:

Dec 04, 2016 7:45:34 PM org.hibernate.Version logVersion
        INFO: HHH000412: Hibernate Core {5.2.5.Final}
        Dec 04, 2016 7:45:34 PM org.hibernate.cfg.Environment <clinit>
        INFO: HHH000206: hibernate.properties not found
        Dec 04, 2016 7:45:34 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
        INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
        Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
        WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
        Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
        INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/hqlstudent]
        Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
        INFO: HHH10001001: Connection properties: {user=root, password=****}
        Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
        INFO: HHH10001003: Autocommit mode: false
        Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
        INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
        Sun Dec 04 19:45:35 EST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
        Dec 04, 2016 7:45:35 PM org.hibernate.dialect.Dialect <init>
        INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
        Dec 04, 2016 7:45:36 PM org.hibernate.AssertionFailure <init>
        ERROR: HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: Table hqlstudent.superemployee not found
        Dec 04, 2016 7:45:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
        INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost/hqlstudent]
        Exception in thread "main" org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.JoinedSubclassEntityPersister
            at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:112)
            at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
            at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:128)
            at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:297)
            at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
            at test.ClientIsAPerSubClass.main(ClientIsAPerSubClass.java:22)
        Caused by: org.hibernate.AssertionFailure: Table hqlstudent.superemployee not found
            at org.hibernate.persister.entity.AbstractEntityPersister.getTableId(AbstractEntityPersister.java:5168)
            at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:433)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
            ... 7 more

以下是我的五个java文件和两个xml文件:

ClientIsAPerSubClass.java

package test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import beans.Admin;
import beans.Admin1;
import beans.HardwareEmployee;
import beans.HardwareEmployee1;
import beans.SoftwareEmployee;
import beans.SoftwareEmployee1;


public class ClientIsAPerSubClass {

    public static void main(String [] args){

        Configuration configuration = new Configuration();
        configuration.configure("resources/hibernateisapersubclass.cfg.xml");
        SessionFactory sessionFactory  = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction= session.beginTransaction();//student object will move database;
        SoftwareEmployee1 softwareEmployee= new SoftwareEmployee1();
        softwareEmployee.setId(1);
        softwareEmployee.setName("bishwash");
        softwareEmployee.setEmail("20wash.sharma@gmail.com");
        softwareEmployee.setSalary(60000);
        softwareEmployee.setTool("hibernate");
        HardwareEmployee1 hardwareEmployee = new HardwareEmployee1();
        hardwareEmployee.setId(2);
        hardwareEmployee.setName("mahendra");
        hardwareEmployee.setEmail("mahendra@mahendra.com");
        hardwareEmployee.setSalary(55000);
        hardwareEmployee.setWorkinghour(20);
        Admin1 admin= new Admin1();
        admin.setId(3);
        admin.setName("ashish@ashish.com");
        admin.setEmail("mahendra@mahendra.com");
        admin.setSalary(60000);
        admin.setBranchname("Kathmandu");;
        session.save(softwareEmployee);
        session.save(hardwareEmployee);
        session.save(admin);
        transaction.commit();
    session.close();
    sessionFactory.close();
        //System.out.println("no of rows dumped from old to new table"+i);

    }

}

Admin.java
package beans;

public class Admin extends Employee {
private String branchname;
    public Admin() {

    }

    public Admin(int id, String name, String email, double salary, String branchname) {
        super(id, name, email, salary);
        this.branchname= branchname;
    }

    public String getBranchname() {
        return branchname;
    }

    public void setBranchname(String branchname) {
        this.branchname = branchname;
    }

}


package beans;

public class HardwareEmployee extends Employee {
private int workinghour;

public HardwareEmployee() {
    super();
    // TODO Auto-generated constructor stub
}

public HardwareEmployee(int id, String name, String email, double salary, int workinghour) {
    super(id, name, email, salary);
    this.workinghour= workinghour;
}

public int getWorkinghour() {
    return workinghour;
}

public void setWorkinghour(int workinghour) {
    this.workinghour = workinghour;
}


}

SoftwareEmployee.java
package beans;

public class SoftwareEmployee extends Employee {
private String tool;

public SoftwareEmployee() {
    super();
    // TODO Auto-generated constructor stub
}

public SoftwareEmployee(int id, String name, String email, double salary ,String tool) {
    super(id, name, email, salary);
    this.tool= tool;
}

public String getTool() {
    return tool;
}

public void setTool(String tool) {
    this.tool = tool;
}
}


HardwareEmployee.java
package beans;

public class HardwareEmployee extends Employee {
private int workinghour;

public HardwareEmployee() {
    super();
    // TODO Auto-generated constructor stub
}

public HardwareEmployee(int id, String name, String email, double salary, int workinghour) {
    super(id, name, email, salary);
    this.workinghour= workinghour;
}

public int getWorkinghour() {
    return workinghour;
}

public void setWorkinghour(int workinghour) {
    this.workinghour = workinghour;
}


}

employeeisapersubclass.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="beans.Employee" table ="superemployee" schema="hqlstudent">
    <id name="id"/>


    <property name="name" />
   <property name="email" />
   <property name="salary" />
   <joined-subclass name="beans.SoftwareEmployee" table="softwareemployee">
   <key column ="id"/>
   <property name="tool"/>

   </joined-subclass>
    <joined-subclass name="beans.HardwareEmployee" table="hardwareemployee">
  <key column ="id"/>
   <property name="workinghour"/>

   </joined-subclass>

   <joined-subclass name="beans.Admin" table="admin">
   <key column ="id"/>
   <property name="branchname"/>

   </joined-subclass>
    </class>
    </hibernate-mapping>



hibernateisapersubclass.cfg.xml

<!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>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.hbm2ddl.auto">
      create
   </property>
   <property name="hibernate.show_sql">
      true
   </property>

   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume customstudent is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/hqlstudent
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      password
   </property>

<mapping resource="resources/employeeisapersubclass.hbm.xml"/>


</session-factory>
</hibernate-configuration>

0 个答案:

没有答案