我試圖修復由我的數據庫上的Hibernate查詢導致的錯誤,但我似乎無法找到查詢來自哪裏。啓用sql
登錄休眠後,我發現錯誤在哪裏,但不知道如何解決。如何查找/更改休眠查詢
Hibernate query(eclipse log) 「update students_classes set student_id = null where student_id =?」
投擲: ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper](HTTP-本地主機 - 127.0.0.1-8080-2)柱 'student_id數據' 不能爲空
的錯誤是在這行拋出:
student = studentDAO.save(student);
其中保存來自
public Entity save(Entity entity) throws Exception {
Entity result = null;
try {
trimAllStrAttributes(entity);
result = em.merge(entity);
} catch (Exception e) {
logger.error("Exception in AbstractDAO", e);
throw e;
}
return result;
}
private void trimAllStrAttributes(Entity product) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
final Class c = product.getClass();
for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(c, Object.class).getPropertyDescriptors()) {
Method method = propertyDescriptor.getReadMethod();
if (method != null) {
String name = method.getName();
// If the current level of Property is of type String
if (method.getReturnType().equals(String.class)) {
String property = (String) method.invoke(product);
if (property != null) {
try {
Method setter = c.getMethod("set" + name.substring(3), new Class<?>[] { String.class });
if (setter != null) {
setter.invoke(product, property.trim());
}
} catch (NoSuchMethodException nsme) {
}
}
}
}
}
}
它可以是一個映射問題,所以這裏是我的恩tities:
StudentClasses
@Column(name = "student_id")
private Long studentId;
@Column(name = "classes_id")
private Long classesId;
學生
@AuditJoinTable
@ManyToMany
@JoinTable(name = "students_classes",
joinColumns = @JoinColumn(name = "student_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "classes_id", referencedColumnName = "id"))
private List<Classes> classes;
@NotAudited
@OneToMany
@JoinColumn(name = "student_id")
private List<StudentClasses> studentsClasses;
我應該怎麼辦?改變hibernate的查詢(在哪裏找到它?)還是在映射級別有問題?
您應該包括在問題的堆棧跟蹤 – meskobalazs
你在做一個'setStudentClasses(newClasses)'在你的代碼,更換託管集合?什麼是'trimAllStrAttbributes'在做什麼? –
stacktrace的相關部分已經在這裏@meskobalazs。我編輯了新的方法M. Deinum –