users@glassfish.java.net

Can't use JPQL for quering Entities with JPA

From: <glassfish_at_javadesktop.org>
Date: Tue, 07 Apr 2009 02:41:18 PDT

Hi!

I am new to JPA and tried to set up a small application which uses JPA. I have created the JDBC Resources in the Glassfish admin-page (using the DerbyPool) and also the persistence.xml accordingly. I created the first instances and persisted it without problems.
But now that I try to query the instances I get errors. I tried the following query:

select u from Users u where u.username = :username

and get this error:
WARNUNG: Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Column 'U' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'U' is not a column in the target table.
Error Code: -1
Call: SELECT u FROM Users u WHERE u.username = ?
        bind => [testuser]
Query: DataReadQuery()
        at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:319)
        at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:566)
        at oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:452)
...

I realized that changing the query to:
select * from Users u where u.username = ?1
(I hat to change the :username, because after changing to * the :username wasn't supported anymore)
resolved the error but leading in receiving only a vector with the object properties instead of the Object itself.
If I see it right this second query is a SQL query which works and the JPQL query doesn't :-(
Can someone tell me what I am missing?

Some background info on what I am using:
Glassfish 2.1
Glassfish's javaDB
JPA used in an EJB
Icefaces framework

One other strange thing is, when deploying the EJB I get already an error that the tables couldn't be created since they already exist. This would be ok since they really exist, but I use in the persistence.xml this property:
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>

Here my whole persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
        <persistence-unit name="testapp" transaction-type="JTA">
                <jta-data-source>jdbc/testapp</jta-data-source>
                <class>de.flyingfinger.testapp.model.ejbprovider.UserImpl</class>
                <class>de.flyingfinger.testapp.model.ejbprovider.EntityImpl</class>
                <properties>
                        <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
                </properties>
        </persistence-unit>
</persistence>
        
... and my UserImpl class:

@Entity(name="Users")
@Table(name="Users")
public class UserImpl extends EntityImpl implements Serializable, User {

        private static final long serialVersionUID = -120937647702624559L;
        private String username;
        private String password;
        private String email;
        private String emailToVerify;

        public String getEmailToVerify() {
                return emailToVerify;
        }
        public void setEmailToVerify(String emailToVerify) {
                this.emailToVerify = emailToVerify;
        }
        public UserImpl() {
                super();
        }
        public String getUsername() {
                return username;
        }
        public void setUsername(String username) {
                this.username = username;
        }
        public String getPassword() {
                return password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
        public String getEmail() {
                return email;
        }
        public void setEmail(String email) {
                this.email = email;
        }
}

... and the EntityImpl class:

@Entity(name="Entity")
@Table(name = "Entity")
@Inheritance(strategy=InheritanceType.JOINED)
public class EntityImpl implements Serializable, EntityBase {
        private static final long serialVersionUID = -142912318644180510L;
        private long id;
        public EntityImpl() {}
        
        @Id
        @TableGenerator(
                        name="EntityIdGenerator",
                        table="entityid")
        @GeneratedValue(strategy = GenerationType.TABLE,
                        generator = "EntityIdGenerator")
        public long getId() {
                return id;
        }
        public void setId(long id) {
                this.id = id;
        }
}

Thanks in advance!!!
[Message sent by forum member 'flyingfinger' (flyingfinger)]

http://forums.java.net/jive/thread.jspa?messageID=340925