Exception in thread "main" org.hibernate.MappingException - java

Test:
import java.util.GregorianCalendar;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import it.s.monitoringDoor.model.Door_Open;
import it.s.monitoringDoor.model.HibernateUtilities;
public class TestHibernate {
public static void main(String[] args){
SessionFactory factory = HibernateUtilities.getSessionFactory();
Door_Open d = new Door_Open();
d.setId(1);
d.setId_door(1);;
d.setTimestamp(getTime());
Session s = factory.openSession();
Transaction t = s.getTransaction();
t.begin();
s.save(d);
t.commit();
}
private static Date getTime(){
GregorianCalendar gc = new GregorianCalendar();
return gc.getTime();
}
}
HibernateUtilities:
package it.s.monitoringDoor.model;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtilities {
//XML based configuration
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
System.out.println("Hibernate Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null)
sessionFactory = buildSessionFactory();
return sessionFactory;
}
}
The Pojo class Door_Open.java
package it.selco.monitoringDoor.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "door_open")
public class Door_Open {
//---------------- Instance variables ----------------
#Id
#GeneratedValue
#Column(name = "id")
private int id;
#Column(name = "id_door", nullable = false)
private int id_door;
#Column(name = "id_door", nullable = false)
private Date timestamp;
//-------------------- Constructor --------------------
public Door_Open (){
;
}
public Door_Open(int id_door, Date timestamp) {
setId_door(id_door);
setTimestamp(timestamp);
}
//--------------------- Get & Set ---------------------
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#SuppressWarnings("unused")
private int getId_door() {
return id_door;
}
public void setId_door(int id_door) {
this.id_door = id_door;
}
#SuppressWarnings("unused")
private Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
//--------------------- Methods ----------------------
//TODO Auto-generated methods variables block
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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 name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/monitor_door</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- ############################################
# mapping files with external dependencies #
############################################ -->
<mapping resource="it/s/monitoringDoor/model/Door_Open.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Door_Open.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Door_Open" table="door_open" schema="monitor_door">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="id_door" column="id_door" type="int" not-null="true"/>
<property name="timestamp" column="timestamp" type="timestamp" not-null="true"/>
</class>
</hibernate-mapping>
When I run the test is returned me the following error.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: it.s.monitoringDoor.model.Door_Open
[...]
some advice?

Look at the package of your Door_Open class, it is: it.selco.monitoringDoor.model while in hibernate.cfg.xml the Door_Open.hbm.xml file is at: <mapping resource="it/s/monitoringDoor/model/Door_Open.hbm.xml"/>. The package and the mapping resource path do not match, they should be the same e.g. it/selco/monitoringDoor/model/Door_Open.hbm.xml
and of course move the Door_Open.hbm.xml to be in the same path as its class.
Hope this helps.
PS, why are you using both annotations (e.g. #Entity) and hbm mapping for your class? I think you should choose one not both.

Related

Hibernate exception class is not mapped

Hey i am new to hibernate and java and im getting this exception i tried multiple solutions i found on google and nothing seems to work. Would appreciate any help. I am trying to return number of rows from table in database.
I am using mapping in xml file and im using addAnnotatedClass yet it is still throwing exception.
Here is my code:
Main.java
package springfoxdemo.java.swagger;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class Main {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String args[]) {
Session session = null;
try
{
try
{
Configuration cfg= new Configuration().addAnnotatedClass(Polica.class);
cfg.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
}
catch(Throwable th)
{
System.err.println("Failed to create sessionFactory object."
+ th);
throw new ExceptionInInitializerError(th);
}
session = sessionFactory.openSession();
Query query = session.createQuery("select count(p) from Polica p");
Iterator count = query.iterate();
System.out.println("No. of rows : "+ count.next());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
session.close();
}
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">PASS</property>
<property name="hibernate.connection.url">DBCONNECTION</property>
<property name="hibernate.connection.username">USERNAME</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="springfoxdemo.java.swagger.Polica"/>
</session-factory>
</hibernate-configuration>
Polica.java
package springfoxdemo.java.swagger;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
#Entity
public class Polica {
#Id
#GeneratedValue
#Column(name="id")
private int id;
#Column(name="ime_pol")
private String ime_pol;
public Polica() {
super();
}
public Polica(int id, String ime_pol) {
this.id = id;
this.ime_pol = ime_pol;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIme_pol() {
return ime_pol;
}
public void setIme_pol(String ime_pol) {
this.ime_pol = ime_pol;
}
}
Exception:
Polica is not mapped [select count(p) from Polica p]

Why am I facing this error? - java.lang.ExceptionInInitializerError

I am creating a simple Laptop-info form using JSF and Hibernate in IntelliJ IDEA. When I execute my project then I face this error:
javax.servlet.ServletException: java.lang.ExceptionInInitializerError
javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)
Here I list the required files and directory structure.
LaptopDAO.java
package Dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import pojo.Laptop;
import util.HibernateUtil;
public class LaptopDAO {
public void SaveLaptop(Laptop laptop)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
session.save(laptop);
t.commit();
session.close();
}
}
Laptop.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pojo.Laptop" table="laptopdb">
<id name="id"><generator class="increment"></generator></id>
<property name="company">
<column name="laptopcompany"></column>
</property>
<property name="model">
<column name="laptopmodel"></column>
</property>
<property name="ram">
<column name="laptopram"></column>
</property>
<property name="hd">
<column name="laptophd"></column>
</property>
</class>
</hibernate-mapping>
Class LaptopBean
package LaptopMBean;
import Dao.LaptopDAO;
import pojo.Laptop;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "laptopBean")
public class LaptopBean {
Laptop l;
public LaptopBean()
{
l = new Laptop();
}
public Laptop getL()
{
return l;
}
public void setL(Laptop l)
{
this.l = l;
}
public void savetoDB()
{
LaptopDAO ld = new LaptopDAO();
ld.SaveLaptop(l);
System.out.print("Success");
}
}
Class Laptop
package pojo;
public class Laptop {
int id;
String company;
String model;
String ram;
String hd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}
public String getHd() {
return hd;
}
public void setHd(String hd) {
this.hd = hd;
}
}
Class HibernateUtil
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static final SessionFactory sessionFactory = buildSessionFactory();
public static SessionFactory buildSessionFactory()
{
try
{
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed. " + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
File hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//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="connection.url">jdbc:mysql://localhost:3306/laptop</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.pool_size">1</property>
<mapping resource="hbm/laptop.hbm.xml"/>
</session-factory>
</hibernate-configuration>
StackTrace
23:44:51,700 INFO [org.hibernate.validator.internal.util.Version] (default
task-4) HV000001: Hibernate Validator 5.1.3.Final
23:44:52,080 INFO [org.hibernate.Version] (default task-4) HHH000412:
Hibernate Core {5.4.0.Final}
23:44:52,084 INFO [org.hibernate.cfg.Environment] (default task-4)
HHH000206: hibernate.properties not found
23:44:52,091 ERROR [stderr] (default task-4) Initial SessionFactory creation
failed.java.lang.NoClassDefFoundError:
net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver
23:44:52,092 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle]
(default task-4) #{laptopBean.savetoDB()}:
java.lang.ExceptionInInitializerError: javax.faces.FacesException: #
{laptopBean.savetoDB()}:
I have added all libraries. How can I find out what the problem is?

java.lang.NullPointerException in sessionfactory hibernate

I'm trying to build an application using hibernate. The thing is, sessionFactory can't open a session because there is a NullPointerException at Session session = sessionFactory.openSession(); and that's why at myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000); too.
Main class
package myaccount1;
import myaccount.entity1.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.List;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class MyAccount {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static void main(String[] args) {
MyAccount myAccount = new MyAccount();
System.out.println("Adding an element");
myAccount.addElement("August", 50000, 14000, 10000, 10000, 16000);
myAccount.addElement("September", 50000, 14000, 10000, 10000, 16000);
System.out.println("List of elements");
}
public void addElement(String mounth, int salary, int must_save_for_rent, int must_save_for_education, int must_save, int balance) {
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
MyAccountEntity element = new MyAccountEntity(mounth, salary, must_save_for_rent, must_save_for_education, must_save, balance);
session.save(element);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
e.printStackTrace();
}
e.printStackTrace();
} finally {
session.close();
}
}
}
Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/app?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">sopromat</property>
<mapping class="myaccount.entity1.MyAccountEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil class
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory =
buildSessionFactory();
private static ServiceRegistry serviceRegistry;
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
//Use config file path explicitly
//configuration.configure("hibernate/hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory "
+ "creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
Mapping file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="myaccount.entity1.MyAccountEntity" table="myaccount">
<id name="id" type="int" column="id">
<generator class="auto_increment"/>
</id>
<property name="mounth" column="mounth" type="string"/>
<property name="salary" column="salary" type="int"/>
<property name="must_save_for_rent" column="must_save_for_rent" type="int"/>
<property name="must_save_for_education" column="must_save_for_education" type="int"/>
<property name="must_save" column="must_save" type="int"/>
<property name="balance" column="balance" type="int"/>
</class>
</hibernate-mapping>
Project structure
enter image description here
What's wrong?
Please help, i don't know what to do.

org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class=

I am trying to write my first hibernate annotation application and below are the things that i did
package org.hibernate.pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="employee")
public class Employee {
#Id
#GeneratedValue
Integer id;
#Column(name="Employee_Name")
String userName;
#Column(name="Address")
String age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* Hi this is to test java commenting
* #return
*/
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Hibernate.config.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">$Ailaja12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sessions</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<!-- <property name="show_sql">true</property> -->
<!-- <mapping resource="org/hibernate/pojo/Employee.xml"></mapping> -->
<mapping class="org.hibernate.pojo.Employee"/>
<mapping resource="org/hibernate/pojo/Item.xml"></mapping>
</session-factory>
</hibernate-configuration>
Client
package org.hibernate.client;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.pojo.Employee;
public class Client {
/**
* #param args
*/
public static void main(String[] args) {
//Starting hibernate environment in your application
Configuration conf = new Configuration();
//2 Loading hibernate configuration file
conf.configure("hibernate.cfg.xml");
SessionFactory factory=new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
//SessionFactory factory = conf.buildSessionFactory();
Session session = factory.getCurrentSession();
Employee ee = new Employee();
ee.setUserName("Kranthi");
//ee.setId(123);
ee.setAge("Dallas");
Transaction tx = session.beginTransaction();
session.save(ee);
tx.commit();
session.close();
factory.close();
}
}
I am getting below exception
Exception in thread "main" org.hibernate.MappingException: An AnnotationConfiguration instance is required to use
I tried by googling but none of them has worked
and below are the list of jars that i have used
antlr_2.7.6.jar
asm-3.3.1.jar
cglib-2.2.2.jar
domj-1.3.jar
ehcache.jar
hibernate-3.2.jar
javax.persistence.jar
jta.jar
mysql-connector.jar
commonlogging.jar
Add below jar to your class path: hibernate-annotations.3.3.0.GA.jar or add below dependency to your pom file,if you are using maven project
<dependency>
<groupId>hibernate-annotations</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.0.GA</version>
</dependency>

[Hibernate]Error: entity class not found:

I get tired of this for a long time. I do not know what caused this error. Here are my files:
Uzytkownik.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Uzytkownik" table="uzytkownicy">
<id column="id" name="id" type="int"/>
<property column="login" generated="never" lazy="false" name="login" type="string"/>
<property column="haslo" generated="never" lazy="false" name="haslo" type="string"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/sprawozdania</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/vaannila/uzytkownik/Uzytkownik.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I use mysql 5.5.
I get the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
at com.vaannila.uzytkownik.Main.saveUzyt(Main.java:22)
at com.vaannila.uzytkownik.Main.main(Main.java:16)
Caused by: org.hibernate.MappingException: entity class not found: Uzytkownik
This are my classes:
main.java
package com.vaannila.uzytkownik;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.persistence.Entity;
import com.vaannila.util.HibernateUtil;
public class Main {
/**
* #param args
*/
public static void main(String[] args) {
Main obj = new Main();
String uzytkownikLogin = obj.saveUzyt("Adam", "Malysz");
}
public String saveUzyt(String login, String haslo){
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
String uzytLog = null;
try {
transaction = session.beginTransaction();
Uzytkownik uzyt = new Uzytkownik();
uzyt.setLogin(login);
uzyt.setHaslo(haslo);
uzytLog = (String) session.save(uzyt);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return uzytLog;
}
}
Uzytkownik.java:
package com.vaannila.uzytkownik;
// default package
// Generated 2011-07-14 13:39:18 by Hibernate Tools 3.4.0.CR1
/**
* Uzytkownik generated by hbm2java
*/
public class Uzytkownik implements java.io.Serializable {
private int id;
private String login;
private String haslo;
public Uzytkownik() {
}
public Uzytkownik(int id) {
this.id = id;
}
public Uzytkownik(int id, String login, String haslo) {
this.id = id;
this.login = login;
this.haslo = haslo;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getLogin() {
return this.login;
}
public void setLogin(String login) {
this.login = login;
}
public String getHaslo() {
return this.haslo;
}
public void setHaslo(String haslo) {
this.haslo = haslo;
}
}
HibernateUtil.java:
package com.vaannila.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Maybe your mapping file is not complete but other wise it should be:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
=> need to set fully qualified class name (with package)
I think it makes sense to specify full-qualified entity class name:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
Don't forget to mention your class using as a entity classes in hibernate configeration file using the mapping tag !!
Example:
<session-factory>
//database configeration goes here
<mapping class="org.fbis.models.Form3A"/>
</session-factory>
Stijn Geukens answers right,but I want to point out more information about this question.
There are two reasons I know causing this problem: entity class not found
First, As Stijn Geukens answers, Your Hibernate mapping is not right, the value of name attribute for the tag class should be the Java class with package ahead.
Second, if you have boolean filed in your Java class, this field can't start with is.Otherwise, the hibernate throws an exception getter method is not found... when run by Java Debug mode or Run mode.But when it comes to web project and you run your project as Server Application,the message becomes entity class not found.It made me puzzled for a long time.So do not name your boolean field with is ahead.

Categories