Java org.hibernate.hql.ast.QuerySyntaxException
未映射用户[选择电子邮件,id 来自电子邮件地址为的用户dsdd@dds.com' 和密码='asasas'] 我有一个登录Bean类。。。下面就是Java org.hibernate.hql.ast.QuerySyntaxException,java,hibernate,Java,Hibernate,未映射用户[选择电子邮件,id 来自电子邮件地址为的用户dsdd@dds.com' 和密码='asasas'] 我有一个登录Bean类。。。下面就是 public ILogin authenticate(Login login) { System.out.println(login); System.out.println(login.getEmail()); String query = "SELECT email, id FROM users
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;
}
我的应用程序-context.xml
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;
}
}
com.intermedix.domain.Login
org.hibernate.dialogue.mysqldialogue
真的
创造
选择电子邮件,用户id
什么是“用户”?您的配置或代码中没有所谓的“用户”,所以Hibernate不知道您在说什么
其次,您的登录
类没有使用@实体
进行注释,因此Hibernate可能会忽略它
因此,添加注释,很可能会将查询更改为:
从登录中选择电子邮件、id
对我来说很明显:“用户没有映射…”
您没有映射用户表,或者配置不正确。即使它不属于您的问题: 不要以这种方式使用Hibernate/JPA(字符串压缩):
<?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>
而是使用类似HQL的准备语句:
String query = "SELECT email, id FROM users AS u WHERE email='"+ login.getEmail() + "' AND password='" + login.getPassword() + "'";
如果您以“您的风格”进行操作,您将从SQL注入中获得很多乐趣
下一步:阅读关于HQL的hibernate参考资料,对我来说,您编写的似乎是SQL而不是HQL
createQuery(
"SELECT l FROM login WHERE l.email=:email AND l.password=:password")
.setParameter("login",login.getEmail())
.setParameter("password",login.getPassword());
实际上,您需要注释Login类,因为您在application-context.xml中这样说(
)
@Entity
@Table(name="users")
public class Login {
这里Form3A是类的名称,配置文件是
List list = getHibernateTemplate().find("from Form3A where FAC_ID=?",FAC_ID);
org.fbis.models.Form3A
但是我的表名是users,我应该将其更改为Login@theJava:否。HQL应该引用实体和属性,而不是表和列。@Java:通过使用@Table(name=“users”)
注释Login
。如果您不告诉Hibernate,您希望Hibernate如何知道users
表?那么@Entity到底是什么?我应该在Login类上面添加@Entity和@table(name=“users”)吗
@Column(name="password")
public String getPassword() {
return password;
}
List list = getHibernateTemplate().find("from Form3A where FAC_ID=?",FAC_ID);
<property name="annotatedClasses">
<list>
<value>org.fbis.models.Form3A</value>
</list>
</property>