zoukankan      html  css  js  c++  java
  • 使用Hibernate注解Annotations进行对象映射的异常处理

     通过Hibernate注解Annotations进行对象映射,想在Oracle数据库中自动创建表,代码如下:

    实体类:

    import javax.persistence.Basic;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    @Entity
    public class Weapon {
    private long id;
    private String name;
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    public long getId() {
     return id;}
    public void setId(long id) {
     this.id = id;}
    @Basic
    public String getName() {
     return name;}
    public void setName(String name) {
     this.name = name;}}

    hibernate.cfg.xml中:

    mapping class="demo.annotations.entity.Weapon"

    测试类:

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.AnnotationConfiguration;
    import demo.annotations.entity.Weapon;

    public class TestAnnotations {
     public static AnnotationConfiguration config2=new AnnotationConfiguration();
     public static SessionFactory sessionFactory;
     public static String config_file="/hibernate.cfg.xml";
     public static void main(String[] args) {
         config2.configure(config_file);
         sessionFactory=config2.buildSessionFactory();
         Session session=sessionFactory.openSession();
         Transaction tx=session.beginTransaction();
         Weapon weapon=new Weapon();
         weapon.setName("屠鹰刀");
         session.save(weapon);
         tx.commit();
         session.close();}}

         运行的时候接连出现异常:首先抛出了无法找到执行类的定义的异常:

         ava.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/

         仔细检查后:发现少导入了一个hibernate-commons-annotations.jar的包,使用Hibernate注解需要三个jar包:ejb3-persistence.jar、hibernate-annotations.jar和hibernate-commons-annotations.jar包。

         导入jar包后继续运行,又报了新的异常:

    890 ERROR JDBCExceptionReporter:101 - ORA-02289: 序列不存在
    Hibernate: select hibernate_sequence.nextval from dual
    859  WARN JDBCExceptionReporter:100 - SQL Error: 2289, SQLState: 42000

        于是开始思考数据库中的序列怎么与表进行关联的问题,审视异常信息后,我把数据库中的序列名称改为了hibernate_sequence,终于渡过这个异常。因为我在@GeneratedValue中设置的是通过sequence自动生成主键武器编号,所以必须要有相应的序列与Weapon表中的主键对应。

         然而天公不作美,此时还是没有成功,报的是表或视图不存在的异常:

    265 ERROR JDBCExceptionReporter:101 - ORA-00942: 表或视图不存在

    265  WARN JDBCExceptionReporter:100 - SQL Error: 942, SQLState: 42000
         这个异常让我丈二和尚抓不着头脑了,注解不能自动地生成表吗?还是我的jar包有问题?我开始总纠结于自己的jar包,怎么调整都不起作用。于是又进行了各种胡乱的尝试,均不成功,开始有些崩溃了。经过屡番失败,在hibernate.cfg.xml中加入property name="hibernate.hbm2ddl.auto"  update  property,执行成功!

        这句话的含义是:只是根据映射文件去和数据库中的表对应起来,如果不一致,就更新表的结构。在自动创建表的环节中需要。这样就正确的建立了Weapon表并且成功添加了一条数据。

  • 相关阅读:
    OSX安装nginx和rtmp模块(rtmp直播服务器搭建)
    用runtime来重写Coder和deCode方法 归档解档的时候使用
    Homebrew安装卸载
    Cannot create a new pixel buffer adaptor with an asset writer input that has already started writing'
    OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
    让nginx支持HLS
    iOS 字典转json字符串
    iOS 七牛多张图片上传
    iOS9UICollectionView自定义布局modifying attributes returned by UICollectionViewFlowLayout without copying them
    Xcode6 iOS7模拟器和Xcode7 iOS8模拟器离线下载
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5357898.html
Copyright © 2011-2022 走看看