S2SH 学习笔记之异常错误——Entity is not mapped

做为初学者,遇到各种各样的初级问题,现通过笔记方式记录下来。

采用的环境是:Struts2.2.3.1+Spring3.0.6+Hibernate3,Tomcat8.0

按照MCV编程模式,在model下编写了一个注解实体类Authority 

package com.control.model;

import java.util.Random;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import javax.persistence.GeneratedValue;
import org.hibernate.annotations.GenericGenerator;

// 权限
@Entity
@Table(name="t_Authority")
public class Authority {

	private int id;
	private String	s_Name;
	private String	s_Note;
	
	
	public Authority() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Authority(String s_Name, String s_Note) {
		super();
		this.s_Name	= s_Name;
		this.s_Note	= s_Note;

	}
	
	@Id
//	@GenericGenerator(name="generator",strategy="increment")
//	@GeneratedValue(generator="generator")
	@Column(name="id")
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="Name")
	public String getName() {
		return s_Name;
	}
	public void setName(String sName) {
		this.s_Name = sName.trim();
	}
	
	@Column(name="Note")
	public String getNote() {
		return s_Note;
	}
	public void setNote(String s_Note) {
		this.s_Note = s_Note.trim();
	}	
}

在AuthorityDaoImpl中查询Authority类

@Repository
public class AuthorityDaoImpl implements AuthorityDao{

	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List List(PageBean pageBean,Authority auth)throws Exception{
		List list = null;
		Session session = this.getSession();

		StringBuffer sb = new StringBuffer("FROM Authority g");
		if(auth != null && StringUtil.isNotEmpty(auth.getName())){
			sb.append(" and g.Name like '%" + auth.getName() + "%'");
		}
		Query query = session.createQuery(sb.toString().replaceFirst("and", "where"));
		if(pageBean != null){
			query.setFirstResult(pageBean.getStart());
			query.setMaxResults(pageBean.getRows());
		}
		list = (List)query.list();
		return list;
	}
	

在Console中提示

org.hibernate.hql.ast.QuerySyntaxException: Authority is not mapped [FROM Authority g]
...
因是初学,对整个SSH体系不是很了解,网上查找了一下原因说注解不正确,但我的注解是正确的,其他实体类没有出现这个问题,一定是哪个环节有问题了。

最后对比示例:终于找到原因了。

对于实体,必须要在applicationContext.xml中的做实体声明。



com.control.model.Authority




总结:

对应Model,要实现自动映射,需要做的工作如下

1、类引用注解

@Entity
@Table(name="t_Authority")

2、applicationContext.xml中的做实体声明


com.control.model.Authority


你可能感兴趣的:(S2SH)