问题描述
我有以下代码行,其中我尝试使用JPQL语法从类中获取所有用户: `
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.List;
public class ClasaDao {
private EntityManager em;
public ClasaDao(EntityManager em){
this.em = em;
}
public List<Clasa> getClase(){
String sql = "SELECT c FROM Clasa c";
TypedQuery<Clasa> query = em.createquery(sql,Clasa.class);
return query.getResultList();
}
public void stergeClasa(Clasa clasa){
clasa = em.merge(clasa);
em.remove(clasa);
}}`
这是Entity类:
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Entity
@Table(name = "Clasa")
public class Clasa implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal(value = TemporalType.DATE)
private Date dataIncepere;
private String nume;
@ManyToOne
private Curs curs;
@ManyToMany(mappedBy = "clasa")
private List<Student> student;
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
public Clasa getClasa() {
return clasa;
}
public void setClasa(Clasa clasa) {
this.clasa = clasa;
}
private Clasa clasa;
public Curs getCurs() {
return curs;
}
public void setCurs(Curs curs) {
this.curs = curs;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDataIncepere() {
return dataIncepere;
}
public void setDataIncepere(Date dataIncepere) {
this.dataIncepere = dataIncepere;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume = nume;
}
@Override
public String toString() {
return "Clasa{" +
"dataIncepere=" + dataIncepere +
",nume='" + nume + '\'' +
'}';
}
}
上面的行产生以下错误:线程“ main”中的异常java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Clasa [从Clasa c中选择SELECT c]
对于可能导致此错误的原因的任何想法,将不胜感激
解决方法
似乎:
@ManyToMany(mappedBy = "clasa")
应该是:
@ManyToMany(mappedBy = "Clasa")
这可能导致IllegalArgumentException。
,如果它说未映射,则该类不包含在持久性单元中。检查您是否在persistence.xml中列出了该类,或者添加了<exclude-unlisted-classes>false</exclude-unlisted-classes>