日期:2014-05-20  浏览次数:20845 次

使用Hibernate注解自动创建表报错
实体类
Java code

package com.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


/*
 * 使用注解创建并关联数据库中的表,需要在hibernate配置文件中设置映射
 */

@Entity
public class Weapon {
    private long id;
    private String name;
    
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    //@SequenceGenerator(name="weapon_seq")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}



Hibernate配置文件中添加
Java code

<mapping class="com.entity.Weapon"/>


测试类
Java code

package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.entity.Weapon;

/*
 * 通过在hibernate文件中注册实体bean,并在实体bean中添加
 * hibernate注解,可以自动创建数据库中的关联表并将属性与字段关联
 */
public class TestAnnotations {
    private static AnnotationConfiguration ac = new AnnotationConfiguration().configure();
    
    private static SessionFactory sf = null;
    
    public static void main(String[] args) {
        sf = ac.buildSessionFactory();
        Session session = sf.openSession();
        Transaction tran = session.beginTransaction();
        Weapon weapon = new Weapon();
        weapon.setName("jack");
        weapon.setId(1);
        session.save(weapon);
        tran.commit();
        session.close();
    }
}




出现错误信息
Java code

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Weapon (name, id) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at com.test.TestAnnotations.main(TestAnnotations.java:27)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

    at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:8739)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more




查询数据库Weapon表没被创建,有遇到这样问题的朋友帮忙分析一下啊,谢谢。