Hi All,
I'm getting this exception and i'm stuck with it four hours!!
The problem I encountered occures when i'm running to the point:
List honeys = criteria.list();
in function named: listHoney under class TestClient.
It must be something i'm not seeing...
And I apologize if its something stupid (and i'm sure it is).
But I need help with this... another person to have a look at it and tell me whats wrong wit this code.
I would realy appreciate it if anyone would try to run this code.
Hibernate version:
3.0.5
Mapping documents:
hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">a5168248</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mapping files -->
<mapping resource="de/laliluna/example/Honey.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Honey.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="de.laliluna.example">
<class name="Honey" table="honey">
<id name="id" column="id" type="java.lang.Integer">
<!-- postgre: <generator class="sequence">
<param name="sequence">honey_id_seq</param>
</generator> -->
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="taste" column="taste" type="java.lang.String" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
HibernateSessionFactory.java:
package de.laliluna.example;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* * Configures and provides access to Hibernate sessions, tied to the * current
* thread of execution. Follows the Thread Local Session * pattern, see
* {@link
http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* * Location of hibernate.cfg.xml file. * NOTICE: Location should be on the
* classpath as Hibernate uses * #resourceAsStream style lookup for its
* configuration file. That * is place the config file in a Java package -
* the default location * is the default Java package.<br>
* <br> * Examples: <br> *
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static SessionFactory sessionFactory;
/**
* * Returns the ThreadLocal Session instance. Lazy initialize * the
* <code>SessionFactory</code> if needed. * *
*
* @return Session *
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isConnected()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* * Close the single hibernate session instance. * *
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/** * Default constructor. */
private HibernateSessionFactory() {
}
}
Honey.java:
package de.laliluna.example;
public class Honey {
private Integer id;
private String name;
private String taste;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
}
TestClient.java (the file which contains the
main function:
package de.laliluna.example;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
//import net.sf.hibernate.HibernateException;
//import net.sf.hibernate.Session;
//import net.sf.hibernate.Transaction;
/**
* @author laliluna
*
*/
public class TestClient {
public static void main(String[] args) {
Integer primaryKey = createHoney();
System.out.println("primary key is " + primaryKey);
updateHoney(primaryKey);
listHoney();
}
private static Integer createHoney() {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("creating honey");
// [laliluna] our returned primary key
Integer id = null;
try {
// [laliluna] get the session from the factory
session = HibernateSessionFactory.currentSession();
// [laliluna] always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
// [laliluna] create a new object
Honey honey = new Honey();
honey.setName("Sebastian's favourite honey");
honey.setTaste("sweet");
// [laliluna] save it to the database, Hibernate returns your object
// with the primary key field updated!
id = (Integer) session.save(honey);
// [laliluna] commit your transaction or nothing is wrote to the db
tx.commit();
// [laliluna] clean up (close the session)
session.close();
} catch (HibernateException e) {
// [laliluna] when an error occured, try to rollback your
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* [laliluna] close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
return id;
}
private static void updateHoney(Integer primaryKey) {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("updating honey");
try {
// [laliluna] get the session from the factory
session = HibernateSessionFactory.currentSession();
// [laliluna] always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
// [laliluna] load object from the database
Honey honey = (Honey) session.get(Honey.class, primaryKey);
// [laliluna] honey is null when no object was found
if (honey != null) {
honey.setName("Sascha's favourite honey");
honey.setTaste("very sweet");
}
session.flush();
// [laliluna] commit your transaction or nothing is wrote to the db
tx.commit();
// [laliluna] clean up (close the session)
session.close();
} catch (HibernateException e) {
// [laliluna] when an error occured, try to rollback your
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* [laliluna] close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
}
private static void listHoney() {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("listing honey");
try {
// [laliluna] get the session from the factory
session = HibernateSessionFactory.currentSession();
// [laliluna] always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
Criteria criteria = session.createCriteria("from Honey");
List honeys = criteria.list();
for (Iterator iter = honeys.iterator(); iter.hasNext();) {
Honey honey = (Honey) iter.next();
System.out.println("Id " + honey.getId() + " Name " + honey.getName());
}
// [laliluna] commit your transaction or nothing is wrote to the db
tx.commit();
// [laliluna] clean up (close the session)
session.close();
} catch (HibernateException e) {
// [laliluna] when an error occured, try to rollback your
e.printStackTrace();
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* [laliluna] close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
}
}
Full stack trace of any exception that occurs:
org.hibernate.MappingException: Unknown entity: from Honey
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.impl.SessionImpl.getOuterJoinLoadable(SessionImpl.java:1337)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at de.laliluna.example.TestClient.listHoney(TestClient.java:160)
at de.laliluna.example.TestClient.main(TestClient.java:27)
Name and version of the database you are using:
MySql 5.0