英文:
IllegalArgumentException - Hibernate Error - org.hibernate.hql.internal.ast.QuerySyntaxException: Clasa is not mapped [SELECT c FROM Clasa c]
问题
我有以下一段代码,其中我尝试使用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);
}
}
这是实体类:
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 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 + '\'' +
'}';
}
}
以上代码出现以下错误:Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Clasa is not mapped [SELECT c FROM Clasa c]
对于可能导致此错误的原因,任何想法将不胜感激。
英文:
I have the following line of code, in which I'm trying to get all users from a class, by using JPQL syntax:
`
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);
}}`
and this is the Entity class:
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 + '\'' +
'}';
}
}
The lines above yield the following error: Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Clasa is not mapped [SELECT c FROM Clasa c]
Any ideas as to what might be causing this error will be greatly appreciated
答案1
得分: 0
看起来:
@ManyToMany(mappedBy = "clasa")
应该改为:
@ManyToMany(mappedBy = "Clasa")
这可能导致IllegalArgumentException异常。
英文:
It seems that:
@ManyToMany(mappedBy = "clasa")
should be rather:
@ManyToMany(mappedBy = "Clasa")
This may be causing the IllegalArgumentException.
答案2
得分: 0
如果显示未映射,则该类未包含在持久性单元中。请检查您的persistence.xml,确认您是否在其中列出了该类,或者尝试添加 <exclude-unlisted-classes>false</exclude-unlisted-classes>
。
英文:
If it says it's not mapped, then the class is not included in the persistence unit. Check you persistence.xml if you listed the class there or maybe add <exclude-unlisted-classes>false</exclude-unlisted-classes>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论