软件版本:
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很简单,没想到还是遇到了问题,先看代码:
看上去没什么问题吧?没想到运行的时候根本跑不起来,老是提示:
[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
)
看了半天看不出有什么问题,最后终于参考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很简单,没想到还是遇到了问题,先看代码:
1
package
test;
2![None.gif](/Images/OutliningIndicators/None.gif)
3
import
org.hibernate.annotations.Entity;
4![None.gif](/Images/OutliningIndicators/None.gif)
5
import
javax.persistence.Id;
6
import
java.io.Serializable;
7![None.gif](/Images/OutliningIndicators/None.gif)
8
@Entity
9![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
class
Flight
implements
Serializable
{
10
Long id;
11
String name;
12![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
@Id
14![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Long getId()
{
15
return id;
16
}
17![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setId(Long id)
{
19
this.id=id;
20
}
21![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getName()
{
23
return name;
24
}
25![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setName(String name)
{
27
this.name=name;
28
}
29![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean equals(Object o)
{
31
if(this==o) return true;
32
if(o==null||getClass()!=o.getClass()) return false;
33![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
final Flight flight=(Flight)o;
35![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
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![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
return true;
40
}
41![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![None.gif](/Images/OutliningIndicators/None.gif)
7
![None.gif](/Images/OutliningIndicators/None.gif)
8
![None.gif](/Images/OutliningIndicators/None.gif)
9
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
40
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
49
![None.gif](/Images/OutliningIndicators/None.gif)
看上去没什么问题吧?没想到运行的时候根本跑不起来,老是提示:
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
看了半天看不出有什么问题,最后终于参考hibernate-annotations-3.2.0.CR1里面单元测试的代码才发现是这段代码有问题:
1
import
org.hibernate.annotations.Entity;
2![None.gif](/Images/OutliningIndicators/None.gif)
3
import
javax.persistence.Id;
4
import
java.io.Serializable;
5
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
要改成:
1
import
javax.persistence.Id;
2
import
javax.persistence.Entity;
3
import
java.io.Serializable;
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
原来我用idea开发时,在自动完成的时候想当然地选用了org.hibernate.annotations包下面的annotation,而实际上应该使用javax.persistence包下面的annotation.想想看也很有道理:这样一来我们的JAVA bean就不用import hibernate的包了,用的都是标准的java包.