Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Prepod prepod = (Prepod) session.get(Prepod.class, 1l);
Student student = (Student) session.get(Student.class, 2l);
session.delete(student);
session.getTransaction().commit();
数据库图:
数据库状态:
中表:
学生:
前置:
代码执行后,我看到:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
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.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3343)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3546)
at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:100)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:364)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:356)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:282)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
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 logic.Main.main(Main.java:63)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Конфликт инструкции DELETE с ограничением REFERENCE "FK_63a66b22ace64374a874912be64". Конфликт произошел в базе данных "ForHiberTest", таблица "dbo.prepod_Student", column 'students_id'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
... 14 more
这意味着约束不允许删除学生,而中间表中存在关于该学生的真实情况。
你能帮我配置级联吗?如果我删除学生,我想从中间表中删除有关该学生的所有条目。prepod - 相同的行为。
我的映射:
@Entity
@Table(name = "prepod")
public class Prepod {
private Long id;
private String name;
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
List<Student> students = new ArrayList<Student>();
@ManyToMany(fetch=FetchType.EAGER)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long i) {
id = i;
}
}
@Entity
@Table(name = "Student")
public class Student {
private Long id;
private String name;
private Long age;
private List<Prepod> prepods = new ArrayList<Prepod>();
@ManyToMany(mappedBy = "students",fetch=FetchType.EAGER)
public List<Prepod> getPrepods() {
return prepods;
}
public void setPrepods(List<Prepod> prepods) {
this.prepods = prepods;
}
public Student() {
name = null;
}
public Student(Student s) {
name = s.getName();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Long getId() {
return id;
}
@Column(name = "name")
public String getName() {
return name;
}
@Column(name = "age")
public Long getAge() {
return age;
}
public void setId(Long i) {
id = i;
}
public void setName(String s) {
name = s;
}
public void setAge(Long age) {
this.age = age;
}
}
更新
对于 Masud CSE CUET 答案:
根据您的建议,我看到了这个日志:
Hibernate: select prepod0_.id as id1_1_1_, prepod0_.name as name2_1_1_, students1_.prepods_id as prepods1_1_3_, student2_.id as students2_2_3_, student2_.id as id1_0_0_, student2_.age as age2_0_0_, student2_.name as name3_0_0_ from prepod prepod0_ left outer join prepod_Student students1_ on prepod0_.id=students1_.prepods_id left outer join Student student2_ on students1_.students_id=student2_.id where prepod0_.id=?
Hibernate: select prepods0_.students_id as students2_0_1_, prepods0_.prepods_id as prepods1_2_1_, prepod1_.id as id1_1_0_, prepod1_.name as name2_1_0_ from prepod_Student prepods0_ inner join prepod prepod1_ on prepods0_.prepods_id=prepod1_.id where prepods0_.students_id=?
Hibernate: select students0_.prepods_id as prepods1_1_1_, students0_.students_id as students2_2_1_, student1_.id as id1_0_0_, student1_.age as age2_0_0_, student1_.name as name3_0_0_ from prepod_Student students0_ inner join Student student1_ on students0_.students_id=student1_.id where students0_.prepods_id=?
Hibernate: select students0_.prepods_id as prepods1_1_1_, students0_.students_id as students2_2_1_, student1_.id as id1_0_0_, student1_.age as age2_0_0_, student1_.name as name3_0_0_ from prepod_Student students0_ inner join Student student1_ on students0_.students_id=student1_.id where students0_.prepods_id=?
Hibernate: select prepods0_.students_id as students2_0_1_, prepods0_.prepods_id as prepods1_2_1_, prepod1_.id as id1_1_0_, prepod1_.name as name2_1_0_ from prepod_Student prepods0_ inner join prepod prepod1_ on prepods0_.prepods_id=prepod1_.id where prepods0_.students_id=?
Hibernate: select prepods0_.students_id as students2_0_1_, prepods0_.prepods_id as prepods1_2_1_, prepod1_.id as id1_1_0_, prepod1_.name as name2_1_0_ from prepod_Student prepods0_ inner join prepod prepod1_ on prepods0_.prepods_id=prepod1_.id where prepods0_.students_id=?
Hibernate: delete from prepod_Student where prepods_id=?
Hibernate: delete from prepod_Student where prepods_id=?
Hibernate: delete from prepod_Student where prepods_id=?
Hibernate: delete from prepod where id=?
Hibernate: delete from prepod where id=?
Hibernate: delete from prepod where id=?
Hibernate: delete from Student where id=?
我想要没有它:
Hibernate: delete from prepod where id=?
Hibernate: delete from prepod where id=?
Hibernate: delete from prepod where id=?