-->
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.  [ 4 posts ] 
Author Message
 Post subject: Error with hibernate annotations: Unknown entity: test.Addre
PostPosted: Thu Aug 09, 2007 11:08 pm 
Newbie

Joined: Tue Dec 05, 2006 7:40 am
Posts: 14
Hi all,

I was try make simple application with hibernate annotations, I use eclipse and I have all jar in classpath.

the code
Code:
package test;


import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.Entity;


@Entity
public class Address {
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private int id;
   
   private String street;
   private String state;
   private String zipCode;
   private String city;
   private String aptNumber;
//get and setters
}



the main class:

Code:
package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class Main {
   
   public static void main(String args[]) {
                        
      new Main().addAddress();
      
   }
   
   public void addAddress() {
      
      
            
      AnnotationConfiguration cfg = new AnnotationConfiguration();
      
      cfg.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
      cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
      cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test");
      cfg.setProperty("hibernate.current_session_context_class", "thread");
      cfg.setProperty("hibernate.connection.username", "root");
      cfg.setProperty("hibernate.connection.password", "alex");
      cfg.setProperty("hibernate.show_sql", "true");
      cfg.setProperty("hibernate.hbm2ddl.auto", "true");                  
      
      cfg.addAnnotatedClass(test.Address.class);
            
      SessionFactory sessionFactory = cfg.buildSessionFactory();
      
      Session session = sessionFactory.getCurrentSession();
      
      session.beginTransaction();
      test.Address address = new test.Address();
      
      session.save(address);
      
      session.getTransaction().commit();
      
      sessionFactory.close();
      
                  
   }
   
}



but I get error:

Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Unknown entity: test.Address
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)
   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:530)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
   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.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy4.save(Unknown Source)
   at test.Main.addAddress(Main.java:41)
   at test.Main.main(Main.java:12)


thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 1:19 pm 
Newbie

Joined: Fri Aug 15, 2008 3:44 pm
Posts: 3
Hi, did you solve the problem? I tried the same and got the same error...
thx for any reply

output:
Code:
16:56:25,867  INFO Version:15 - Hibernate Annotations 3.4.0.CR2
16:56:25,889  INFO Environment:543 - Hibernate 3.3.0.GA
16:56:25,894  INFO Environment:576 - hibernate.properties not found
16:56:25,901  INFO Environment:709 - Bytecode provider name : javassist
16:56:25,909  INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
16:56:26,007  INFO Version:14 - Hibernate Commons Annotations 3.1.0.CR2
16:56:26,011  INFO Configuration:1460 - configuring from resource: /hibernate.cfg.xml
16:56:26,012  INFO Configuration:1437 - Configuration resource: /hibernate.cfg.xml
16:56:26,146  INFO Configuration:1575 - Configured SessionFactory: null
16:56:26,147 DEBUG InitSessionFactory:41 - add class - cfg.addAnnotatedClass(Terrarium.class);
16:56:26,166  INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
16:56:26,271  INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
16:56:26,278  INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
16:56:26,278  INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
16:56:26,281  INFO DriverManagerConnectionProvider:68 - autocommit mode: false
16:56:26,301  INFO DriverManagerConnectionProvider:103 - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/testdb
16:56:26,302  INFO DriverManagerConnectionProvider:109 - connection properties: {user=testuser, password=****}
16:56:26,449  INFO SettingsFactory:116 - RDBMS: PostgreSQL, version: 8.3.3
16:56:26,450  INFO SettingsFactory:117 - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.3 JDBC3 with SSL (build 603)
16:56:26,468  INFO Dialect:175 - Using dialect: org.hibernate.dialect.PostgreSQLDialect
16:56:26,533  INFO TransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
16:56:26,536  INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:56:26,536  INFO SettingsFactory:170 - Automatic flush during beforeCompletion(): disabled
16:56:26,540  INFO SettingsFactory:174 - Automatic session close at end of transaction: disabled
16:56:26,541  INFO SettingsFactory:181 - JDBC batch size: 15
16:56:26,542  INFO SettingsFactory:184 - JDBC batch updates for versioned data: disabled
16:56:26,543  INFO SettingsFactory:189 - Scrollable result sets: enabled
16:56:26,543  INFO SettingsFactory:197 - JDBC3 getGeneratedKeys(): disabled
16:56:26,544  INFO SettingsFactory:205 - Connection release mode: auto
16:56:26,545  INFO SettingsFactory:232 - Default batch fetch size: 1
16:56:26,546  INFO SettingsFactory:236 - Generate SQL with comments: disabled
16:56:26,546  INFO SettingsFactory:240 - Order SQL updates by primary key: disabled
16:56:26,546  INFO SettingsFactory:244 - Order SQL inserts for batching: disabled
16:56:26,547  INFO SettingsFactory:420 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:56:26,555  INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
16:56:26,556  INFO SettingsFactory:252 - Query language substitutions: {}
16:56:26,557  INFO SettingsFactory:257 - JPA-QL strict compliance: disabled
16:56:26,557  INFO SettingsFactory:262 - Second-level cache: enabled
16:56:26,557  INFO SettingsFactory:266 - Query cache: disabled
16:56:26,562  INFO SettingsFactory:405 - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
16:56:26,562  INFO RegionFactoryCacheProviderBridge:61 - Cache provider: org.hibernate.cache.EhCacheProvider
16:56:26,569  INFO SettingsFactory:276 - Optimize cache for minimal puts: disabled
16:56:26,569  INFO SettingsFactory:285 - Structured second-level cache entries: disabled
16:56:26,580  INFO SettingsFactory:314 - Statistics: disabled
16:56:26,580  INFO SettingsFactory:318 - Deleted entity synthetic identifier rollback: disabled
16:56:26,582  INFO SettingsFactory:333 - Default entity-mode: pojo
16:56:26,582  INFO SettingsFactory:337 - Named query checking : enabled
16:56:26,707  INFO SessionFactoryImpl:187 - building session factory
16:56:26,739 DEBUG CacheManager:239 - Configuring ehcache from classpath.
16:56:26,745  WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/home/adi/workspace/jars/hibernate/ehcache-1.2.3.jar!/ehcache-failsafe.xml
16:56:26,746 DEBUG ConfigurationFactory:87 - Configuring ehcache from URL: jar:file:/home/adi/workspace/jars/hibernate/ehcache-1.2.3.jar!/ehcache-failsafe.xml
16:56:26,747 DEBUG ConfigurationFactory:139 - Configuring ehcache from InputStream
16:56:26,773 DEBUG DiskStoreConfiguration:65 - Disk Store Path: /tmp
16:56:26,789 DEBUG ConfigurationHelper:208 - No CacheManagerEventListenerFactory class specified. Skipping...
16:56:26,790 DEBUG ConfigurationHelper:183 - No CachePeerListenerFactoryConfiguration specified. Not configuring a CacheManagerPeerListener.
16:56:26,790 DEBUG ConfigurationHelper:159 - No CachePeerProviderFactoryConfiguration specified. Not configuring a CacheManagerPeerProvider.
16:56:26,802 DEBUG ConfigurationHelper:135 - No BootstrapCacheLoaderFactory class specified. Skipping...
16:56:26,812  INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
16:56:26,854  INFO SchemaExport:226 - Running hbm2ddl schema export
16:56:26,855  INFO SchemaExport:251 - exporting generated schema to database
16:56:26,873  INFO SchemaExport:268 - schema export complete
Exception in thread "main" org.hibernate.MappingException: Unknown entity: de.budisantoso.test.Terrarium
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1365)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
   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.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
   at $Proxy5.save(Unknown Source)
   at de.budisantoso.test.RunHibernateTest.createTerrarium(RunHibernateTest.java:40)
   at de.budisantoso.test.RunHibernateTest.main(RunHibernateTest.java:21


Last edited by ktb on Sat Aug 16, 2008 1:21 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 1:19 pm 
Newbie

Joined: Fri Aug 15, 2008 3:44 pm
Posts: 3
Hi, did you solve the problem? I tried the same and got the same error...
thx for any reply

output:
Code:
...


edit: sorry for double posting


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 16, 2008 2:18 pm 
Newbie

Joined: Fri Aug 15, 2008 3:44 pm
Posts: 3
found the solution ... it's simple:

Code:
import javax.persistemce.Entity;


and use @org.hibernate.annotations.Entity only in addition to @javax.persistemce.Entity, eg:

Code:
import java.io.Serializable;

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

@Entity
@org.hibernate.annotations.Entity(mutable=true, dynamicInsert=true, dynamicUpdate=true)
@SequenceGenerator(name="myclass_seq", sequenceName="myclass_id_seq")
public class MyClass implements Serializable {

    private static final long serialVersionUID = 7628430776579817658L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "myclass_seq")
    private Long id;
    private String name;

// ...

}


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