私は休止状態にしていないので、休止状態4を使用しています。次のコードには、アノテーションを使用した1対1の関係の簡単な例が含まれています。親オブジェクトを削除しようとすると、外部キー制約の例外がスローされます。私は固執してこれを達成する方法を混乱させる。私はカスケード型をすべて使用していますが間違いを修正する休止状態で1対1の関係を使用して削除
これは、データベースに挿入して更新するために使用される親クラスです。
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="address")
public class Address {
@Id
@Column(name="address_id")
private long addressId=0;
@Column(name="place",length=30)
private String place=null;
@OneToOne(targetEntity=Student.class,fetch=FetchType.EAGER , cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="stud_id",referencedColumnName="student_id")
private Student parent;
public long getAddressId() {
return addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public Student getParent() {
return parent;
}
public void setParent(Student parent) {
this.parent = parent;
}
}
次に、オブジェクトを削除する方法を示します。しかし、私は例外を取得しています。どのように親オブジェクトに関連付けられている子レコードを削除する?
public void deleteStudentDao(long studentId) throws Exception{
Session session=null;
try{
if(studentId >0 && factory!=null
&& !factory.isClosed()){
session=factory.openSession();
session.beginTransaction();
Student student=new Student();
student.setStudentId(studentId);
Address address=new Address();
address.setParent(student);
session.delete(address);
session.getTransaction().commit();
System.out.println("The Student "+studentId+" detail deleted");
}
}catch(HibernateException e){
session.getTransaction().rollback();
throw e;
}finally{
if(session!=null){
session.close();
}
}
}
例外は、あなたがそのようなアドレスdelete
いけない
ERROR: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`address`, CONSTRAINT `FK_4ok3rcn3mnfl5ma2wuwvkluuo` FOREIGN KEY (`stud_id`) REFERENCES `students` (`student_id`))
Mar 10, 2017 10:50:34 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.mv.dao.StudentDetailDAO.deleteStudentDao(StudentDetailDAO.java:98)
at com.mv.studentutil.StudentRelationalUtil.deleteStudentUtil(StudentRelationalUtil.java:56)
at com.mv.studentmodifier.StudentModifier.main(StudentModifier.java:54)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`address`, CONSTRAINT `FK_4ok3rcn3mnfl5ma2wuwvkluuo` FOREIGN KEY (`stud_id`) REFERENCES `students` (`student_id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1049)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 15 more
http://stackoverflow.com/questions/19678797/how-to-update-and-delete-a-record-in-hibernate-one:ここ
はあなたが試すことができますコードです1対1マッピングは、この回答を通じて – Murad
例外のスタックトレースを投稿 – Chaitanya
ありがとうchiatanya。それは動作し、あなたは私の日を救った –