解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题 HibernateJavaAntApacheIDEA
程序员文章站
2022-03-14 21:46:52
...
软件版本:
hibernate-3.2.0.cr2
hibernate-annotations-3.2.0.CR1
IDE:
idea 5.1
以前都是用XDoclet来实现类似Annotation的功能,但是xdoclet的版本更新实在太慢,永远跟不上hibernate的更新速度,导致很多hibernate新的功能xdoclet都没办法支持.还好,jdk5.0有了Annotation,而且hibernate也开始支持Annotation了.
原来以为从xdoclet转到Annotation很简单,没想到还是遇到了问题,先看代码:
看上去没什么问题吧?没想到运行的时候根本跑不起来,老是提示:
看了半天看不出有什么问题,最后终于参考hibernate-annotations-3.2.0.CR1里面单元测试的代码才发现是这段代码有问题:
要改成:
原来我用idea开发时,在自动完成的时候想当然地选用了org.hibernate.annotations包下面的annotation,而实际上应该使用javax.persistence包下面的annotation.想想看也很有道理:这样一来我们的JAVA bean就不用import hibernate的包了,用的都是标准的java包.
hibernate-3.2.0.cr2
hibernate-annotations-3.2.0.CR1
IDE:
idea 5.1
以前都是用XDoclet来实现类似Annotation的功能,但是xdoclet的版本更新实在太慢,永远跟不上hibernate的更新速度,导致很多hibernate新的功能xdoclet都没办法支持.还好,jdk5.0有了Annotation,而且hibernate也开始支持Annotation了.
原来以为从xdoclet转到Annotation很简单,没想到还是遇到了问题,先看代码:
1package test;
2
3import org.hibernate.annotations.Entity;
4
5import javax.persistence.Id;
6import java.io.Serializable;
7
8@Entity
9public class Flight implements Serializable{
10 Long id;
11 String name;
12
13 @Id
14 public Long getId(){
15 return id;
16 }
17
18 public void setId(Long id){
19 this.id=id;
20 }
21
22 public String getName(){
23 return name;
24 }
25
26 public void setName(String name){
27 this.name=name;
28 }
29
30 public boolean equals(Object o){
31 if(this==o) return true;
32 if(o==null||getClass()!=o.getClass()) return false;
33
34 final Flight flight=(Flight)o;
35
36 if(id!=null?!id.equals(flight.id):flight.id!=null) return false;
37 if(name!=null?!name.equals(flight.name):flight.name!=null) return false;
38
39 return true;
40 }
41
42 public int hashCode(){
43 int result;
44 result=(id!=null?id.hashCode():0);
45 result=29*result+(name!=null?name.hashCode():0);
46 return result;
47 }
48}
49
2
3import org.hibernate.annotations.Entity;
4
5import javax.persistence.Id;
6import java.io.Serializable;
7
8@Entity
9public class Flight implements Serializable{
10 Long id;
11 String name;
12
13 @Id
14 public Long getId(){
15 return id;
16 }
17
18 public void setId(Long id){
19 this.id=id;
20 }
21
22 public String getName(){
23 return name;
24 }
25
26 public void setName(String name){
27 this.name=name;
28 }
29
30 public boolean equals(Object o){
31 if(this==o) return true;
32 if(o==null||getClass()!=o.getClass()) return false;
33
34 final Flight flight=(Flight)o;
35
36 if(id!=null?!id.equals(flight.id):flight.id!=null) return false;
37 if(name!=null?!name.equals(flight.name):flight.name!=null) return false;
38
39 return true;
40 }
41
42 public int hashCode(){
43 int result;
44 result=(id!=null?id.hashCode():0);
45 result=29*result+(name!=null?name.hashCode():0);
46 return result;
47 }
48}
49
看上去没什么问题吧?没想到运行的时候根本跑不起来,老是提示:
[java] 10:54:55,842 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] org.hibernate.hql.ast.QuerySyntaxException: Flight is not mapped [from Flight]
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:180)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:710)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[java] at org.apache.tools.ant.Task.perform(Task.java:364)
[java] at org.apache.tools.ant.Target.execute(Target.java:341)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:668)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:187)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
[java] org.hibernate.hql.ast.QuerySyntaxException: Flight is not mapped [from Flight]
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:180)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:710)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[java] at org.apache.tools.ant.Task.perform(Task.java:364)
[java] at org.apache.tools.ant.Target.execute(Target.java:341)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:668)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:187)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
看了半天看不出有什么问题,最后终于参考hibernate-annotations-3.2.0.CR1里面单元测试的代码才发现是这段代码有问题:
1import org.hibernate.annotations.Entity;
2
3import javax.persistence.Id;
4import java.io.Serializable;
5
2
3import javax.persistence.Id;
4import java.io.Serializable;
5
要改成:
1import javax.persistence.Id;
2import javax.persistence.Entity;
3import java.io.Serializable;
2import javax.persistence.Entity;
3import java.io.Serializable;
原来我用idea开发时,在自动完成的时候想当然地选用了org.hibernate.annotations包下面的annotation,而实际上应该使用javax.persistence包下面的annotation.想想看也很有道理:这样一来我们的JAVA bean就不用import hibernate的包了,用的都是标准的java包.
推荐阅读
-
解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题 HibernateJavaAntApacheIDEA
-
解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题 HibernateJavaAntApacheIDEA
-
解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题 HibernateJavaAntApacheIDEA
-
解决 Hibernate Annotation 的 "no persistent classes found for query class" 问题 HibernateJavaAntApacheIDEA