我正在使用Hibernate / JPA,并且在Hibernate类之间存在以下关系。
MediaRequestDetails< - >披露(一对一共享主键) 披露< - > DisclosureAuthor(一对多)
以下是课堂定义。
@Entity
@Table(name = "AW_REQUEST_DETAILS")
public class MediaRequestDetails implements WFPayload , Serializable{
@Id
@SequenceGenerator(name="REQUEST_ID_SEQ", sequenceName="REQUEST_ID_DBSEQ", allocationSize=1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REQUEST_ID_SEQ")
@Column(name = "ID", nullable = false)
Long requestId;//id of the request
@OneToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL,mappedBy = "mediaRequDetails")
DisclosureDetails disclosureDetails;
}
披露明细课程定义
@Entity
@Table(name="AW_DISC_DETAILS")
public class DisclosureDetails implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "REQUEST_ID")
Long requestId;
@Column(name="DISC_LANG")
String discLanguage;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,orphanRemoval=true)
@JoinColumn(name="REQUEST_ID", referencedColumnName="REQUEST_ID")
Set<DisclosureAuthor> reqDiscAuthorsSet;
@JoinColumn(name = "REQUEST_ID")
@OneToOne(optional=false)
@MapsId
private MediaRequestDetails mediaRequDetails;
//getter setter methods are here
@PrePersist
public void prePersistCallBack(){
if(reqDiscAuthorsSet!=null && reqDiscAuthorsSet.size() >0){
for(DisclosureAuthor discAuthor:reqDiscAuthorsSet){
DisclosureAuthorPK dpk=discAuthor.getPk();
dpk.setRequestId(mediaRequDetails.requestId);
}
}
}
@PreUpdate
public void preUpdateCallBack(){
if(reqDiscAuthorsSet!=null && reqDiscAuthorsSet.size() >0){
for(DisclosureAuthor discAuthor:reqDiscAuthorsSet){
DisclosureAuthorPK dpk=discAuthor.getPk();
dpk.setRequestId(mediaRequDetails.requestId);
}
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((requestId == null) ? 0 : requestId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DisclosureDetails other = (DisclosureDetails) obj;
if (requestId == null) {
if (other.requestId != null)
return false;
} else if (!requestId.equals(other.requestId))
return false;
return true;
}
}
披露作者类定义
@Entity
@Table(name="AW_DISC_AUTHOR")
public class DisclosureAuthor implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EmbeddedId
private DisclosureAuthorPK pk;
//getter and setter methods goes here
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pk == null) ? 0 : pk.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DisclosureAuthor other = (DisclosureAuthor) obj;
if (pk == null) {
if (other.pk != null)
return false;
} else if (!pk.equals(other.pk))
return false;
return true;
}
}
DisclosureAuthorPK
@Embeddable
public class DisclosureAuthorPK implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "REQUEST_ID", nullable = false)
private Long requestId;
@Column(name = "AUTHOR_ID", nullable = false)
private Long authorsId
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((authorsId == null) ? 0 : authorsId.hashCode());
result = prime * result
+ ((requestId == null) ? 0 : requestId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DisclosureAuthorPK other = (DisclosureAuthorPK) obj;
if (authorsId == null) {
if (other.authorsId != null)
return false;
} else if (!authorsId.equals(other.authorsId))
return false;
if (requestId == null) {
if (other.requestId != null)
return false;
} else if (!requestId.equals(other.requestId))
return false;
return true;
}
}
我正在尝试使用以下程序更新mediarequestDetails。 requestId 187已经有三个与数据库相关联的作者。 但是在下面的程序中,我只是在集合中添加了一个作者,其余的两个作者必须从数据库中删除,这是我的期望。
public class TestDisclosureSample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManager entityManager = Persistence.createEntityManagerFactory("rawsjpa").createEntityManager();
//prepare mediarequest object
MediaRequestDetails obj=new MediaRequestDetails();
obj.setRequestId(187L);
obj.setRequestType("MDA");
//prepare disclosure details
DisclosureDetails discDetails=new DisclosureDetails();
discDetails.setRequestId(187L);
discDetails.setDiscLanguage("English");
discDetails.setMediaRequDetails(obj);
obj.setDisclosureDetails(discDetails);
DisclosureAuthor dscauthor1=new DisclosureAuthor();
DisclosureAuthorPK pk1 = new DisclosureAuthorPK();
pk1.setAuthorsId(1237L);
pk1.setRequestId(187L);
dscauthor1.setPk(pk1);
//dscauthor1.setMediaRequDetails(obj);
discAuthorSet.add(dscauthor1);
Session session =(Session)entityManager.getDelegate();
if(entityManager!=null){
System.out.println("************* EntityManager is obtained for Updation*****************");
Transaction tx=session.beginTransaction();
session.saveOrUpdate(obj);
tx.commit();
}
session.close();
}
}
当我执行上述程序时,我得到以下输出,但有异常。
Hibernate: select disclosure_.AUTHOR_ID, disclosure_.REQUEST_ID from AW_DISC_AUTHOR disclosure_ where disclosure_.AUTHOR_ID=? and disclosure_.REQUEST_ID=?
Hibernate: select disclosure_.AUTHOR_ID, disclosure_.REQUEST_ID from AW_DISC_AUTHOR disclosure_ where disclosure_.AUTHOR_ID=? and disclosure_.REQUEST_ID=?
Hibernate: update AW_REQUEST_DETAILS set ANALYST_ATTESTATION=?, ANALYST_DERV_POS=?, ANALYST_DISCLOSURE=?, ANALYST_POS_DETAILS=?, CA_COMMENT=?, CORP_AFFAIRS_OWNER=?, CORP_AFFAIRS_REGION=?, REQ_CREATE_DATE=?, REQ_CREATED_BY=?, IS_EVENT_CAN_OR_POST=?, EVENT_DATETIME=?, EVENT_TIME_ZONE=?, EVENT_TYPE_ID=?, IS_ANALYST_ATTESTATION=?, IS_APPRVL_BY_CORP=?, IS_APPRVL_BY_LEGAL=?, IS_BYPASS_DISCL=?, LAST_MODIFIED_BY=?, LAST_MODIFIED_DATE=?, LEGAL_COMMENT=?, IS_LOCATION_USA=?, PRINTED_MTRL=?, PROCESS_INSTANCE_ID=?, RSN_EVENT_CAN_OR_POST=?, REQ_COMP_NOT_FOUND=?, REQUEST_STATUS=?, REQUEST_TYPE=?, REQUESTOR=?, REQUESTOR_REGION=?, REQUESTOR_TYPE=?, SUBMIT_COMMENT=?, TASKREADY_ID=?, TASKREADY_NAME=? where ID=?
Hibernate: update AW_DISC_DETAILS set DISC_LANG=? where REQUEST_ID=?
Hibernate: update AW_REQUEST_DETAILS_COMPANIES set REQUEST_ID=null where REQUEST_ID=?
Hibernate: update AW_REQUEST_DETAILS_COMPANIES set REQUEST_ID=null where REQUEST_ID=?
Hibernate: update AW_REQUEST_DETAILS_COUNTRIES set REQUEST_ID=null where REQUEST_ID=?
Hibernate: update AW_REQUEST_DETAILS_COUNTRIES set REQUEST_ID=null where REQUEST_ID=?
Hibernate: update AW_DISC_AUTHOR set REQUEST_ID=null where REQUEST_ID=?
Aug 14, 2015 5:06:45 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1407, SQLState: 72000
Aug 14, 2015 5:06:45 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01407: cannot update ("AW"."AW_DISC_AUTHOR"."REQUEST_ID") to NULL
Aug 14, 2015 5:06:45 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.dialect.Oracle8iDialect$2.convert(Oracle8iDialect.java:471)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:189)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:59)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1157)
at org.hibernate.action.internal.CollectionRemoveAction.execute(CollectionRemoveAction.java:111)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:395)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:387)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:307)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:349)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1159)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.cira.raws.mediawf.bean.TestDisclosureSample.saveMediaRequestDetailsWithDisc(TestDisclosureSample.java:64)
at com.cira.raws.mediawf.bean.TestDisclosureSample.main(TestDisclosureSample.java:46)
Caused by: java.sql.SQLException: ORA-01407: cannot update ("AW"."AW_DISC_AUTHOR"."REQUEST_ID") to NULL
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3694)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186)
... 14 more
对问题的长度抱歉,但我只提供了确定根本原因所需的详细信息任何人都可以告诉我在上面的映射中我做错了什么?