如何使用EntityManager查询从数据库返回对象-异常:org.hibernate.hql.internal.ast.QuerySyntaxException:

时间:2018-07-16 15:28:03

标签: hibernate entitymanager

我需要创建一个查询,该查询按用户名从数据库中返回一个用户。 因此,我需要以类格式(User.class)返回此结果

我尝试实现此代码,但有一个例外:

org.hibernate.hql.internal.ast.QuerySyntaxException:未映射myDB.user

public User getUserByUsername(String username){
        EntityManager em = emf.createEntityManager();

        User u = (User) em.createQuery("FROM myDB.user WHERE myDB.user.username = :username").setParameter(username, username);
        em.close();
        return u;
}

关注我的实体类

@Entity
@Table(name = "user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private int id;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;
    @Column(name = "email")
    private String email;
    @Column(name = "lastAccessed")
    private Date lastAccessed;


    /**get and setters*/
}

0 个答案:

没有答案