我试图修复由我的数据库上的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 –