-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: "Unknown entity" exception driving me nuts
PostPosted: Fri Sep 30, 2005 8:33 pm 
Newbie

Joined: Sun Jun 12, 2005 11:30 am
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 8:46 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
change

Code:
Criteria criteria = session.createCriteria("from Honey");


to

Code:
Criteria criteria = session.createCriteria(Honey.class);


or

Code:
Criteria criteria = session.createCriteria("com.package.Honey");


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 4:12 am 
Newbie

Joined: Sun Jun 12, 2005 11:30 am
Posts: 2
10q so much :)

I guess I had gone blind :)
How can I rate your reply?

Thanks allot,
Asaf


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.