我有两个表,“病人”和“约会”。 一个病人可以有很多约会。当我要从“患者”表中删除患者时,冬眠然后说“约会”表不存在。 任何帮助,将不胜感激。
设置患者类别中的关系
targetEntity = Appointment.class,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Appointment> appointmentset = new HashSet<Appointment>();
患者类别中的许多关系
@ManyToOne
@JoinColumn(name = "Patient_Id")
private Patient patient;
删除患者方法
public void deletePatient2(Integer id){
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
TypedQuery query = session.getNamedQuery("deletePatient");
query.setParameter("id", id);
query.executeUpdate();
}catch (HibernateException e){
e.printStackTrace();
if (tx != null) tx.rollback();
}
session.flush();
tx.commit();
}
删除患者命名查询
@NamedQuery(
name = "deletePatient",
query = "delete from Patient p where p.id = :id"
)
May 05, 2019 10:13:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
May 05, 2019 10:13:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table 'KYMb02vi2Z.Patient_Appointment' doesn't exist
org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
at org.hibernate.hql.internal.ast.exec.BasicExecutor.doExecute(BasicExecutor.java:100)
at org.hibernate.hql.internal.ast.exec.DeleteExecutor.execute(DeleteExecutor.java:105)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:454)
at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:382)
at org.hibernate.internal.SessionImpl.executeUpdate(SessionImpl.java:1562)
at org.hibernate.query.internal.AbstractProducedQuery.doExecuteUpdate(AbstractProducedQuery.java:1641)
at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1618)
at SessionManager.deletePatient2(SessionManager.java:1393)
堆栈错误,约会类中的表被设置为约会
@Entity
@Table(name = "Appointment")
public class Appointment
但是由于某种原因,即使我重命名该表,它仍会查找错误表,只是在开始时添加另一个Patient_,所以它仍在查找表Patient_Appointment。
在患者类别中重新映射了一对多
@OneToMany(
targetEntity = Appointment.class,
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Appointment> appointments = new ArrayList<Appointment>();
public void addAppointment(Appointment appointment){
appointments.add(appointment);
appointment.setPatient(this);
}
public void removeAppointment(Appointment appointment){
appointments.remove(appointment);
appointment.setPatient(null);
}
解决方案
@OneToMany(
mappedBy = "patient",
targetEntity = Appointment.class,
cascade = CascadeType.ALL,
orphanRemoval = true
)
添加了mappingBy