1

I am going through some example of a hibernate book. But i am not able to load data based on entity name. below are the details

The entity class

@Entity
public class SimpleNaturalIdEmployee implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;
    @NaturalId
    Integer badge;
    String name;

    public SimpleNaturalIdEmployee() {
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getBadge() {
        return badge;
    }

    public void setBadge(Integer badge) {
        this.badge = badge;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SimpleNaturalIdEmployee)) return false;

        SimpleNaturalIdEmployee that = (SimpleNaturalIdEmployee) o;

        if (badge != null ? !badge.equals(that.badge) : that.badge != null) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (badge != null ? badge.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}

my hibernate configuration file

<?xml version="1.0"?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/rd?autoReconnect=true</property>

        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- set up c3p0 for use -->
        <property name="c3p0.max_size">10</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping class="chapter06.primarykey.after.Book"/>
        <mapping class="chapter06.compoundpk.CPKBook"/>
        <mapping class="chapter06.compoundpk.EmbeddedPKBook"/>
        <mapping class="chapter06.compoundpk.IdClassBook"/>
        <mapping class="chapter06.twotables.Customer"/>

        <mapping class="chapter06.mappedsuperclass.ComputerBook"/>

        <mapping class="chapter06.naturalid.Employee"/>
        <mapping class="chapter06.naturalid.SimpleNaturalIdEmployee"/>
    </session-factory>
</hibernate-configuration>

I using test class to run few scenario

@Test
public void testSimpleNaturalId() {
    Integer id = 107;

    try (Session session = getSession()) {
        Transaction tx = session.beginTransaction();

        SimpleNaturalIdEmployee employee =
                session
                        .byId(SimpleNaturalIdEmployee.class)
                        .load(id);
        assertNotNull(employee);
        SimpleNaturalIdEmployee badgedEmployee =
                session
                        .bySimpleNaturalId(SimpleNaturalIdEmployee.class)
                        .load(541011);
     //   assertEquals(badgedEmployee, employee);
        SimpleNaturalIdEmployee badgedEmployee1 =
                (SimpleNaturalIdEmployee) session.byId("SimpleNaturalIdEmployee").load(id);

        tx.commit();
    }
}

I am using hibernate 5 and i have tried all suggested way to creating a session but noting worked

Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new 
StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry registry = builder.build();
factory = configuration.addAnnotatedClass(SimpleNaturalIdEmployee.class).buildSessionFactory(registry);
return factory.openSession();

But still i get the below exception

org.hibernate.UnknownEntityTypeException: Unable to locate persister: SimpleNaturalIdEmployee at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:647) at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2946) at org.hibernate.internal.SessionImpl.access$1700(SessionImpl.java:203) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.(SessionImpl.java:2689) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.(SessionImpl.java:2679) at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1195)

I am trying to load using entity name Please help

Jin Lee
  • 3,194
  • 12
  • 46
  • 86
Learner
  • 237
  • 4
  • 15
  • Where is your xml configuration file located and what is its name? – Ioannis Mitrolios Sep 13 '17 at 09:46
  • The hibernate-cfg.xml is located under test resources folder of project – Learner Sep 13 '17 at 10:14
  • 1
    https://stackoverflow.com/questions/32197562/unknownentitytypeexception-unable-to-locate-persister-hibernate-5-0 Check this might help! – Ioannis Mitrolios Sep 13 '17 at 10:28
  • Is SimpleNaturalIdEmployee actually part of the package chapter06.naturalid? There is a package declaration missing in your class name (not sure if you just omitted it in this post), where there should be package chapter06.naturalid. –  Sep 14 '17 at 01:04
  • Yes the class is visible – Learner Sep 14 '17 at 06:34

0 Answers0