Clazz.hbm.xml;<hibernate-mapping
package="com.hib.entity">
<class name="Clazz" table="clazz">
<id name="id" type="java.lang.Long">
<column name="ID"></column>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"></property>
<property name="studentCount" type="java.lang.Integer">
<column name="student_count"></column>
</property>
<property name="re" type="java.lang.String"></property>
<set name="students" table="student" inverse="true" fetch="join" cascade="save-update">
<key column="clazzID"></key>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<hibernate-mapping
package="com.hib.entity">
<class name="Student" table="student">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="sid" type="java.lang.Integer"></property>
<property name="name" type="java.lang.String"></property>
<property name="age" type="java.lang.Integer"></property>
<property name="sex" type="java.lang.Integer"></property>
<many-to-one name="clazz" class="Clazz" cascade="all">
<column name="clazzID"></column>
</many-to-one>
</class>
</hibernate-mapping>hibernate.cfg.xml; <mapping resource="com/hib/entity/Clazz.hbm.xml"/>
<mapping resource="com/hib/entity/Student.hbm.xml"/>
package="com.hib.entity">
<class name="Clazz" table="clazz">
<id name="id" type="java.lang.Long">
<column name="ID"></column>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"></property>
<property name="studentCount" type="java.lang.Integer">
<column name="student_count"></column>
</property>
<property name="re" type="java.lang.String"></property>
<set name="students" table="student" inverse="true" fetch="join" cascade="save-update">
<key column="clazzID"></key>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<hibernate-mapping
package="com.hib.entity">
<class name="Student" table="student">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="sid" type="java.lang.Integer"></property>
<property name="name" type="java.lang.String"></property>
<property name="age" type="java.lang.Integer"></property>
<property name="sex" type="java.lang.Integer"></property>
<many-to-one name="clazz" class="Clazz" cascade="all">
<column name="clazzID"></column>
</many-to-one>
</class>
</hibernate-mapping>hibernate.cfg.xml; <mapping resource="com/hib/entity/Clazz.hbm.xml"/>
<mapping resource="com/hib/entity/Student.hbm.xml"/>
解决方案 »
- 在jsp页面有两个seelct s1,s2想实现联动效果 s1,s2都是连接数据库的
- xpath读取XML乱码的问题
- java编写NT服务问题!
- 求一mysql正则表达式
- middlegen是不是只能从数据库形成.hbm.xml文件,而从.hbm.xml文件形成数据库或java是不是就要用hibernate带的hbm2java等工具?
- J2EE具体定义?
- 明天去买书,请过来人推荐两本
- 请问,这个报错是什么问题啊?
- 救命:JDOM如何生成这样的根元素??
- Struts2 + Spring3 + MyBatis集成
- commons fileupload问题,遇到问题如何跳转?
- 大家好,办个小忙 百里马 寻 伯乐
org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at com.hib.factory.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:31)
at com.hib.dao.StudentDAO.saveStudent(StudentDAO.java:13)
at com.hib.test.TestStudentDAO.testSaveStudent(TestStudentDAO.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.MappingException: param named "property" is required for foreign id generation strategy
at org.hibernate.id.ForeignGenerator.configure(ForeignGenerator.java:82)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)
... 22 more
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/hib/entity/Clazz.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at com.hib.factory.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at com.hib.factory.HibernateSessionFactory.getSession(HibernateSessionFactory.java:53)
at com.hib.dao.StudentDAO.saveStudent(StudentDAO.java:13)
at com.hib.test.TestStudentDAO.testSaveStudent(TestStudentDAO.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.DuplicateMappingException: Duplicate collection role mapping com.hib.entity.Clazz.students
at org.hibernate.cfg.Mappings.addCollection(Mappings.java:124)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2066)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2041)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:359)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
at org.hibernate.cfg.Configuration.add(Configuration.java:669)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 25 more
这是异常信息
......
<param name="sequence">xxxxx(你创建的sequence名字)</param>///加上这句</generator>2.如果是Mysql native默认为主键生成方式为auto increment 。你创建这张表的时候 主键勾选auto increment 选项就可以了。这个好办这都是主键生成策略问题。楼主要留意了!!!!!!!!!!!
<generator class="native"/>
表明主键有数据库生成
看看你数据库中的主键是不是标示列
楼主你这个错误应该是hibernate.cfg.xml的错误,里边应该是加载多了
<mapping resource="com/hib/entity/Student.hbm.xml"/>就是这些,你仔细找一下,所有的mapping resource标签对应的xml文件,应该有缺的,意思就是,你的mapping resource标签里肯定的有错误,不管是哪个有错误,都会报第一个错误。也是就你最上边的
<mapping resource="com/hib/entity/Clazz.hbm.xml"/>报他有错误,其实是别的mapping resource错了。
我通常出现这种情况是hibernate.cfg.xml里映射了一张表,但是其实在该路径下并没有那个xml文件,经常有人上传了hibernate.cfg.xml却忘了上传数据库表的xml。所以楼主你就一个一个找吧,或者你把其他的都去掉
只留一个<mapping resource="com/hib/entity/Clazz.hbm.xml"/>就应该不会报错了