问题描述
搞了半天了也不知道为什么,大家帮帮我啊,加我QQ教我也行:494005463!谢谢Member类:packagejfish;importjava.util.Date;importjava.util.HashSet;importjava.util.Set;publicclassMember{privateStringname;//会员名privateStringid;//会员IDprivateSet<Song>store_Song=newHashSet<Song>();publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicSet<Song>getStore_Song(){returnstore_Song;}publicvoidsetStore_Song(Set<Song>storeSong){store_Song=storeSong;}}
Song类:packagejfish;importjava.util.Date;importjava.util.HashSet;importjava.util.Set;publicclassSong{privateStringid;//歌曲IDprivateStringname;//歌曲名privateSet<Member>store_Member=newHashSet<Member>();publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicSet<Member>getStore_Member(){returnstore_Member;}publicvoidsetStore_Member(Set<Member>storeMember){store_Member=storeMember;}}
Song.hbm.xml:<?xmlversion="1.0"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><classname="jfish.Song"><idname="id"><generatorclass="uuid"/></id><propertyname="name"unique="true"not-null="true"length="30"column="c_name"/><setname="store_Member"table="t_store_song"inverse="false"lazy="true"cascade="all"><keycolumn="song_id"/><many-to-manyclass="jfish.Member"/></set></class></hibernate-mapping>
Member.hbm.xml:<?xmlversion="1.0"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><classname="jfish.Member"><idname="id"length="32"><generatorclass="uuid"/></id><propertyname="name"unique="true"not-null="true"length="30"column="c_name"/><setname="store_Song"table="t_store_song"inverse="true"><keycolumn="member_id"/><many-to-manyclass="jfish.Song"/></set></class></hibernate-mapping>
测试类:packagejfish;importjava.util.Date;importjava.util.HashSet;importjava.util.Set;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.cfg.Configuration;importorg.hibernate.tool.hbm2ddl.SchemaExport;publicclassTest{publicstaticvoidmain(String[]arg){//读取hibernate配置文件hibernate.cfg.xmlConfigurationcfg=newConfiguration().configure();//创建SessionFactorySessionFactoryfactory=cfg.buildSessionFactory();//创建SessionSessionsession=factory.openSession();//开启事务session.beginTransaction();Membermember=newMember();member.setName("nninnn11");Songsong=newSong();song.setName("111111");session.save(song);session.save(member);//member.getStore_Song().add(song);song.getStore_Member().add(member);//提交事务session.getTransaction().commit();if(session.isOpen()){session.close();}}}
错误信息:Hibernate:insertintoSong(c_name,id)values(?,?)Hibernate:insertintoMember(c_name,id)values(?,?)Hibernate:insertintot_store_song(song_id,elt)values(?,?)对这句话的疑问,应该生成的是insertintot_store_song(song_id,elt,member_id)values(?,?,?);19:53:11,815WARNJDBCExceptionReporter:77-SQLError:1364,SQLState:HY00019:53:11,817ERRORJDBCExceptionReporter:78-Field'member_id'doesn'thaveadefaultvalue19:53:11,821ERRORAbstractFlushingEventListener:301-Couldnotsynchronizedatabasestatewithsession
解决方案
本帖最后由 fullmoonyes 于 2010-04-21 20:03:41 编辑
解决方案二:
顶起吧,关注!!!
解决方案三:
<idname="id"length="32"><generatorclass="uuid"/></id>
解决方案四:
楼上的,那里怎么了啊?说清楚点啊!
解决方案五:
大家都来看看啊,急啊!