QuerySyntaxException:意外的令牌*

时间:2016-02-18 10:28:59

标签: java sql oracle hibernate jpa

当我尝试使用 EntityManager 运行查询时出现以下错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8

导致这种情况的原因是什么?

代码:

public static void main(String [] args) {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myClass");

        EntityManager em = entityManagerFactory.createEntityManager();

        List<String> results= em.createQuery(
                "SELECT * FROM myClass ")
                .setMaxResults(10)
                .getResultList();

}

1 个答案:

答案 0 :(得分:5)

您不能在HQL中使用 * 运算符。您可以尝试如下:

    package com.demo.mongo.example.model;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;

    @Document
    public class UserDetails {
    	@Id
    	private String name;
    	private String password;
    	private String emailid;
    	private int contact;

    	public UserDetails(){}

    	public UserDetails(String name,String password,String emailid,int contact){
    		this.name=name;
    		this.password=password;
    		this.emailid=emailid;
    		this.contact=contact;
    	}

    	public void setName(String name){
    		this.name=name;
    	}

    	public void setPassword(String password){
    		this.password=password;
    	}
    	
    	public void setEmailid(String emailid){
    		this.emailid=emailid;
    	}
    	public void setContact(int contact){
    		this.contact=contact;
    	}
    	public String getPassword(){
    		return this.password;
    		
    	}
    	public String getName(){
    		return this.name;
    	}
    	public int getContact(){
    		return this.contact;
    		
    	}
    	public String getEmailid(){
    		return this.emailid;
    		
    	}
    }

希望这有帮助!