java org.hibernate.hql.ast.QuerySyntaxException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4645924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.hql.ast.QuerySyntaxException
提问by theJava
org.hibernate.hql.ast.QuerySyntaxException:
users is not mapped [SELECT email, id FROM users WHERE email='[email protected]' AND password='asasas']
org.hibernate.hql.ast.QuerySyntaxException:
用户未映射 [SELECT email, id FROM users WHERE email='[email protected]' AND password='asasas']
public ILogin authenticate(Login login) {
System.out.println(login);
System.out.println(login.getEmail());
String query = "SELECT email, id FROM users WHERE email='"
+ login.getEmail() + "' AND password='" + login.getPassword() + "'";
results = getHibernateTemplate().find(query);
System.out.println(results);
return null;
}
I have a Login Bean class... here it follows.
我有一个 Login Bean 类……它如下。
package
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Login {
public Login(){}
private Long id = null;
private String email;
private String password;
public Login(String email, String password)
{
this.email = email;
this.password = password;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
}
My application-context.xml
我的应用程序context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="com.intermedix"/>
<context:annotation-config/>
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.intermedix.domain.Login</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
<property name="username" value="monty"/>
<property name="password" value="indian"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
回答by Ralph
Even if it does not belong to your question:
即使它不属于您的问题:
Don't use Hibernate/JPA in this way (String concatination) !:
不要以这种方式使用 Hibernate/JPA(字符串连接)!:
String query = "SELECT email, id FROM users AS u WHERE email='"+ login.getEmail() + "' AND password='" + login.getPassword() + "'";
Instead use HQL like prepared statements:
而是像准备好的语句一样使用 HQL:
createQuery(
"SELECT l FROM login WHERE l.email=:email AND l.password=:password")
.setParameter("login",login.getEmail())
.setParameter("password",login.getPassword());
If you do it in "your style" you will have lot of fun with SQL Injections!
如果你以“你的风格”来做,你会在 SQL 注入中获得很多乐趣!
Next: read the hibernate reference about HQL, for me it looks like you are writing SQL instead of HQL.
下一步:阅读有关 HQL 的 hibernate 参考,对我来说,看起来您正在编写 SQL 而不是 HQL。
回答by skaffman
SELECT email, id FROM users
选择电子邮件,ID FROM 用户
What is "users"? There is nothing in your config or code called "users", so Hibernate has no idea what you're talking about.
什么是“用户”?您的配置或代码中没有任何内容称为“用户”,因此 Hibernate 不知道您在说什么。
Secondly, your Login
class is not annotated with @Entity
, so Hibernate is likely ignoring it.
其次,您的Login
类没有用 注释@Entity
,因此 Hibernate 可能会忽略它。
So add the annotation, and most likely change your query to:
所以添加注释,并且很可能将您的查询更改为:
SELECT email, id FROM Login
选择电子邮件,ID FROM 登录
回答by darioo
Looks obvious to me : "users is not mapped..."
对我来说很明显:“用户没有被映射......”
Either you didn't map the Users table, or you have it incorrectly configured.
您没有映射用户表,或者您的配置不正确。
回答by user1701556
@Entity
@Table(name="users")
public class Login {
You actually need to annotate Login class since you say (<property name="annotatedClasses">
) that in application-context.xml for example like this.
您实际上需要注释 Login 类,因为您<property name="annotatedClasses">
在 application-context.xml 中这样说 ( )。
@Column(name="password")
public String getPassword() {
return password;
}
回答by feel good and programming
List list = getHibernateTemplate().find("from Form3A where FAC_ID=?",FAC_ID);
HERE Form3A is name of class and config file is
这里 Form3A 是类的名称,配置文件是
<property name="annotatedClasses">
<list>
<value>org.fbis.models.Form3A</value>
</list>
</property>