Link to home
Start Free TrialLog in
Avatar of stephan_zehnder
stephan_zehnder

asked on

Hibernate: no persistent classes found for query class: from mapped.PersistentTable1

I'm doing a little test with hibernate3 and annotations. Here is what i have:

================================== configuration =============================
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql:horizon://localhost</property>
    <property name="connection.username">nh_user</property>
    <property name="connection.password">nh_user</property>
    <property name="dialect ">org.hibernate.dialect.ProgressDialect</property>
    <!-- property name="cache.provider_class">net.sf.ehcache.hibernate.Provider</property-->
    <property name="show_sql">true</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
   
   
    <!-- mapping package="mapped" /-->
    <mapping class="mapped.PersistentTable1" />
    <mapping class="mapped.PersistentTable2" />
  </session-factory>
</hibernate-configuration>


========================================== PersistentTable1 ===========================================
/*
 * $Id$
 *
 * Copyright (c) 2006 Nexus Telecom AG
 * Feldbachstrasse, 8634 Hombrechtikon, Switzerland
 * All Rights Reserved.
 */
package mapped;

import java.util.Set;

import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

@Entity
@Table(name="table1")
public class PersistentTable1 {

  private int id;
  private String text;
  private Set table2s;
 
  public PersistentTable1() {
  }

  @Id
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  @OneToMany
  @JoinColumn(name="table1_id")
  public Set getTable2s() {
    return table2s;
  }

  public void setTable2s(Set table2s) {
    this.table2s = table2s;
  }
 

}

========================================== PersistentTable2 ===========================================
...


========================================== Main ===================================================
public class Main {

  public static void main(String[] args) {
    Session session = HibernateUtil.getSession();
    Query query = session.createQuery("from mapped.PersistentTable1");
    List<PersistentTable1> table1s = (List<PersistentTable1>)query.list();
   
    System.out.println("");
  }
}

========================================== log ===============================================

16:12:37,732  INFO Environment:479 - Hibernate 3.1.1
16:12:37,747  INFO Environment:509 - hibernate.properties not found
16:12:37,794  INFO Environment:525 - using CGLIB reflection optimizer
16:12:37,809  INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
16:12:37,918  INFO Configuration:1296 - configuring from resource: /hibernate.cfg.xml
16:12:37,918  INFO Configuration:1273 - Configuration resource: /hibernate.cfg.xml
16:12:38,323  INFO Configuration:1407 - Configured SessionFactory: null
16:12:38,385  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
16:12:38,385  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
16:12:38,385  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
16:12:38,401  INFO DriverManagerConnectionProvider:80 - using driver: org.postgresql.Driver at URL: jdbc:postgresql:horizon://localhost
16:12:38,401  INFO DriverManagerConnectionProvider:86 - connection properties: {user=------, password=****}
16:12:38,603  INFO SettingsFactory:77 - RDBMS: PostgreSQL, version: 8.1.1
16:12:38,603  INFO SettingsFactory:78 - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.0 JDBC3g with SSL (build 311)
16:12:38,634  INFO Dialect:103 - Using dialect: org.hibernate.dialect.PostgreSQLDialect
16:12:38,712  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
16:12:38,712  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:12:38,712  INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
16:12:38,728  INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
16:12:38,728  INFO SettingsFactory:136 - JDBC batch size: 15
16:12:38,728  INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
16:12:38,728  INFO SettingsFactory:144 - Scrollable result sets: enabled
16:12:38,728  INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled
16:12:38,728  INFO SettingsFactory:160 - Connection release mode: auto
16:12:38,728  INFO SettingsFactory:187 - Default batch fetch size: 1
16:12:38,728  INFO SettingsFactory:191 - Generate SQL with comments: disabled
16:12:38,728  INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
16:12:38,728  INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:12:38,728  INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
16:12:38,728  INFO SettingsFactory:203 - Query language substitutions: {}
16:12:38,728  INFO SettingsFactory:209 - Second-level cache: enabled
16:12:38,728  INFO SettingsFactory:213 - Query cache: disabled
16:12:38,743  INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.NoCacheProvider
16:12:38,743  INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
16:12:38,743  INFO SettingsFactory:237 - Structured second-level cache entries: disabled
16:12:38,743  INFO SettingsFactory:257 - Echoing all SQL to stdout
16:12:38,743  INFO SettingsFactory:264 - Statistics: disabled
16:12:38,743  INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
16:12:38,743  INFO SettingsFactory:283 - Default entity-mode: pojo
16:12:38,790  INFO SessionFactoryImpl:153 - building session factory
16:12:38,790  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
16:12:38,868  WARN QuerySplitter:116 - no persistent classes found for query class: from mapped.PersistentTable1

Why isn't it finding the persistent class? The configuration is in the classpath.
Avatar of girionis
girionis
Flag of Greece image

Make sure that you have the right mapping entry in the hibernate.cfg.xml
Avatar of stephan_zehnder
stephan_zehnder

ASKER

They should be correct. Or do you see a problem?

I also tried with:

sessionFactory = new AnnotationConfiguration().addAnnotatedClass(PersistentTable1.class).addAnnotatedClass(PersistentTable2.class).configure().buildSessionFactory();

but doesn't help eighter.

Any help?
Do you have a PersistentTable1.hbm.xml file? Where do you map this class (PersistentTable1.class) to the corresponding table in the database? Also are you using an application server as JBoss or such or a standard java application? If you are using a JBoss do you have a .har file? If yes you might need to use the exploded structure in order to run your web application.
It is mapped with annotation within the class files, there are no *.hbm.xml files. It's a standard java application. Just a very small test (I thought).
During runtime, is the mappded.PersistentObject1 class present in the classpath ?
For some reason it does not map the fields to the corresponding table in the database. Not sure what's going on, I have yet to use annotations with hibernate. Try to annotate the fields when they are declared instead when you use them at the methods, eg:

@id
private int id;

etc

ALso have a look at the documentation about annotations, it might give you some clues: http://www.hibernate.org/hib_docs/annotations/reference/en/html/

If you still have problems post again.
Not sure but while using annotation the corresponding *.hbm.xml files get created during compile time. dont you need to add that to the cfg.xml ? Or does the cfg.xml also gets modified.

Just guessing as I havent used Hibernate with Annotations.
yes it is in classpath. no this hbm.xml files aren't created during compile time, the information goes into the class files. what you mean is xdoclet, thats different. annotaions are with java 5.0.
I hope that your app is picking the right hibernate.cfg.xml to start off  with and not some other configuration file :).
no it's not i think, because if i change something in it it gives an exception. but the idea is good ;-)
What version of hibernate do you have? I think annotations are available to version 3.1.1 and above.
And make sure that hibernate-annotations.jar fiel is in your classpath.

If all else fails have a look here: http://www.hibernate.org/hib_docs/annotations/reference/en/html/ch01.html it has a simple tutorial on how to use annotations with hibernate.
It is in the classpath. This is the tutorial I used to do it.
I am not sure what else could be, only thing I can think of is that the fields in the database do not correspond properly to the mapping. Check the database and see if all is ok there. As a last try start over with another tutorial: http://uk.builder.com/programming/java/0,39026606,39295358,00.htm
The other thing you can do is to try the same application you have but with only one field in your java object and in your database. It will give you more clues as to where the problem is.
Out of curiosity what java version do you use?
Java version 1.5.

The other thing I tried nothing different. Ts, so weird this stuff.

Well I am stumped. I too am curious to see what other people can say on this.
Let me give another try :

The mapped.PersistentObject1 might be making references to some other classes as well. Are all the classes(imports) in the heirearchy accessible to the Class Loader that loads the mapped.PersistentObject1 class else class loading will fail.

Are you using .har files. In that case you can try to package all classes of the app in that file and test it out.
And maybe specify the classpath in the .har file for hibernate classes.
Yes they are all in the classpath. No har deployment is used.
Are all classes in the system classpath ?
Or any files under the WEB-INF/classes etc etc
OK I get it.. you  are using main so everything is in system classpaht.
yep.
I went back and tried this with mapping files (*.hbm.xml) then it works fine. Still haven't got an idea what the problem with the annotations is.
Finally I got it!!!

The problem is:
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Table;

Should be:
import javax.persistence.Entity;
import javax.persistence.Table;
Nice one :) Please ask a moderator to PAQ this question and refund the points back to you.
Please PAQ
ASKER CERTIFIED SOLUTION
Avatar of mrigank
mrigank

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In the Annotation FAQ I found something about the package. In the examples for Annotations you never see the package.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is it possible to split the points between them?:
girionis, mrigank

They took a lot of time for this.

Thx.