Java org.springframework.orm.hibernate3.HibernateQueryException-HibernateTemplate
我在HibernateTemplate上遇到了问题,我不知道哪里出了问题。我正在使用Hibernate3和Tomcat6。 在我的DAO中,我有一些函数尝试使用字符串查询数据库,它们似乎工作得很好。像这样Java org.springframework.orm.hibernate3.HibernateQueryException-HibernateTemplate,java,mysql,web-applications,tomcat6,hibernate-mapping,Java,Mysql,Web Applications,Tomcat6,Hibernate Mapping,我在HibernateTemplate上遇到了问题,我不知道哪里出了问题。我正在使用Hibernate3和Tomcat6。 在我的DAO中,我有一些函数尝试使用字符串查询数据库,它们似乎工作得很好。像这样 public AppUser getUserByUserName(String username){ HibernateTemplate template=new HibernateTemplate(sessionFactory); try{ List<A
public AppUser getUserByUserName(String username){
HibernateTemplate template=new HibernateTemplate(sessionFactory);
try{
List<AppUser> appList = template.find(" from AppUser as au where au.username=?", username);
tempUser = appList.get(0);
return tempUser;
} catch(Exception e){
System.out.println("Problem in AppUserDao--get byUsername: " + e.toString());
return null;
}
}
public-AppUser-getUserByUserName(字符串用户名){
HibernateTemplate=新的HibernateTemplate(sessionFactory);
试一试{
列出appList=template.find(“从AppUser作为au,其中au.username=?”,username);
tempUser=appList.get(0);
返回临时用户;
}捕获(例外e){
System.out.println(“AppUserDao中的问题——GetByUserName:+e.toString());
返回null;
}
}
然而,当我尝试使用整数进行查询时。比如:
public List<AppUser> getAllMerchants(){
HibernateTemplate template=new HibernateTemplate(sessionFactory);
try{
List<AppUser> appList = template.find(" from appuser as au where au.securityLevel!=?", 112);
if(appList.size() > 0)
return appList;
else
return null;
} catch(Exception e){
System.out.println("Problem in AppUserDao--getAllMerchants: " + e.toString());
return null;
}
}
公共列表getAllMerchants(){
HibernateTemplate=新的HibernateTemplate(sessionFactory);
试一试{
List appList=template.find(“从appuser作为au,其中au.securityLevel!=?”,112);
如果(appList.size()>0)
返回应用程序列表;
其他的
返回null;
}捕获(例外e){
println(“AppUserDao中的问题--getAllMerchants:+e.toString());
返回null;
}
}
我得到这个错误:org.springframework.orm.hibernate3.HibernateQueryException:appuser未被映射[从appuser映射为au,其中au.securityLevel!=?];嵌套异常为org.hibernate.hql.ast.QuerySyntaxException:appuser未映射[从appuser映射为au,其中au.securityLevel!=?]
org.springframework.orm.hibernate3.HibernateQueryException: appuser is not mapped [ from appuser as au where au.securityLevel!=?]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: appuser is not mapped [ from appuser as au where au.securityLevel!=?]
我的实体似乎有必要的注释。因为它适用于第一个函数,我不明白为什么它不适用于第二个函数
@Entity
@Table(name="appuser")
public class AppUser {
private int id;
private String username;
private String password;
private String firstName;
private String secondName;
private int state;
private int securityLevel;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator= "idSeq")
@SequenceGenerator(name="idSeq",sequenceName="app_user_seq_id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="password_2")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getSecurityLevel() {
return securityLevel;
}
public void setSecurityLevel(int securityLevel) {
this.securityLevel = securityLevel;
}
}
@实体
@表(name=“appuser”)
公共类AppUser{
私有int-id;
私有字符串用户名;
私有字符串密码;
私有字符串名;
私有字符串secondName;
私人和国家;
私有int安全级别;
@身份证
@GeneratedValue(策略=GenerationType.AUTO,generator=“idSeq”)
@SequenceGenerator(name=“idSeq”,sequenceName=“app\u user\u seq\u id”)
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
@列(name=“password_2”)
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getFirstName(){
返回名字;
}
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
公共字符串getSecondName(){
返回secondName;
}
public void setSecondName(字符串secondName){
this.secondName=secondName;
}
public int getState(){
返回状态;
}
公共无效设置状态(int状态){
this.state=状态;
}
public int getSecurityLevel(){
返回安全级别;
}
public void setSecurityLevel(int securityLevel){
this.securityLevel=securityLevel;
}
}
看起来你的代码有输入错误。 在第一个函数中,在第二个函数中使用“from AppUser”。 尝试将第二个查询更改为“来自AppUser”。谢谢您的回答 我意识到我们必须使用commandclass名称,而不是在查询中使用表名
return getHibernateTemplate().find("from ModuleCommand order by application");
ModuleCommand-命令类名称
app_模块-表名
豆荚
<bean id="....
.
.
.
<property name="commandClass" value="com.web.mon.thread.ModuleCommand" />
...
</bean>
绝对正确。哈哈哈。我想我只是需要一双新鲜的眼睛。我忘了我用了@Table注释。谢谢你抽出时间,没问题。别忘了把问题标为已回答。