-->
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.  [ 9 posts ] 
Author Message
 Post subject: Query problem
PostPosted: Sun Apr 11, 2010 8:29 pm 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
Hi all,
Im using hibernate in netbeans to map to a mySQL database.
I having problems with my query...
Code:
user = (Users) session.createQuery("from Users users where UserName=:userName")
                    .setString("userName", userName)
                    .uniqueResult();
            session.getTransaction().commit();

I ran this query in the HQL editor...and got the result I wanted. but when I run it in the code above, it throws this exception:
Exception in thread "AWT-EventQueue-0" org.hibernate.hql.ast.QuerySyntaxException: Users is not mapped [from Users users where UserName=:userName]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
.............

thanks in advance.


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Sun Apr 11, 2010 8:54 pm 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
and another thing..
what is this?...
12/04/2010 10:52:08 AM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
12/04/2010 10:52:08 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
12/04/2010 10:52:08 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
12/04/2010 10:52:08 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
12/04/2010 10:52:08 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
12/04/2010 10:52:08 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
12/04/2010 10:52:08 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
12/04/2010 10:52:08 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
12/04/2010 10:52:08 AM org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
12/04/2010 10:52:08 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
12/04/2010 10:52:08 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
12/04/2010 10:52:08 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
12/04/2010 10:52:08 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/assignment
12/04/2010 10:52:08 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root}
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.0.51b-community-nt
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
12/04/2010 10:52:08 AM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
12/04/2010 10:52:08 AM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
12/04/2010 10:52:08 AM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
12/04/2010 10:52:08 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
12/04/2010 10:52:08 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
12/04/2010 10:52:08 AM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
12/04/2010 10:52:08 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured


it prints out when I open a session.


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 12:05 am 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
even when I try something simple like:
Code:
Query test = session.createQuery("from Users");

it still dosn't work..
Ive been searching for the answer for hours...and the only answer i have got is you have to use the class name..not the table name..but I am using the class name.

when I try and put the class path
(
Code:
Query test = session.createQuery("from src.model.Users");
)
I get this:
WARNING: no persistent classes found for query class: from src.model.Users and it dosn't do anything.
someone please help..


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 3:06 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
You can use "FROM Users" (the class name).

But the error says: "Users is not mapped".

This means something is wrong in your mapping. Can you show you hbm file?


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 3:13 am 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
thanks for the reply XD
here is Users.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 12/04/2010 1:03:49 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="src.model.Users" table="users" catalog="assignment">
        <id name="userId" type="int">
            <column name="UserID" />
            <generator class="assigned" />
        </id>
        <property name="userName" type="string">
            <column name="UserName" length="50">
                <comment>Username</comment>
            </column>
        </property>
        <property name="userPassword" type="string">
            <column name="UserPassword">
                <comment>User's Password (encrypted or hashed)</comment>
            </column>
        </property>
        <property name="userFirstName" type="string">
            <column name="UserFirstName" length="50">
                <comment>First/Given Name</comment>
            </column>
        </property>
        <property name="userLastName" type="string">
            <column name="UserLastName" length="50">
                <comment>Last Name/Surname</comment>
            </column>
        </property>
        <property name="userEmail" type="string">
            <column name="UserEmail" length="50">
                <comment>User email address</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 3:27 am 
Newbie

Joined: Thu Jul 26, 2007 8:25 am
Posts: 6
Hi,

Your code should be some thing like this.

user = (Users) session.createQuery("from src.model.Users where userName=:userName")
.setString("userName", userName)
.uniqueResult();
session.getTransaction().commit();

it seems Users.class does not exist in classpath. Make sure it is and in same package structure.


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 3:31 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I don't see something wrong, but I haven't used catalog="assignment" and the column in the id before so the problem could be in them.

If you have your java class like: private String userName, you don't have to use type="string" in your mapping. Hibernate can figure it out. Just check your DB ones to be sure. Example: if you have a String which you want to be a text in de DB, you do need to add it in your mapping.

Tip: you could also map it like this:
dani_501 wrote:
Code:
    <class name="src.model.Users" table="users" catalog="assignment">
        <id name="userId" type="int">
            <column name="UserID" />
            <generator class="assigned" />
        </id>

        <property name="userName" column="UserName" length="50" />
        <property name="userPassword" column="UserPassWord" />
        <property name="userFirstName" column="UserFirstName" length="50" />
        <property name="userLastName" column="UserLastName" length="50" />
        <property name="userEmail" column="UserEmail" length="50" />
    </class>


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 3:40 am 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
I've tried that but it give me this warning
WARNING: no persistent classes found for query class: from src.model.Users u
and dosen't do anything to the user object.
stupid question: where do i change the classpath?


Top
 Profile  
 
 Post subject: Re: Query problem
PostPosted: Mon Apr 12, 2010 7:21 am 
Newbie

Joined: Sun Apr 11, 2010 7:58 pm
Posts: 6
Thanks for all your help, but i solved the problem.
Netbeans wasn't updating the hibernate.cfg.xml in the build file..so I just deleted it then re-built the application.
Thanks again.
Daniel.


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