问题描述
代码如下;我有一个主题的实体ThemeBean.javaimportjava.io.Serializable;importjava.util.Date;importjava.util.Set;importjava.util.TreeSet;importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;importjavax.persistence.OneToMany;importjavax.persistence.SequenceGenerator;importjavax.persistence.Table;@Entity@Table(name="THEME")publicclassThemeBeanimplementsSerializable{privatestaticfinallongserialVersionUID=-4658290538342157675L;@Id@Column(name="THEME_ID")@SequenceGenerator(name="SEQ_THEME_SEQ",sequenceName="THEME_SEQUENCE",allocationSize=1)@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_THEME_SEQ")privateLongid;@Column(name="THEME_NAME_EN")privateStringthemeNameEn;@OneToMany(cascade=CascadeType.ALL)@JoinColumn(name="THEME_ID")privateSet<ThemeBeanHistory>themeBeanHistorySet;publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicStringgetThemeNameEn(){returnthemeNameEn;}publicvoidsetThemeNameEn(StringthemeNameEn){this.themeNameEn=themeNameEn;}publicSet<ThemeBeanHistory>getThemeBeanHistorySet(){returnthemeBeanHistorySet;}publicvoidsetThemeBeanHistorySet(Set<ThemeBeanHistory>themeBeanHistorySet){this.themeBeanHistorySet=themeBeanHistorySet;}publicvoidaddThemeHistory(ThemeBeanHistorythemeHistory){if(getThemeBeanHistorySet()==null){setThemeBeanHistorySet(newTreeSet());}getThemeBeanHistorySet().add(themeHistory);}}还有个主题历史的实体:importjava.io.Serializable;importjava.util.Date;importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;importjavax.persistence.SequenceGenerator;importjavax.persistence.Table;/****类说明:主题bean历史记录*/@Entity@Table(name="THEME_HISTORY")publicclassThemeBeanHistoryimplementsSerializable{privatestaticfinallongserialVersionUID=-7707936885348490454L;@Id@Column(name="ID")@SequenceGenerator(name="SEQ_THEME_HIS_SEQ",sequenceName="THEME_HISTORY_SEQUENCE",allocationSize=1)@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_THEME_HIS_SEQ")privateLongid;@Column(name="STATUS")privateStringstatus;@Column(name="OPERATION_DATE")privateDateoperationDate;@Column(name="MODIFIER_ID")privateLongmodifierId;@Column(name="OPERATE_RESULT")privateBooleanoperateResult;publicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}publicStringgetStatus(){returnstatus;}publicvoidsetStatus(Stringstatus){this.status=status;}publicDategetOperationDate(){returnoperationDate;}publicvoidsetOperationDate(DateoperationDate){this.operationDate=operationDate;}publicLonggetModifierId(){returnmodifierId;}publicvoidsetModifierId(LongmodifierId){this.modifierId=modifierId;}publicBooleangetOperateResult(){returnoperateResult;}publicvoidsetOperateResult(BooleanoperateResult){this.operateResult=operateResult;}}我只做了one-to-many单向映射,插入主题的时候,历史表会新增一条记录,并且外键是有的,如果变成双向关联,新增的时候历史表的外键为null,我很不解,反复尝试后,发现只有在one端加joinColumn历史表的外键才不为null。现在的问题是,我要做one端删除的时候,多方只是将外键update为null.求解啊
解决方案
解决方案二:
该回复于2011-03-11 11:17:35被版主删除
解决方案三:
晕啊。。。hibernateannotation实在弄的有点晕乎
解决方案四:
数据库的设计就有问题外键应该在many端设啊在多方配置many-to-one就可以了一个主题对应多个历史记录。在历史记录里面设个外键指向主题就可以了
解决方案五:
级联可以做到一方删除,外键变成null吗?这个是不是需要自己写代码来做............如果删除的是大量数据,那么还是直接用sql做吧...
解决方案六:
谢谢你的回答,我尝试过这样,不知道是不是你的意思历史表:@ManyToOne@JoinColumn(name="THEME_ID")privateThemeBeanthemeBean主题表@OneToMany(cascade=CascadeType.ALL,mappedBy="themeBean")privateSet<ThemeBeanHistory>themeBeanHistorySet;这样配在新增主题的时候,历史表里的外键为null
解决方案七:
我现在就是想删除一方的时候将多方也删除了,可以目前是将多方的外键置null,并不删除,郁闷啦...
解决方案八:
在多方把标签改为@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
解决方案九:
楼主把数据库的建表语句贴出来我怀疑数据库设计有问题
解决方案十:
7楼的正解,可是删除的时候是不是要先load一下,如果不load的话报错CouldnotexecuteJDBCbatchupdate;SQL[deletefromTHEMEwhereTHEME_ID=?];constraint[null];nestedexceptionisorg.hibernate.exception.ConstraintViolationException:CouldnotexecuteJDBCbatchupdate如何不load的情况下删除呢
解决方案十一:
8楼,我的数据库应该没问题,发给你看看吧CREATETABLETHEME_HISTORY(IDNUMBER(10)NOTNULL,THEME_IDNUMBER(10),STATUSVARCHAR2(10BYTE),OPERATION_DATETIMESTAMP(6),OPERATE_RESULTVARCHAR2(10BYTE),MODIFIER_IDNUMBER(10))TABLESPACEUSERSPCTUSED0PCTFREE10INITRANS1MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0BUFFER_POOLDEFAULT)LOGGINGNOCOMPRESSNOCACHENOPARALLELMONITORING;COMMENTONTABLETHEME_HISTORYIS'主题历史表';COMMENTONCOLUMNTHEME_HISTORY.IDIS'主题历史表ID';COMMENTONCOLUMNTHEME_HISTORY.THEME_IDIS'主题ID';COMMENTONCOLUMNTHEME_HISTORY.STATUSIS'状态';COMMENTONCOLUMNTHEME_HISTORY.OPERATION_DATEIS'操作时间';COMMENTONCOLUMNTHEME_HISTORY.OPERATE_RESULTIS'操作结果';COMMENTONCOLUMNTHEME_HISTORY.MODIFIER_IDIS'修改人ID';CREATEUNIQUEINDEXPK_THEME_HISTORYONTHEME_HISTORY(ID)LOGGINGTABLESPACEUSERSPCTFREE10INITRANS2MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0BUFFER_POOLDEFAULT)NOPARALLEL;ALTERTABLETHEME_HISTORYADD(CONSTRAINTPK_THEME_HISTORYPRIMARYKEY(ID)USINGINDEXTABLESPACEUSERSPCTFREE10INITRANS2MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0));ALTERTABLETHEME_HISTORYADD(CONSTRAINTFKF3E058F544BA70ADFOREIGNKEY(THEME_ID)REFERENCESTHEME(THEME_ID));
解决方案十二:
看主题表的不看历史表
解决方案十三:
主题表CREATETABLEMDM_THEME(THEME_IDNUMBER(10)NOTNULL,THEME_NAME_ENVARCHAR2(50BYTE),)TABLESPACEUSERSPCTUSED0PCTFREE10INITRANS1MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0BUFFER_POOLDEFAULT)LOGGINGNOCOMPRESSNOCACHENOPARALLELMONITORING;COMMENTONTABLEMDM_THEMEIS'主题表';COMMENTONCOLUMNMDM_THEME.THEME_IDIS'主题ID';COMMENTONCOLUMNMDM_THEME.SYSTEM_IDIS'系统ID';COMMENTONCOLUMNMDM_THEME.THEME_NAME_ENIS'英文名称';CREATEUNIQUEINDEXPK_THEMEONTHEME(THEME_ID)LOGGINGTABLESPACEUSERSPCTFREE10INITRANS2MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0BUFFER_POOLDEFAULT)NOPARALLEL;ALTERTABLETHEMEADD(CONSTRAINTPK_THEMEPRIMARYKEY(THEME_ID)USINGINDEXTABLESPACEUSERSPCTFREE10INITRANS2MAXTRANS255STORAGE(INITIAL64KMINEXTENTS1MAXEXTENTS2147483645PCTINCREASE0));
解决方案十四:
把主题表@OneToMany去掉你再删除看下
解决方案十五:
去掉怎么级联啊。。汗
解决方案:
不用从新load啊,你用的是什么技术连接的数据库。
解决方案:
历史表:配个ManyToOne主题表去掉OneToMany你操作试下撒
解决方案:
Oracle数据库用spring管理连接的<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/><propertyname="url"value="jdbc:oracle:thin:@192.168.1.2:1521:XE"/><propertyname="username"value="zc"/><propertyname="password"value="zhangchun"/></bean>
解决方案:
好强,你看着就头晕
解决方案:
用逻辑控制级联
解决方案:
sessionFactory和transaction是这样配的<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><propertyname="dataSource"><refbean="dataSource"/></property><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop><propkey="hibernate.show_sql">true</prop><propkey="hibernate.connection.autocommit">true</prop><propkey="hibernate.hbm2ddl.auto">update</prop></props></property><propertyname="packagesToScan"><list><value>com.aq.osgi.beans</value></list></property></bean><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean>
解决方案:
底层删除方法:protectedvoiddelete(Objectobject)throwsCurrentlyException{try{this.getHibernateTemplate().delete(object);}catch(HibernateExceptionhe){//记录错误日志,抛出已定义异常CurrentlyExceptionce=newCurrentlyException(ErrorCodeConstant.ERROR_1001,he);log.error(he.getMessage());throwce;}}
解决方案:
删除的时候还是报了CouldnotexecuteJDBCbatchupdate;SQL[deletefromTHEMEwhereTHEME_ID=?];constraint[null];nestedexceptionisorg.hibernate.exception.ConstraintViolationException:CouldnotexecuteJDBCbatchupdate而且我看了下hql,好像没有删历史表。直接去删主题表.
解决方案:
该回复于2011-03-11 16:21:59被版主删除
解决方案:
在映射文件里面写级联会不会更好点,楼主的配置,那叫一个晕啊!
解决方案:
想不清的话就不要建关系,直接sql吧。一样的效果。
解决方案:
该回复于2011-03-14 08:59:43被版主删除
解决方案:
你看看是不是控制反转的问题