小女子大问题
====hibernate 级联删除 问题====
现有 相册(Album) 和 照片 2个表(Photo)
我想在删除 相册的同时删除相册里所有的照片
两个表的实体和映射如下
public class Album implements Serializable {
private int id;
private String name;//名称
private String description;//描述
private String pwd;//密码
private AlbumState albumState;//状态 多对一 单向
private User user;//所属用户// 多对一 单向
private Photo photo;//相册封面 多对一
private List<Photo> photsSet; // 所有相片 一对多
private String createDate;//创建时间
private String updateDate;//更新时间
private int count;//相册中的照片数。不配置hbm.xml
......
}
关键映射
<!--相册状态 多对一-->
<many-to-one name="albumState" column="albumstate_id" fetch="join" lazy="false"></many-to-one><!-- 相册封面 多对一 -->
<many-to-one name="photo" column="photo_id" fetch="join" lazy="false"></many-to-one><!-- 所属用户 多对一 单向 -->
<many-to-one name="user" column="user_id"></many-to-one><!-- 所有照片 一对多 单向 -->
<list name="photsSet" lazy="true" cascade="delete" >
<key column="album_id"></key>
<list-index column="id"/>
<one-to-many class="Photo"/>
</list>
--------
public class Photo implements Serializable {
private int id;
private String name;
private String description;
private String img;//原图
private String thumbnail;//缩略图
private User user;//所属用户
private Album album;//所属相册 多对一
.......
}
关键映射
<!--所属相册 多对一-->
<many-to-one name="album" column="album_id"></many-to-one>
现在吧,数据库里的数据是这样的
下图t_album 表 这是相册id为31的相册
下图t_photo表 这是相册id为31的所有照片
也就是相册id为31的相册封面照片的id是100.而相册id为31的相册中包含相片98,99,100 三张
现在我通过单元测试执行如下代码
欲删除id为31的相册以及里面的照片。
因为我在Album 的一对多(photsSet)中配置了级联删除(cascade="delete")
但是不管用~~~ 而且出现如下异常
/*删除相册*/
public void deleteAlbum(){
Session session=HibernateUtil.getSession();
session.beginTransaction();
Album album=new Album();
album.setId(31);
try {
session.delete(album);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}finally{
HibernateUtil.closeSession(session);
}
}
异常
Hibernate: update t_photo set album_id=null, id=null where album_id=?
03 五月 2009 20:13:27.500 [ERROR] [main] [org.hibernate.util.JDBCExceptionReporter] - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法更新标识列 'id'。
03 五月 2009 20:13:27.500 [ERROR] [main] [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.xiaonei.zy.test.InitalAlbumTest.deleteAlbum(InitalAlbumTest.java:293)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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: com.microsoft.jdbc.base.BaseBatchUpdateException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法更新标识列 'id'。
at com.microsoft.jdbc.base.BasePreparedStatement.executeBatchEmulation(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeBatch(Unknown Source)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 24 more
它总是在说无法更新标识列。。这里不懂了。不太明白是怎么回事!
如何解决呢?我就是想通过hibernate的级联删除 ,删除相册的时候删除其关联的照片 。但是不行啊,而且还出现更新语句。。不懂了。。
谁帮我瞅瞅呀~~~~
知道的朋友可以加我QQ136836301(说明:hibernate级联删除问题)
====hibernate 级联删除 问题====
现有 相册(Album) 和 照片 2个表(Photo)
我想在删除 相册的同时删除相册里所有的照片
两个表的实体和映射如下
public class Album implements Serializable {
private int id;
private String name;//名称
private String description;//描述
private String pwd;//密码
private AlbumState albumState;//状态 多对一 单向
private User user;//所属用户// 多对一 单向
private Photo photo;//相册封面 多对一
private List<Photo> photsSet; // 所有相片 一对多
private String createDate;//创建时间
private String updateDate;//更新时间
private int count;//相册中的照片数。不配置hbm.xml
......
}
关键映射
<!--相册状态 多对一-->
<many-to-one name="albumState" column="albumstate_id" fetch="join" lazy="false"></many-to-one><!-- 相册封面 多对一 -->
<many-to-one name="photo" column="photo_id" fetch="join" lazy="false"></many-to-one><!-- 所属用户 多对一 单向 -->
<many-to-one name="user" column="user_id"></many-to-one><!-- 所有照片 一对多 单向 -->
<list name="photsSet" lazy="true" cascade="delete" >
<key column="album_id"></key>
<list-index column="id"/>
<one-to-many class="Photo"/>
</list>
--------
public class Photo implements Serializable {
private int id;
private String name;
private String description;
private String img;//原图
private String thumbnail;//缩略图
private User user;//所属用户
private Album album;//所属相册 多对一
.......
}
关键映射
<!--所属相册 多对一-->
<many-to-one name="album" column="album_id"></many-to-one>
现在吧,数据库里的数据是这样的
下图t_album 表 这是相册id为31的相册
下图t_photo表 这是相册id为31的所有照片
也就是相册id为31的相册封面照片的id是100.而相册id为31的相册中包含相片98,99,100 三张
现在我通过单元测试执行如下代码
欲删除id为31的相册以及里面的照片。
因为我在Album 的一对多(photsSet)中配置了级联删除(cascade="delete")
但是不管用~~~ 而且出现如下异常
/*删除相册*/
public void deleteAlbum(){
Session session=HibernateUtil.getSession();
session.beginTransaction();
Album album=new Album();
album.setId(31);
try {
session.delete(album);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}finally{
HibernateUtil.closeSession(session);
}
}
异常
Hibernate: update t_photo set album_id=null, id=null where album_id=?
03 五月 2009 20:13:27.500 [ERROR] [main] [org.hibernate.util.JDBCExceptionReporter] - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法更新标识列 'id'。
03 五月 2009 20:13:27.500 [ERROR] [main] [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.xiaonei.zy.test.InitalAlbumTest.deleteAlbum(InitalAlbumTest.java:293)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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: com.microsoft.jdbc.base.BaseBatchUpdateException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法更新标识列 'id'。
at com.microsoft.jdbc.base.BasePreparedStatement.executeBatchEmulation(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeBatch(Unknown Source)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 24 more
它总是在说无法更新标识列。。这里不懂了。不太明白是怎么回事!
如何解决呢?我就是想通过hibernate的级联删除 ,删除相册的时候删除其关联的照片 。但是不行啊,而且还出现更新语句。。不懂了。。
谁帮我瞅瞅呀~~~~
知道的朋友可以加我QQ136836301(说明:hibernate级联删除问题)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货