问题描述
首先说一下,小弟想用hibernate保存实体到数据库,但是出了一些问题,我用的sqlserver数据库,为表创建了存储过程和触发器。数据库有三个表:student,score,mark为student表创建了一个Insert触发器,当有新纪录添加到该表的时候,自动向mark表中添加一条日志信息这是我的代码1.实体l类publicclassStudentimplementsjava.io.Serializable{//FieldsprivateIntegerid;privateStringsname;privateIntegersex;privateSetscores=newHashSet(0);//Constructors/**defaultconstructor*/publicStudent(){}/**minimalconstructor*/publicStudent(Stringsname){this.sname=sname;}/**fullconstructor*/publicStudent(Stringsname,Integersex,Setscores){this.sname=sname;this.sex=sex;this.scores=scores;}publicStudent(Stringsname,Integersex){this.sname=sname;this.sex=sex;}//PropertyaccessorspublicIntegergetId(){returnthis.id;}publicvoidsetId(Integerid){this.id=id;}publicStringgetSname(){returnthis.sname;}publicvoidsetSname(Stringsname){this.sname=sname;}publicIntegergetSex(){returnthis.sex;}publicvoidsetSex(Integersex){this.sex=sex;}publicSetgetScores(){returnthis.scores;}publicvoidsetScores(Setscores){this.scores=scores;}}2.测试类:publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstub//读取并解析配置文件Configurationconf=newConfiguration().configure();//读取并解析映射文件兮兮SessionFactorysf=conf.buildSessionFactory();Sessionsession=sf.openSession();//打开sessionTransactiontx=null;try{tx=session.beginTransaction();//开始一个事务Studentstudent=newStudent("王青",Integer.valueOf(1));session.save(student);tx.commit();//提交事务}catch(Exceptione){if(tx!=null)tx.rollback();e.printStackTrace();}finally{session.close();}}3.映射文件:student.hbm.xlm<hibernate-mapping><classname="com.DAO.Student"table="student"schema="dbo"catalog="jj"><idname="id"type="java.lang.Integer"><columnname="ID"/><generatorclass="native"/></id><propertyname="sname"type="java.lang.String"><columnname="sname"length="70"not-null="true"/></property><propertyname="sex"type="java.lang.Integer"><columnname="sex"/></property><setname="scores"inverse="true"><key><columnname="sid"not-null="true"/></key><one-to-manyclass="com.DAO.Score"/></set></class></hibernate-mapping>4.映射文件:score.hbm.xml<hibernate-mapping><classname="com.DAO.Score"table="score"schema="dbo"catalog="jj"><composite-idname="id"class="com.DAO.ScoreId"><key-propertyname="id"type="java.lang.Integer"><columnname="ID"/></key-property><key-many-to-onename="student"class="com.DAO.Student"><columnname="sid"/></key-many-to-one><key-propertyname="scores"type="java.lang.Integer"><columnname="scores"/></key-property></composite-id><many-to-onename="student"class="com.DAO.Student"update="false"insert="false"fetch="select"><columnname="sid"not-null="true"/></many-to-one></class></hibernate-mapping>5.映射文件mark.hbm.xnl:<hibernate-mapping><classname="com.DAO.Mark"table="mark"schema="dbo"catalog="jj"><idname="id"type="java.lang.Integer"><columnname="id"/><generatorclass="native"/></id><propertyname="tg"type="java.lang.String"><columnname="tg"length="110"/></property><propertyname="time"type="java.util.Date"><columnname="time"length="23"/></property></class></hibernate-mapping>
解决方案
解决方案二:
为什么不贴出来错误??
解决方案三:
哦,忘了,补充一下,我的报错信息是:org.hibernate.exception.GenericJDBCException:couldnotinsert:[com.DAO.Student]atorg.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)atorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)atorg.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)atorg.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)atorg.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)atorg.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)atorg.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)atorg.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)atorg.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)atorg.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:518)atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:514)atcom.DAO.Test.main(Test.java:25)Causedby:com.microsoft.sqlserver.jdbc.SQLServerException:必须执行该语句才能获得结果。atcom.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(UnknownSource)atcom.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(UnknownSource)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atorg.hibernate.util.GetGeneratedKeysHelper.getGeneratedKey(GetGeneratedKeysHelper.java:65)atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1970)...15more
解决方案四:
Causedby:com.microsoft.sqlserver.jdbc.SQLServerException:必须执行该语句才能获得结果。hibernate配置文件配置连接有问题好像是。连接不到数据库
解决方案五:
引用3楼lmy_java的回复:
hibernate配置文件配置连接有问题好像是。连接不到数据库
从前面的错误信息来看,数据库肯定是连上了的。错误肯定出在触发器和Hibernate的配合使用上。如果不是绕不过去,建议不要在Hibernate管理的表上使用触发器。