-->
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.  [ 2 posts ] 
Author Message
 Post subject: @NamedQuery stands for column name or field name?
PostPosted: Mon Jan 02, 2006 11:46 pm 
Newbie

Joined: Wed Nov 02, 2005 10:49 pm
Posts: 12
Under Hibernate Core 3.1 + Hibernate Annotations 3.1beta7

Code:
package zappy.htlgm.ejb1;

import javax.persistence.AccessType;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;

@Entity(access=AccessType.FIELD)
@NamedQuery(name="user.valid", queryString="select u from User u where u.id=:id")
@NamedQuery(name="user.invalid", queryString="select u from User u where id=:id")
public class User {
   
    @Id
    @Column(name="USR000")
    private int id;
   
    @Column(name = "USR001")
    private String mName;

    public int getId() {
        return id;
    }
   
    public void setId(int pId) {
        id = pId;
    }
   
    public String getName() {
        return mName;
    }
   
    private void setName(String pName) {
        mName = pName;
    }
}


Problems:
1. @NamedQuery stands for EJB3 QL, why query user.valid "where u.id=:id"(where "u.id" prefixed with "u.") would be passed, but query user.invalid "where id=:id"(without "u." prefix) not? (got java.sql.SQLException: Unknown column 'id' in 'where clause')

2. change query.invalid slightly to be "where USER000 = :id", then it passed, why? but USER000 is a column name but not a field one...

3. JSR-200 final draft describe that @NameQuery has two attribute: name and query, but why Hibernate Annotations implements it as queryString?

4. following consitent coding style(POJO is better to be transient), so how can I preserve the "mName"(prefixed with 'm'), but used as 'name' in query language, but mapping to 'USR001' in physical table?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 6:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
1. choose either prefix or no prefix, but do not mix and match
2. this is a side effect of the fact that the HQL parser support any kind of function in the where clause. What you've done is a bad practice anyway
3. This has changed btw the last 2 draft and I haven't catched up yet. (ANN-205)
4. annotate the getters and not the fields. I personally think prefixes are a bad practice, esp regarding the modern IDE we have in Java.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.