首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

使用Hibernate注解自动创建表报错,该如何解决

2012-03-30 
使用Hibernate注解自动创建表报错实体类Java codepackage com.entityimport javax.persistence.Entityim

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

Java code
package com.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;/* * 使用注解创建并关联数据库中的表,需要在hibernate配置文件中设置映射 */@Entitypublic 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 dualHibernate: 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表没被创建,有遇到这样问题的朋友帮忙分析一下啊,谢谢。



[解决办法]
有没有在配置文件里设置对象建模工具???
<property name="hibernate.hbm2ddl.auto">update</property>
有没有???
[解决办法]

探讨
恩,很给力!这个,真没加。而且加上了可以自动创建表了。不过为什么教学视频上根本没提要加这句啊?这句话具体的含义能详细解释一下吗?

热点排行