问题描述
学习hibernate注解建表的时候出现的问题packagedemo.annotations.entity;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.Table;@Entitypublicclassweapon{privateintid;privateStringnamt;@Id@GeneratedValue(strategy=GenerationType.SEQUENCE)publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetNamt(){returnnamt;}publicvoidsetNamt(Stringnamt){this.namt=namt;}}
=================================================================packagedemo.annotations.entity;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.cfg.AnnotationConfiguration;publicclassTestpo{publicstaticAnnotationConfigurationconfig=newAnnotationConfiguration();publicstaticSessionFactorysessionFactory;publicstaticStringcontig_file="/hibernate.cfg.xml";publicstaticvoidmain(String[]args){config.configure(contig_file);sessionFactory=config.buildSessionFactory();Sessionsession=sessionFactory.openSession();Transactiontran=session.beginTransaction();weaponwp=newweapon();wp.setNamt("恐龙");session.save(wp);tran.commit();session.close();}}
提示错误是log4j:WARNNoappenderscouldbefoundforlogger(org.hibernate.cfg.annotations.Version).log4j:WARNPleaseinitializethelog4jsystemproperly.log4j:WARNSeehttp://logging.apache.org/log4j/1.2/faq.html#noconfigformoreinfo.Hibernate:selecthibernate_sequence.nextvalfromdualExceptioninthread"main"org.hibernate.exception.SQLGrammarException:couldnotgetnextsequencevalueatorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)atorg.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:119)atorg.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:81)atorg.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)atorg.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)atorg.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)atorg.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:551)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:547)atdemo.annotations.entity.Testpo.main(Testpo.java:24)Causedby:java.sql.SQLException:ORA-02289:序列不存在然后把@GeneratedValue(strategy=GenerationType.SEQUENCE)
句话去掉后报错是log4j:WARNNoappenderscouldbefoundforlogger(org.hibernate.cfg.annotations.Version).log4j:WARNPleaseinitializethelog4jsystemproperly.log4j:WARNSeehttp://logging.apache.org/log4j/1.2/faq.html#noconfigformoreinfo.Hibernate:insertintoweapon(namt,id)values(?,?)Exceptioninthread"main"org.hibernate.exception.SQLGrammarException:CouldnotexecuteJDBCbatchupdateatorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)atorg.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)atorg.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)atorg.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)atorg.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)atorg.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)atorg.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)atdemo.annotations.entity.Testpo.main(Testpo.java:28)Causedby:java.sql.BatchUpdateException:ORA-00942:表或视图不存在
解决方案
解决方案二:
确实日志的jar包
解决方案三:
上面帖子中的确实改缺失。
解决方案四:
引用2楼yuxinjian的回复:
上面帖子中的确实改缺失。
大哥你说的是哪个啊包啊拜托啦!!!
解决方案五:
你至少应该为成员属性id,namt加上@Column属性吧
解决方案六:
@GeneratedValue(strategy=GenerationType.SEQUENCE)
GenerationType.SEQUENCE这种主键生成策略是调用底层数据库的序列来生成主键(例如Oracle),楼主用的是什么数据库?如果去掉@GeneratedValue注解(相当于GenerationType.AUTO),那就得手动设置ID值。
解决方案七:
楼上说的不错,你的数据库如果是oracle才用sequence
解决方案八:
你的数据库的方言是oracle吗?如果不是的,请检查以下啊,要换成自己的数据库的方言啊...