org.hibernate.UnknownEntityTypeException: не удается найти персистент: com.entity.Employee, я пытаюсь сделать кэш второго уровня - PullRequest
0 голосов
/ 24 сентября 2019

[пожалуйста, найдите код ниже ссылки, которую я застрял при загрузке класса исключения persister в потоке "main" org.hibernate.UnknownEntityTypeException: невозможно найти persister: com.entity.Employee в org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister (MetamodelImpl.java:637) в org.hibernate.internal.SessionImpl.locateEntityPersister (SessionImpl.java:2947) в org.hibernate.internal.SessionImpl.access $ 1800 (SessionImpl.jternalinh3: 20).SessionImpl $ IdentifierLoadAccessImpl. (SessionImpl.java:2698) в org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl. (SessionImpl.java:2684) в org.hibernate.internal.SessionImpl.byIj.gav (org).hibernate.internal.SessionImpl.load (SessionImpl.java:1065) в com.Hibernatecache.App.main (App.java:30)] [1]

package com.Hibernatecache;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.entity.Employee;

public class App {
    public static void main(String[] args) {

        StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
        Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();

        SessionFactory factory = meta.getSessionFactoryBuilder().build();
        //SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session1 = factory.openSession();   
        System.out.println("load");
        Employee emp1 =  session1.load(com.entity.Employee.class,new Integer(2));
        System.out.println(emp1.getId() + " " + emp1.getAnswername() + " " + emp1.getPostedby());
        session1.close();

        Session session2 = factory.openSession();
        Employee emp2 =  session2.load(Employee.class,2);
        System.out.println(emp2.getId() + " " + emp2.getAnswername() + " " + emp2.getPostedby());
        session2.close();

    }

}


Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: com.entity.Employee
    at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:637)
    at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2947)
    at org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:203)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2698)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2684)
    at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1202)
    at org.hibernate.internal.SessionImpl.load(SessionImpl.java:1065)
    at com.Hibernatecache.App.main(App.java:30)


package com.entity;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Table(name = "ANS")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Employee {


    @Column(name="ID")
    private int id;

    @Column(name="ANSWERNAME")
    private String answername;

    @Column(name="POSTEDBY")
    private String postedby;

    @Column(name="QUESTIONID")
    private int questionid;

    @Column(name="TYPE")
    private int type;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAnswername() {
        return answername;
    }

    public void setAnswername(String answername) {
        this.answername = answername;
    }

    public String getPostedby() {
        return postedby;
    }

    public void setPostedby(String postedby) {
        this.postedby = postedby;
    }

    public int getQuestionid() {
        return questionid;
    }

    public void setQuestionid(int questionid) {
        this.questionid = questionid;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Employee(int id, String answername, String postedby, int questionid, int type) {
        super();
        this.id = id;
        this.answername = answername;
        this.postedby = postedby;
        this.questionid = questionid;
        this.type = type;
    }

}

"- // Hibernate/ Конфигурация гибернации DTD 5.3 // EN "
" http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- <property name="hbm2dll.auto">update</property> -->
        <!-- <property name="hibernate.dialect">org.hibernate.dialect.OracelDialect</property> -->
        <!-- <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> -->
        <!-- <property name="hibernate.connection.username">empDetail</property> -->
        <!-- <property name="hibernate.connection.password">empDetail</property> -->
        <!-- <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> -->
        <!-- <property name="show_sql">true</property> -->

        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.username">empDetail</property>
        <property name="hibernate.connection.password">empDetail</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver </property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>

        <property name="cache.use_second_level_cache">true</property>
        <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

        <mapping class="com.entity.Employee"></mapping>

    </session-factory>
</hibernate-configuration>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...