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

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

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

问题

我有以下一段代码,其中我尝试使用JPQL语法从一个班级中获取所有用户:

  1. import javax.persistence.EntityManager;
  2. import javax.persistence.TypedQuery;
  3. import java.util.List;
  4. public class ClasaDao {
  5. private EntityManager em;
  6. public ClasaDao(EntityManager em){
  7. this.em = em;
  8. }
  9. public List<Clasa> getClase(){
  10. String sql = "SELECT c FROM Clasa c";
  11. TypedQuery<Clasa> query = em.createQuery(sql, Clasa.class);
  12. return query.getResultList();
  13. }
  14. public void stergeClasa(Clasa clasa){
  15. clasa = em.merge(clasa);
  16. em.remove(clasa);
  17. }
  18. }

这是实体类:

  1. import javax.persistence.*;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import java.util.List;
  5. @Entity
  6. @Table(name = "Clasa")
  7. public class Clasa implements Serializable {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.IDENTITY)
  10. private int id;
  11. @Temporal(value = TemporalType.DATE)
  12. private Date dataIncepere;
  13. private String nume;
  14. @ManyToOne
  15. private Curs curs;
  16. @ManyToMany(mappedBy = "clasa")
  17. private List<Student> student;
  18. public List<Student> getStudent() {
  19. return student;
  20. }
  21. public void setStudent(List<Student> student) {
  22. this.student = student;
  23. }
  24. public Curs getCurs() {
  25. return curs;
  26. }
  27. public void setCurs(Curs curs) {
  28. this.curs = curs;
  29. }
  30. public int getId() {
  31. return id;
  32. }
  33. public void setId(int id) {
  34. this.id = id;
  35. }
  36. public Date getDataIncepere() {
  37. return dataIncepere;
  38. }
  39. public void setDataIncepere(Date dataIncepere) {
  40. this.dataIncepere = dataIncepere;
  41. }
  42. public String getNume() {
  43. return nume;
  44. }
  45. public void setNume(String nume) {
  46. this.nume = nume;
  47. }
  48. @Override
  49. public String toString() {
  50. return "Clasa{" +
  51. "dataIncepere=" + dataIncepere +
  52. ", nume='" + nume + '\'' +
  53. '}';
  54. }
  55. }

以上代码出现以下错误: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:
`

  1. import javax.persistence.EntityManager;
  2. import javax.persistence.TypedQuery;
  3. import java.util.List;
  4. public class ClasaDao {
  5. private EntityManager em;
  6. public ClasaDao(EntityManager em){
  7. this.em = em;
  8. }
  9. public List&lt;Clasa&gt; getClase(){
  10. String sql = &quot;SELECT c FROM Clasa c&quot;;
  11. TypedQuery&lt;Clasa&gt; query = em.createQuery(sql, Clasa.class);
  12. return query.getResultList();
  13. }
  14. public void stergeClasa(Clasa clasa){
  15. clasa = em.merge(clasa);
  16. em.remove(clasa);
  17. }}`

and this is the Entity class:

  1. import javax.persistence.*;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. import java.util.List;
  5. @Entity
  6. @Table(name = &quot;Clasa&quot;)
  7. public class Clasa implements Serializable {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.IDENTITY)
  10. private int id;
  11. @Temporal(value = TemporalType.DATE)
  12. private Date dataIncepere;
  13. private String nume;
  14. @ManyToOne
  15. private Curs curs;
  16. @ManyToMany(mappedBy = &quot;clasa&quot;)
  17. private List&lt;Student&gt; student;
  18. public List&lt;Student&gt; getStudent() {
  19. return student;
  20. }
  21. public void setStudent(List&lt;Student&gt; student) {
  22. this.student = student;
  23. }
  24. public Clasa getClasa() {
  25. return clasa;
  26. }
  27. public void setClasa(Clasa clasa) {
  28. this.clasa = clasa;
  29. }
  30. private Clasa clasa;
  31. public Curs getCurs() {
  32. return curs;
  33. }
  34. public void setCurs(Curs curs) {
  35. this.curs = curs;
  36. }
  37. public int getId() {
  38. return id;
  39. }
  40. public void setId(int id) {
  41. this.id = id;
  42. }
  43. public Date getDataIncepere() {
  44. return dataIncepere;
  45. }
  46. public void setDataIncepere(Date dataIncepere) {
  47. this.dataIncepere = dataIncepere;
  48. }
  49. public String getNume() {
  50. return nume;
  51. }
  52. public void setNume(String nume) {
  53. this.nume = nume;
  54. }
  55. @Override
  56. public String toString() {
  57. return &quot;Clasa{&quot; +
  58. &quot;dataIncepere=&quot; + dataIncepere +
  59. &quot;, nume=&#39;&quot; + nume + &#39;\&#39;&#39; +
  60. &#39;}&#39;;
  61. }
  62. }

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

看起来:

  1. @ManyToMany(mappedBy = "clasa")

应该改为:

  1. @ManyToMany(mappedBy = "Clasa")

这可能导致IllegalArgumentException异常。

英文:

It seems that:

  1. @ManyToMany(mappedBy = &quot;clasa&quot;)

should be rather:

  1. @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:

确定