-->
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.  [ 10 posts ] 
Author Message
 Post subject: New "flavour" of the good old MappingException
PostPosted: Sun Feb 15, 2009 10:46 am 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
Hello... Hibernate forum!

I've implemented the example in:

http://www.zabada.com/tutorials/hiberna ... xample.php

simplifying it to only one entity: Book. No relations, no foreign keys, no other tables, nothing else.

But, I get:

Quote:
GRAVE: Unknown entity: com.ibm.wpprs.example.Book; nested exception is org.hibernate.MappingException: Unknown entity: com.ibm.wpprs.example.Book
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.ibm.wpprs.example.Book; nested exception is org.hibernate.MappingException: Unknown entity: com.ibm.wpprs.example.Book
org.hibernate.MappingException: Unknown entity: com.ibm.wpprs.example.Book
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:535)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:534)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:526)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:522)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:612)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:310)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:609)
at com.ibm.wpprs.example.BookHibernateDaoImpl.saveBook(BookHibernateDaoImpl.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:161)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy3.saveBook(Unknown Source)
at com.ibm.wpprs.example.TestMain.main(TestMain.java:23)


Now, I've tried all the traditional:

wrong spelling of the package name. here's my spring configuration xml file:

Quote:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:mem:z</prop>
<prop key="hibernate.connection.username">sa</prop>
<prop key="hibernate.connection.password"></prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.connection.pool_size">1</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.ibm.wpprs.example.Book</value>
</list>
</property>
</bean>



<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor"
autowire="byName" />



<bean id="service" class="com.ibm.wpprs.example.TestSpring">
<property name="subClass">
<ref bean="subClass" />
</property>
</bean>



<bean id="subClass" class="com.ibm.wpprs.example.TestSubClass">
<property name="name">
<value>Quentin</value>
</property>
</bean>



<bean id="bookDaoTarget" class="com.ibm.wpprs.example.BookHibernateDaoImpl"
autowire="byName" />



<bean id="bookDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.ibm.wpprs.example.BookDao</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>bookDaoTarget</value>
</list>
</property>
</bean>



</beans>



The class is definitely com.ibm.wpprs.example.Book.

Then, wrong Entity persistence class. In aaaaall my pojos, I have javax.persistence. Here's my class code:

Quote:
package com.ibm.wpprs.example;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book implements Serializable{

/**
*
*/
private static final long serialVersionUID = -1L;

@Id
@GeneratedValue (strategy=GenerationType.AUTO)
private long bookId;

public long getBookId() {
return bookId;
}

public void setBookId(long bookId) {
this.bookId = bookId;
}

private String title;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

}



I was wondering: could it be a problem with the used jars? Ive put all the jars that were inside Hibernate EntityManager 3.4.0 GA, all the "required" from the core one, the spring ones (I've put also the bean example, and it gives me the output "hello Quentin"), the commons collections, pool and dbcp. I've also tried to put a simple data source:

Quote:

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.hibernate.dialect.HSQLDialect" />
<property name="url" value="jdbc:hsqldb:mem:z" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>


And, tried to switch to my local MySQL instance, but I always got to the same exact error.

From the spring forum, all the topics related to that Exception were directing to here.

I was wondering: is there an exact, accurate list of all the jars I have to import, to make basic examples like that one working?

Thank you, I know it's a HUGE amount of stuff! :D


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 11:03 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
I have a suspicion that your problem could be related to a mixture of Hibernate config and JPA config.

I would expect to see EntityManagerFactory mentioned somewhere, given that the annotations in you Book class are from JPA.


--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 11:18 am 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
Hello Sounie

First of all, thanks for the reply, that was FAST!

Where should it be mentioned? In the spring configuration, you mean? If you have a look at:

http://forum.springframework.org/showth ... actoryBean

I have a very similar problem. I'm using the AnnotationSessionFactoryBean, which usually makes a complete SessionFactory to inject into the, as in my example, HibernateDaoSupport, or used as self standing.

Should I use it somewhere inside my persistent class? (the EntityManagerFactory)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2009 4:06 pm 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
Sorry, I didn't mention that I have also tried the non-autowired version:

Quote:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>



<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.ibm.wpprs.hibernate.Book</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/library</prop>
<prop key="hibernate.connection.username">XXXX</prop>
<prop key="hibernate.connection.password">XXXX</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>



<bean id="service" class="com.ibm.wpprs.example.TestSpring">
<property name="subClass">
<ref bean="subClass" />
</property>
</bean>



<bean id="subClass" class="com.ibm.wpprs.example.TestSubClass">
<property name="name">
<value>Riccardo</value>
</property>
</bean>



<bean id="bookDao" class="com.ibm.wpprs.example.BookHibernateDaoImpl"
parent="hibernateDaoSupport" />



<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


</beans>


but... exact same error...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 8:07 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
I may have been wrong in my previous post.

I've just implemented a very similar setup to what you have been dealing with and can get by without striking the exception that you get.

The versions I have used are:
Spring 2.5.5
Hibernate 3.3.1.GA
Hibernate EntityManager 3.4.0.GA

My config is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
            <prop key="hibernate.connection.url">jdbc:hsqldb:hsql://localhost
            </prop>
            <prop key="hibernate.connection.username">sa</prop>
            <prop key="hibernate.connection.password"></prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
         </props>
      </property>
      <property name="annotatedClasses">
         <list>
            <value>forum.problem.Book</value>
         </list>
      </property>
   </bean>


   <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>


   <bean id="bookDaoTarget" class="forum.problem.BookDAOHibernate" lazy-init="false">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="bookDAO" class="org.springframework.aop.framework.ProxyFactoryBean" lazy-init="false">
      <property name="proxyInterfaces">
         <value>forum.problem.BookDAO</value>
      </property>

      <property name="interceptorNames">
         <list>
            <value>hibernateInterceptor</value>
            <value>bookDaoTarget</value>
         </list>

      </property>
   </bean>

</beans>


BookDAO is simply
Code:
package forum.problem;

public interface BookDAO
{
  public Book getBookById(long id);
}


BookDAOHibernate is:
Code:
package forum.problem;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class BookDAOHibernate extends HibernateTemplate implements BookDAO
{
  public Book getBookById(long id)
  {
    return (Book) this.getSession().load(Book.class, id);
  }
}


Book is:
Code:
package forum.problem;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book implements Serializable
{
  private static final long serialVersionUID = -1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long bookId;


  public long getBookId()
  {
    return bookId;
  }


  public void setBookId(long bookId)
  {
    this.bookId = bookId;
  }

  private String title;


  public String getTitle()
  {
    return title;
  }


  public void setTitle(String title)
  {
    this.title = title;
  }
}


My Driver code is:

Code:
package forum.problem;

import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Driver
{
  /**
   * @param args
   */
  public static void main(String[] args)
  {
    ApplicationContext context = new FileSystemXmlApplicationContext("E:\\Source\\HibernateJPASpring\\applicationConfig.xml");
   
    System.out.println("DEBUG");
   
    SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
    System.out.println("sessionFactory = " + sessionFactory);
    BookDAO bookDao = (BookDAO)context.getBean("bookDaoTarget");
    Book book = bookDao.getBookById(1);
   
    System.out.println("DEBUG book: " + book);
  }
}

which just spits out the an exception when it doesn't find a book with the specified id.

The jars in my project are:

hibernate3.jar
spring.jar
javax.persistence_1.0.0.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-simple-1.5.6.jar
slf4j-api-1.5.6.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hsqldb.jar

The main differences that I can see are, I explicitly specify the injection of sessionFactory into my beans, and I may have different jars - or even a different version of Spring / Hibernate.



--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 2:07 pm 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
Hello Stephen

Today, I'll go and test it. It goes without saying that, if it's the solution, you become officially "da man" ;D

Thx, looking forward to try your implementation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 3:16 pm 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
Back from the tests...

IT DIDN'T WORK! I was pretty sure I had done everything allright, this time!

But anyway, you gave me the clue about the libraries. It must be something related to the included jars. In fact, it's the only difference I find, between yours and my project.

I think I'll go for a build with Maven as well.

Thanks for your help Stephen, you even tried to meet my hard-headed understanding capabilities emulating my exercise, I definitely appreciate that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 3:20 pm 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Sorry to hear it didn't work for you, but I'm curious what is different about your environment.

I set up a project from scratch, pulling in the various jars as they were needed (usually discovered by various exceptions being thrown).

I didn't use maven or any other tools to sort out dependencies, so what you see listed in my post is exactly what I have in my project.

I could email you a zipped copy of my project if you would like to try it out?

--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 5:35 pm 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
It'd be lovely of you. Please send it here:

xxxxx

So, finally, I can understand which is the missing part in my MIND!

The automation of the build, and the resolution of the dependencies, is something mandatory for me, Stephen. I really need something that puts in place those jar hyerarchies automatically. Otherwise, like for this issue, I remain struck for weeks, which is way too much.

Again, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 9:38 pm 
Newbie

Joined: Sun Feb 15, 2009 9:50 am
Posts: 7
Location: Amterdam
So, yet we have a quite reasonable culprit: if I put the jar I have in the brand new spring-framework-2.5.6 archive, I get this:

Code:
GRAVE: Error registering bean with name 'sessionFactory' defined in file [C:\workspace2\NewMinimal\src\spring\springexample.xml]: Bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] not found; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'sessionFactory' defined in file [C:\workspace2\NewMinimal\src\spring\springexample.xml]: Bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] not found; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
   at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
   at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Class.java:247)
   at org.springframework.util.ClassUtils.forName(ClassUtils.java:84)
   at org.springframework.beans.factory.support.BeanDefinitionReaderUtils.createBeanDefinition(BeanDefinitionReaderUtils.java:61)
   at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitionElement(DefaultXmlBeanDefinitionParser.java:331)
   at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitionElement(DefaultXmlBeanDefinitionParser.java:299)
   at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitions(DefaultXmlBeanDefinitionParser.java:236)
   at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.registerBeanDefinitions(DefaultXmlBeanDefinitionParser.java:185)
   at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:188)
   at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:146)
   at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:99)
   at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:102)
   at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:70)
   at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:87)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:264)
   at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:82)
   at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:67)
   at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:58)
   at com.ibm.wpprs.example.TestMain.main(TestMain.java:18)


While instead, with yours, it commits a book, and then retrieve it afterwards, correctly.

Probably, I'm missing the correct hyerarchies to respect.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.