问题描述
登陆用户信息:users.hbm.xml<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.po.Users" table="users"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment"/> </id> <one-to-one name="userinfos" cascade="all"></one-to-one> <property name="username" type="java.lang.String"> <column name="username" length="20" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="20" /> </property> <property name="sort" type="java.lang.String"> <column name="sort" length="10" /> </property> </class></hibernate-mapping>用户详细信息:userinfos.hbm.xml<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.po.Userinfos" table="userinfos"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="foreign"> <param name="property">users</param> </generator> </id> <one-to-one name="users" class="com.po.Users" constrained="true"/> <property name="sex" type="java.lang.String"> <column name="sex" length="2" /> </property> <property name="registertime" type="java.util.Date"> <column name="registertime" length="19" not-null="true" /> </property> <property name="homeaddress" type="java.lang.String"> <column name="homeaddress" length="30" /> </property> <property name="post" type="java.lang.String"> <column name="post" length="10" /> </property> <property name="phone" type="java.lang.String"> <column name="phone" length="20" /> </property> <property name="cardno" type="java.lang.String"> <column name="cardno" length="30" /> </property> <property name="email" type="java.lang.String"> <column name="email" length="20" /> </property> <property name="birth" type="java.lang.String"> <column name="birth" length="20" /> </property> </class></hibernate-mapping>他们是一对一的关系测试类:public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub Users u=new Users(); Userinfos userinfos=new Userinfos(); ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); u.setUsername("hisen"); u.setPassword("123456"); u.setSort("用户"); userinfos.setCardno("333"); userinfos.setUsers(u); u.setUserinfos(userinfos); IUserDao userDao=(IUserDao)ctx.getBean("userDao"); userDao.saveUser(u); }保存的时候出现错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: could not instantiate id generatorat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)at java.security.AccessController.doPrivileged(Native Method)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)at com.Test.main(Test.java:19)Caused by: org.hibernate.MappingException: could not instantiate id generatorat 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 org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)... 15 moreCaused by: org.hibernate.MappingException: param named "property" is required for foreign id generation strategyat org.hibernate.id.ForeignGenerator.configure(ForeignGenerator.java:82)at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)... 23 more不知道哪里配置错了
解决方案
sessionFactory估计你的spring接管hibernate的地方配置不对吧