日期:2014-05-17  浏览次数:20907 次

hibernate+mysql 插入时报错 Column XXX specified twice
Rss.hbm.xml中:
省略...
<many-to-one name="city" column="city_id" cascade="save-update"></many-to-one>
省略...
City.hbm.xml中:
省略
<set name="rsses" inverse="true" lazy="false">
   <key column="city_id" />
   <one-to-many class="com.citycard.pojo.Rss"/>
</set>
省略

Rss和City是多对一的关系,即一个city对应多个rss,已经实现了关联查询,可是在新插入一个rss 的时候会报错:
ERROR: Column 'CITY_ID' specified twice
org.hibernate.exception.SQLGrammarException: Column 'CITY_ID' specified twice
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy16.executeUpdate(Unknown Source)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2767)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3278)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)

生成的HQL语句是
Hibernate: insert into rss (RSS_NAME, RSS_URL, CITY_ID, city_id) values (?, ?, ?, ?)
,我猜是Rss.hbm.xml中<many-to-one name="city" column="city_id" cascade="save-update"></many-to-one>导致的,但是注释掉这句后,可以插入新RSS,但是关联查询又无法实现了,QAQ求大神帮助啊。。。
------解决方案--------------------
<many-to-one name="city" column="city_id" cascade="save-update"></many-to-one>加上insert="false"