Results 1 to 2 of 2

Thread: Query method declaration can not be parsed correctly

  1. #1
    Join Date
    Jun 2009
    Posts
    1

    Default Query method declaration can not be parsed correctly

    There is an error occurred when application context initializing, I do not know what is the problem, could someone tell me why? thanks a lot.

    Code:
    Caused by: java.lang.IllegalArgumentException: Unable to resolve attribute [userName] against path
    	at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120)
    	at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229)
    	at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:200)
    	at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:408)
    	at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:197)
    	at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:144)
    	at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:86)
    	at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:44)
    Repository:
    Code:
    @Repository("userDao")
    public interface UserDao extends CrudRepository<User, Long> {
    	
    	public User findByUserName(String userName);
    }
    Domain:
    Code:
    @Entity
    @Table(name = "T_USER") 
    public class User implements Persistable<Long> {
    
    	private static final long serialVersionUID = -8549228842877928873L;
    
    	@Id
    	//@SequenceGenerator(name="userSeq",sequenceName="SEQ_USER")
    	//@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="userSeq")
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	@Column(name="ID")
    	private Long _id;
    	
    	@Column(name="USER_NAME")
    	private String _userName;
    	
    	@Column(name="PASSWORD")
    	private String _password;
    	
    	public String getUserName() {
    		return _userName;
    	}
    	public void setUserName(String userName) {
    		_userName = userName;
    	}
    	public String getPassword() {
    		return _password;
    	}
    	public void setPassword(String password) {
    		_password = password;
    	}
    	
    	@Override
    	public Long getId() {
    		
    		return _id;
    	}
    	
    	@Override
    	public boolean isNew() {
    		return null == getId();
    	}

  2. #2

    Default

    Quote Originally Posted by damage85 View Post
    There is an error occurred when application context initializing, I do not know what is the problem, could someone tell me why? thanks a lot.

    Code:
    Caused by: java.lang.IllegalArgumentException: Unable to resolve attribute [userName] against path
    	at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120)
    	...
    Repository:
    Code:
    @Repository("userDao")
    public interface UserDao extends CrudRepository<User, Long> {
    	
    	public User findByUserName(String userName);
    }
    Domain:
    Code:
    @Entity
    @Table(name = "T_USER") 
    public class User implements Persistable<Long> {
    	...
    	@Id
    	...
    	@Column(name="ID")
    	private Long _id;
    	
    	@Column(name="USER_NAME")
    	private String _userName;
    	...
    My guess is that since you are using field access by annotating the member variable (i.e., field), Hibernate is expecting "_userName", not "userName". (I just tested this with EclipseLink, and it has pretty much the same issue with your code.)

    If you really need to stick to what many consider poor style (and just as many consider good style, see for example, stackoverflow: Is there a standard in java to use _ [underscore] in front of variable or class name [closed]) and use an underscore for member variable names, then you'll have to either make a sacrifice and pollute your repository find method names with the underscore

    Code:
    public User findBy_userName(String userName);
    (this works in my testing done just now with EclipseLink) or annotate your getter methods to declare method access [Chapter 20 Introduction to the Java Persistence API (The Java EE 6 Tutorial)].

    Likewise, any @Query statement you write will have to use the underscore if you use field access:

    Code:
    @Query("SELECT u FROM User u WHERE LOWER(u._firstName) LIKE LOWER(:searchTerm) ...")
    But I have to ask (and hope not to start a war here ... hmmm, maybe I shouldn't ask), what does the underscore buy you, given syntax highlighting in modern IDEs?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •