-->
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.  [ 7 posts ] 
Author Message
 Post subject: Unkown entity runtime error for annotated class
PostPosted: Mon Feb 06, 2006 4:46 am 
Newbie

Joined: Tue Jan 31, 2006 2:30 am
Posts: 12
I have successfully created a simple test project using hibernate and am now in the process of learning hibernate annotations. When I set the precedence of the AnnotationConfiguration to the default "hbm", I don't get any errors. However, when I change it to "class" so that it will use my code annotations, I get an unknown entity error in my Person class.

Hibernate version:
hibernate v3.1.1
hibernate-annotations 3.1 beta8

Mapping documents:
This is my hbm.xml for my Person class
Code:
   <class name="contacts.Person" table="PERSONS">
      <id name="id" column="PERSON_ID">
         <generator class="native"/>
      </id>
      
      <property name="birthDay" type="date" column="BIRTHDAY"/>
      <property name="firstName"/>
      <property name="lastName"/>


my hibernate.cfg.xml
Code:
<hibernate-configuration>
   <session-factory>
      <!-- Database connection settings -->
      <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
      <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
      <property name="connection.username">sa</property>
      <property name="connection.password"></property>
      
      <!-- JDBC connection pool (use the built-in) -->
      <property name="connection.pool_size">1</property>
      
      <!-- SQL dialect -->
      <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
      
      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>
      
      <!-- Disable the second-level cache -->
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      
      <!-- Echo all executed SQL to stdout -->
      <property name="show_sql">true</property>
      
      <!-- Drop and re-create the database schema on startup -->
      <property name="hbm2ddl.auto">update</property>
      
      <mapping class="contacts.Person"/>      
      <mapping resource="contacts/Person.hbm.xml"/>


   </session-factory>


my HibernateUtil
Code:
public class HibernateUtil
{
   private static final SessionFactory   sessionFactory;

   static
   {
      try
      {
         AnnotationConfiguration cfg = new AnnotationConfiguration();
         cfg.configure("hibernate.cfg.xml");
         cfg.setPrecedence("class");
         sessionFactory = cfg.buildSessionFactory();
      }
      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 Session getSession()
      throws HibernateException
   {
      return sessionFactory.openSession();
   }
}

When I set the precedence to "hbm", it works fine. But when I set it to "class", I get the error


Full stack trace of any exception that occurs:
    Exception in thread "main" org.hibernate.MappingException: Unknown entity: ingeni.training.contacts.Person
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:513)
    at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1319)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:89)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:557)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:545)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:541)
    at ingeni.training.manager.PersonManager.addPerson(PersonManager.java:32)
    at ingeni.training.manager.PersonManager.main(PersonManager.java:16)



Can you guys tell me what I did wrong in the way I used hibernate annotations?

_________________
When all else fails - fresh tactics!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:14 am 
Newbie

Joined: Tue Jan 31, 2006 2:30 am
Posts: 12
Here is how I defined the Person class.


Code:
package ingeni.training.contacts;

import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

@Entity
@Table (name="tbl_person")
public class Person
{
   private Long   id;

   private String   firstName;

   private String   lastName;
   
   private Date   birthDay;


   @Temporal (TemporalType.DATE)
   public Date getBirthDay()
   {
      return birthDay;
   }


   public void setBirthDay(Date birthDay)
   {
      this.birthDay = birthDay;
   }


   public String getFirstName()
   {
      return firstName;
   }


   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }


   @Id @GeneratedValue
   public Long getId()
   {
      return id;
   }


   private void setId(Long id)
   {
      this.id = id;
   }


   public String getLastName()
   {
      return lastName;
   }


   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }
}

_________________
When all else fails - fresh tactics!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you map the same entity twice? don't do that

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 1:57 am 
Newbie

Joined: Tue Jan 31, 2006 2:30 am
Posts: 12
In the annotations manual, it said you can have 2 mappings. One using the annotations, the other using the hbm.xml mapping file. If there is a conflict, it will pick the hbm.xml (default option). When I set the setPrecedence property to "hbm" it works fine. But if I change it to "class" so that it will use the annotated values, it gives me an error.

I also tried just removing the hbm.xml mapping file so that my annotations will be used. But it still results in an error

_________________
When all else fails - fresh tactics!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 9:55 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It should work. Can you provide a runnable testcase involving 1 class (annotated and with it's hbm file) that actually fails to load (ie query not possible for eg) on JIRA, I'll have a look

_________________
Emmanuel


Top
 Profile  
 
 Post subject: import statements:
PostPosted: Fri Feb 10, 2006 1:38 am 
Newbie

Joined: Thu Mar 17, 2005 4:57 am
Posts: 4
I had the same problem and when i changed by import statements from
Code:
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

to
Code:
import javax.persistence.Entity;
import javax.persistence.Table;

it worked. I know that this can not be the solution but may help you to determine the problem. So it validates (at least for me) that all of my settings are true for providing the right platform in order to make hibernate work but it does not.. you may see my post about that:

http://forum.hibernate.org/viewtopic.php?t=955374

with a title of "org.hibernate.annotations.* OR import javax.persistence.*"

[/code]

_________________
Kaan Yamanyar


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 11, 2006 1:47 am 
Newbie

Joined: Tue Jan 31, 2006 2:30 am
Posts: 12
Wow, thanks a lot!!!
That fixed my problem.

_________________
When all else fails - fresh tactics!


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