org.hibernate.hql.ast.QuerySyntaxException
="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="student"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!--
<jta-data-source>java:/student</jta-data-source>
-->
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password"
value="666666*" />
<!--
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.hbm2ddl.auto" value="update" />
-->
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
=============================================================================================
package com.fengmanfei.student.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity(name = "tb_class")
//@Table(name = "tb_class")
public class ClassEO implements Serializable {
private static final long serialVersionUID = 4231121505530870021L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "classEO")
private Set<StudentEO> students = new HashSet<StudentEO>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getStudents() {
return students;
}
public void setStudents(Set<StudentEO> students) {
this.students = students;
}
}
=============================================================================
package com.fengmanfei.student.servlet;
import java.io.IOException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fengmanfei.student.entity.ClassEO;
import com.fengmanfei.student.exception.StudentException;
public class ClassServlet extends HttpServlet {
private static final long serialVersionUID = -8205614348005279786L;
// @PersistenceUnit(unitName="student")
private EntityManagerFactory emf;
@Override
public void init() throws ServletException {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("student");
}
}
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if ("list".equals(action)) {
EntityManager em = emf.createEntityManager();
try {
// 读取数据库时不需要事务,直接查询即可ClassEO
Query query = em.createQuery(" from ClassEO order by id asc ");
List<ClassEO> classList = query.getResultList();
request.setAttribute("classList", classList);
request.getRequestDispatcher("/listClass.jsp").forward(request,
response);
} finally {
em.close();
}
} else if ("add".equals(action)) {
String name = request.getParameter("name");
// 如果没有输入名称,则抛出StudentException异常。
if (name == null || name.trim().length() == 0) {
throw new StudentException("请填写班级名称");
}
// 获取EntityManager对象
EntityManager em = emf.createEntityManager();
// JPA写数据库必须开启事务。
EntityTransaction trans = em.getTransaction();
try {
// 事务开始
trans.begin();
// 查找同名的ClassEO
List classList = em.createQuery(
" from ClassEO where name = :name ").setParameter(
"name", name).getResultList();
// 如果已经存在,则报错
if (classList.size() > 0) {
throw new StudentException("班级已经存在:" + name);
}
// new一个新的Entity Object
ClassEO classEO = new ClassEO();
// 设置名称。ID有数据库自动生成。
classEO.setName(name);
// persist
em.persist(classEO);
// 事务提交。如果前面或者这时有异常抛出,会自动回滚(Rollback)。
trans.commit();
// 成功信息
request.setAttribute("msg", "班级 " + classEO.getName()
+ " 添加成功。");
// 显示成功页面
request.setAttribute("classEO", classEO);
request.getRequestDispatcher("/addClassSuccess.jsp").forward(
request, response);
} finally {
// EntityManager 对象关闭
em.close();
}
} else if ("edit".equals(action)) {
EntityManager em = emf.createEntityManager();
try {
// 要修改的班级ID
int classId = Integer.parseInt(request.getParameter("classId"));
// 查找班级实体
ClassEO classEO = em.find(ClassEO.class, classId);
request.setAttribute("classEO", classEO);
request.getRequestDispatcher("/addClass.jsp").forward(request,
response);
} finally {
em.close();
}
} else if ("save".equals(action)) {
EntityManager em = emf.createEntityManager();
// 写数据库,需要事务
EntityTransaction trans = em.getTransaction();
try {
// 事务开始
trans.begin();
// 修改的是哪个班级
int classId = Integer.parseInt(request.getParameter("classId"));
String name = request.getParameter("name");
if (name == null || name.trim().length() == 0) {
throw new StudentException("请填写班级名称");
}
List list = em.createQuery(
" from ClassEO where name = :name and id <> :id ")
.setParameter("name", name).setParameter("id", classId)
.getResultList();
if (list.size() > 0) {
throw new StudentException("修改后的班级名称与其他班级冲突,修改失败");
}
// 查找班级实体
ClassEO classEO = em.find(ClassEO.class, classId);
// 修改属性
classEO.setName(name);
em.merge(classEO);
trans.commit();
request.setAttribute("msg", "班级 " + classEO.getName()
+ " 修改成功。");
request.setAttribute("classEO", classEO);
request.getRequestDispatcher("/addClassSuccess.jsp").forward(
request, response);
} finally {
em.close();
}
} else if ("delete".equals(action)) {
EntityManager em = emf.createEntityManager();
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
int classId = Integer.parseInt(request.getParameter("classId"));
ClassEO classEO = em.find(ClassEO.class, classId);
if (classEO.getStudents().size() > 0) {
throw new StudentException("班级 " + classEO.getName()
+ " 还有 " + classEO.getStudents().size()
+ " 个学生,不能删除。");
}
em.remove(classEO);
trans.commit();
request.setAttribute("msg", "班级 " + classEO.getName()
+ " 删除成功。");
request.setAttribute("classEO", classEO);
request.getRequestDispatcher("/addClassSuccess.jsp").forward(
request, response);
} finally {
em.close();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
@Override
public void destroy() {
// 销毁 EntityManagerFactory
emf.close();
}
}
========================================================================
JPA详解的例子,部署到jboss中后,显示:java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: ClassEO is not mapped [ from ClassEO order by id asc ]
用的是myeclipse8.5、jboss5.0、mysql5.1,网上查找到的:都说jpa操作的是实体类,不是表,这显然是对的。请高手指教。
[解决办法]
楼主,hql是Hibernate对应的数据库访问语句。ejb有自己专用的EJB QL.
一般情况是把hibernate与entity bean混在一起了。
楼主用的ejb怎么会爆出heibernate的异常呢。
[解决办法]
@Entity
@Table(name = "tb_class")
public class ClassEO implements Serializable