关于hibernate4.3.8final版本(annotation)注解问题 相当的bug啊 求大神指点迷津

在hibernate官方文档中给出了 如下注解方式:

5.1. Mapping declaration

Object/relational mappings can be defined in three approaches:

  • using Java 5 annotations (via the Java Persistence 2 annotations)
  • using JPA 2 XML deployment descriptors (described in chapter XXX)
  • using the Hibernate legacy XML files approach known as hbm.xml

Annotations are split in two categories, the logical mapping annotations (describing the object model, the association between two entities etc.) and the physical mapping annotations (describing the physical schema, tables, columns, indexes, etc). We will
mix annotations from both categories in the following code examples.

JPA annotations are in the javax.persistence.* package. Hibernate specific extensions are in
org.hibernate.annotations.*. You favorite IDE can auto-complete annotations and their attributes for you (even without a specific "JPA" plugin, since JPA annotations are plain Java 5 annotations).

Here is an example of mapping

package eg;

@Entity
@Table(name="cats") @Inheritance(strategy=SINGLE_TABLE)
@DiscriminatorValue("C") @DiscriminatorColumn(name="subclass", discriminatorType=CHAR)
public class Cat {

   @Id @GeneratedValue
   public Integer getId() { return id; }
   public void setId(Integer id) { this.id = id; }
   private Integer id;

   public BigDecimal getWeight() { return weight; }
   public void setWeight(BigDecimal weight) { this.weight = weight; }
   private BigDecimal weight;

   @Temporal(DATE) @NotNull @Column(updatable=false)
   public Date getBirthdate() { return birthdate; }
   public void setBirthdate(Date birthdate) { this.birthdate = birthdate; }
   private Date birthdate;

   @org.hibernate.annotations.Type(type="eg.types.ColorUserType")
   @NotNull @Column(updatable=false)
   public ColorType getColor() { return color; }
   public void setColor(ColorType color) { this.color = color; }
   private ColorType color;

   @NotNull @Column(updatable=false)
   public String getSex() { return sex; }
   public void setSex(String sex) { this.sex = sex; }
   private String sex;

   @NotNull @Column(updatable=false)
   public Integer getLitterId() { return litterId; }
   public void setLitterId(Integer litterId) { this.litterId = litterId; }
   private Integer litterId;

   @ManyToOne @JoinColumn(name="mother_id", updatable=false)
   public Cat getMother() { return mother; }
   public void setMother(Cat mother) { this.mother = mother; }
   private Cat mother;

   @OneToMany(mappedBy="mother") @OrderBy("litterId")
   public Set<Cat> getKittens() { return kittens; }
   public void setKittens(Set<Cat> kittens) { this.kittens = kittens; }
   private Set<Cat> kittens = new HashSet<Cat>();
}

@Entity @DiscriminatorValue("D")
public class DomesticCat extends Cat {

   public String getName() { return name; }
   public void setName(String name) { this.name = name }
   private String name;
}

自己写出的的注解代码:

package org.hibernate.annotation.entity;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity(name="_teacher")

public class Teacher {

private Integer id;

private String name;

private String title;

@Id

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

}

差别 如果是@Entity(name="_teacher")这样去注解当database中的table与实体类名不一直的时候这样是可以通过的

而当你在hibernate4.3.8版本也就是4.0以后的版本中,你按照hibernate官方文档去注解

package org.hibernate.annotation.entity;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@(table="_teacher")

public class Teacher {

private Integer id;

private String name;

private String title;

@Id

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

}

此刻是编译通过了而在运行时却通过不了 一直抛以下异常:

java.lang.ExceptionInInitializerError

at org.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:26)

at org.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:10)

at org.hibernate.test.HibernateTest.test2(HibernateTest.java:20)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.junit.runners.ParentRunner.run(ParentRunner.java:292)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax/persistence/Index;

at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:973)

at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:824)

at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)

at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)

at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)

at org.hibernate.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:23)

... 25 more

--楼主虽然已经解决此问题,但是针对hibernate注解,官方文档给出的不能使用,一直不太明白,还请大神指点,共同探讨

时间: 2024-09-30 04:36:24

关于hibernate4.3.8final版本(annotation)注解问题 相当的bug啊 求大神指点迷津的相关文章

hibernate annotation注解方式来处理映射关系

在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用annotation的方式可以更简介,所以这里就简单记录下通过annotation来配置各种映射关系,在hibernate4以后已经将annotation的jar包集成进来了,如果使用hibernate3的版本就需要引入annotation的jar包. 一.单对象操作 @Entity

第17篇-JAVA Annotation 注解

第17篇-JAVA Annotation 注解 每篇一句 :真的努力后你会发现自己要比想象的优秀很多 初学心得: 怀着一颗奋斗不息的心,一切困苦艰辛自当迎刃而解 (笔者:JEEP/711)[JAVA笔记 | 时间:2017-05-17| JAVA Annotation注解 ] 1.什么是注解(Annotation) Annotation 其实就是代码里的特殊标记, 它用于替代配置文件,也就是说,传统方式通过配置文件告诉类如何运行,有了注解技术后,开发人员可以通过注解告诉类如何运行.在Java技术

Java基础笔记 – Annotation注解的介绍和使用 自定义注解

Java基础笔记 – Annotation注解的介绍和使用 自定义注解 本文由arthinking发表于5年前 | Java基础 | 评论数 7 |  被围观 25,969 views+ 1.Annotation的工作原理:2.@Override注解:3.@Deprecated注解:4.@SuppressWarnings注解:5.自定义注解:5.1.添加变量:5.2.添加默认值:5.3.多变量使用枚举:5.4.数组变量:6.设置注解的作用范围:6.1.在自定义注解中的使用例子:7.使用反射读取R

自己写的基于java Annotation(注解)的数据校验框架

JavaEE6中提供了基于java Annotation(注解)的Bean校验框架,Hibernate也有类似的基于Annotation的数据校验功能,我在工作中,产品也经常需要使 用数据校验,为了方便和重用,自己写了一个简单的基于Annotation的校验框架.有兴趣的可以扩展. 框架说明: AnnotationValidable接口:所有需要使用该校验框架的类都要实现它,该类中没有任何方法需要实现,仅仅是一个表明那些类需要使用该校验框架的标识. GetFiledValue类:是一个工具类,对

Java Annotation 注解

java_notation.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin: 0 0 10px; padding: 8px 10px 0; font: 13.34px/1.4 helvetica, arial, freesans, clean, sans-serif; width: 452px; background-color: #fff } div.oembedall

Android开发学习之路--Annotation注解简化view控件之初体验

一般我们在写android Activity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,每次写这么多代码总觉得很烦躁.近来看了下android中有Annotation来实现这方面的简化,对于java不是很了解,就简单的看了下.上次玩web的时候,springmvc也有很多的注解,不知道怎么实现的,这里其实基本上类似. Annotation注解这里主要还是讲讲怎么使用吧,单纯的原理会把人绕进去的,没办法,

Hibernate中的Annotation注解与XML配置的使用

通过XML配置来实现持久化: 首先我们来建立一个实体类: package com.hibernate.model; public class Student {     private int id;     private String name;     private int age;          public int getId() {         return id;     }     public void setId(int id) {         this.id 

struts 文件下载 annotation 注解版

[本文简介] 本文将简单介绍使用 struts2 ,通过零配置和 annotation 实现文件下载功能. [文件夹结构] [web.xml有关struts的配置] <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

spring framework Annotation(注解)

spring  framework (4.2.4)Annotation(注解)1.建立java项目 所用JAR commons-logging-1.1.3.jar spring-aop-4.2.4.RELEASE.jar spring-beans-4.2.4.RELEASE.jar spring-context-4.2.4.RELEASE.jar spring-core-4.2.4.RELEASE.jar spring-expression-4.2.4.RELEASE.jar 2.创建bean.