IllegalArgumentException – Hibernate Error – org.hibernate.hql.internal.ast.QuerySyntaxException: Clasa is not mapped [SELECT c FROM Clasa c]

huangapple go评论74阅读模式
英文:

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&lt;Clasa&gt; getClase(){
String sql = &quot;SELECT c FROM Clasa c&quot;;
TypedQuery&lt;Clasa&gt; 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 = &quot;Clasa&quot;)
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 = &quot;clasa&quot;)
private List&lt;Student&gt; student;
public List&lt;Student&gt; getStudent() {
return student;
}
public void setStudent(List&lt;Student&gt; 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 &quot;Clasa{&quot; +
&quot;dataIncepere=&quot; + dataIncepere +
&quot;, nume=&#39;&quot; + nume + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}

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 = &quot;clasa&quot;)

should be rather:

@ManyToMany(mappedBy = &quot;Clasa&quot;)

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 &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;

huangapple
  • 本文由 发表于 2020年8月15日 04:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定