我尝试使用MySql和Hibernate创建简单的JPA应用程序,我想从表学生中加载4条记录 db table
但是我收到了这个错误:
Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: Named query not found: findAll
这是我的实体类
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
@Entity
@Table(name="student")
@NamedQueries({
@NamedQuery(name = "findAll", query = "select st from student st")
})
public class student{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="sex")
private String sex;
public student(String name, String surname, String sex) {
this.name = name;
this.surname = surname;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "student [id=" + id + ", name=" + name + ", surname=" + surname + ", sex=" + sex + "]";
}
DAO
package dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import entity.student;
public class StudentDAO {
public List<student> getAllStd(EntityManager em){
TypedQuery<student> q = em.createNamedQuery("findAll", student.class);
return q.getResultList();
}
}
最后调用DAO方法的bean
package test;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import dao.StudentDAO;
import entity.student;
@Stateless
public class Test2Bean {
@PersistenceContext()
private EntityManager em;
public void getAllStudents(){
StudentDAO stdDAO = new StudentDAO();
List<student> students = stdDAO.getAllStd(em);
System.out.println(students.size());
}
}
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="mysql">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/mysqlds</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
我不知道为什么DAO方法无法访问查询,我在实体类中做了很少的测试方法,DAO方法可以访问实体学生。
感谢所有答案
答案 0 :(得分:1)
...试
@Entity
@Table(name="student")
@NamedQueries({
@NamedQuery(name = "Student.findAll", query = "select st from student st")
})
public class student{
添加学生。在“findAll”之前
不确定是不是这样,但我的命名查询通常都有。