# hibernate mit postgreSQL



## DirtyBit (7. Jun 2019)

Hallo,
ich versuche mich gerade in hibernate einzuarbeiten und habe dafür versucht ein Objekt einer "Employee" Klasse in meine PostgreSQL datenbank zu laden. Allerdings erhalte ich folgenden Fehler: Exception in thread "main" org.hibernate.boot.InvalidMappingException: class org.dirtybit.Employee not found while looking for property: id

Ich arbeite mit Eclipse und haben ein Maven Projekt erstellt vom Typ "maven-archetype-webapp" version 1.0.


Spoiler: Ordnerstruktur









Spoiler: hibernate.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.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">123</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property>



        <property name="connection_pool_size">1</property>

        <property name="hbm2ddl.auto">create</property>

        <property name="show_sql">true</property>



       <mapping resource="employee.hbm.xml"/>

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






Spoiler: employee.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="org.dirtybit.Employee" table="EMPLOYEE">
    <id name="id">
    <generator class="assigend"></generator>
    </id>

    <property name="firstName"></property>
    <property name="lastName"></property>
</class>

</hibernate-mapping>
```







Spoiler: StoreData.java





```
package org.dirtybit.playground;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class StoreData {
    public static void main(String[] args) {
        
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        
        Transaction tx = session.beginTransaction();
        Employee e = new Employee();
        e.setFirstName("Bob");
        e.setLastName("Baumeister");
        e.setId(101);
        session.save(e);
        tx.commit();
        session.close();
    }
}
```






Spoiler: Employee.java





```
package org.dirtybit.playground;

public class Employee { 
    private int id; 
    private String firstName,lastName; 
    
    protected Employee() { }
    
    public int getId() { 
        return id; 
    } 
    public void setId(int id) { 
        this.id = id; 
    } 
    public String getFirstName() { 
        return firstName; 
    } 
    public void setFirstName(String firstName) { 
        this.firstName = firstName; 
    } 
    public String getLastName() { 
        return lastName; 
    } 
    public void setLastName(String lastName) { 
        this.lastName = lastName; 
    } 
      
      
    }
```






Spoiler: Console Output



Juni 07, 2019 1:55:57 NACHM. org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.3.Final}
Juni 07, 2019 1:55:58 NACHM. org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Juni 07, 2019 1:55:58 NACHM. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Juni 07, 2019 1:55:58 NACHM. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbcostgresql://localhost:5432/postgres]
Juni 07, 2019 1:55:58 NACHM. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, pool_size=1, user=postgres}
Juni 07, 2019 1:55:58 NACHM. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Juni 07, 2019 1:55:58 NACHM. org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Juni 07, 2019 1:55:58 NACHM. org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Exception in thread "main" org.hibernate.MappingException: class org.dirtybit.Employee not found while looking for property: id
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:221)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:513)
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindSimpleEntityIdentifier(ModelBinder.java:729)
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindEntityIdentifier(ModelBinder.java:351)
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindRootEntity(ModelBinder.java:246)
    at org.hibernate.boot.model.source.internal.hbm.ModelBinder.bindEntityHierarchy(ModelBinder.java:193)
    at org.hibernate.boot.model.source.internal.hbm.HbmMetadataSourceProcessorImpl.processEntityHierarchies(HbmMetadataSourceProcessorImpl.java:144)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:226)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at org.dirtybit.playground.StoreData.main(StoreData.java:9)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.dirtybit.Employee]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:136)
    at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:217)
    ... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.dirtybit.Employee
    at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:415)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133)
    ... 15 more



Eclipse sagt er findet die Klasse Employee nicht während er nach der ID sucht.  Ich werde daraus nicht schlau. ich habe die Klassen und xml files nach bestem wissen und gewissen gestaltet.

hoffe auf einen Tipp.
freundliche Grüße,
DirtyBit


----------



## thecain (7. Jun 2019)

DirtyBit hat gesagt.:


> <class name="org.dirtybit.Employee" table="EMPLOYEE">


da fehlt doch ein playground im name


----------



## DirtyBit (11. Jun 2019)

thecain hat gesagt.:


> da fehlt doch ein playground im name


 vielen Dank, das hat geholfen. 
Ich hätte eine weitere Frage.
Was ist performanter bzw. besserer Stil: CriteriaQueries / HQL zu benutzen oder die gesamte Tabelle als Liste zu holen und in Java in einer schleife  diejenigen Eemente raussuchen die man gerade braucht ?


----------



## M.L. (12. Jun 2019)

Vielleicht hilft dieses Video bei (Folge)Problemen weiter: Hibernate & PostgreSQL : 6 Things You Need To Know


----------

