Java 如何查找/更改hibernate查询
我正试图修复由数据库上的Hibernate查询引起的错误,但我似乎找不到查询来自何处。启用Java 如何查找/更改hibernate查询,java,mysql,spring,hibernate,jpa,Java,Mysql,Spring,Hibernate,Jpa,我正试图修复由数据库上的Hibernate查询引起的错误,但我似乎找不到查询来自何处。启用sql登录hibernate后,我找到了错误所在,但不知道如何修复 Hibernate查询(eclipse日志) “更新学生\u类设置学生\u id=null,其中学生\u id=?” 投掷: 错误[org.hibernate.engine.jdbc.spi.SqlExceptionHelper](http-localhost-127.0.0.1-8080-2)列“学生id”不能为空 将在此行引发错误: s
sql
登录hibernate后,我找到了错误所在,但不知道如何修复
Hibernate查询(eclipse日志)
“更新学生\u类设置学生\u id=null,其中学生\u id=?”
投掷:
错误[org.hibernate.engine.jdbc.spi.SqlExceptionHelper](http-localhost-127.0.0.1-8080-2)列“学生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) {
}
}
}
}
}
}
学生
@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;
@AuditJoinTable
@许多
@JoinTable(name=“students\u classes”,
joinColumns=@JoinColumn(name=“student\u id”,referencedColumnName=“id”),
inverseJoinColumns=@JoinColumn(name=“classes\u id”,referencedColumnName=“id”))
私人名单类;
@未经审核
@独身癖
@JoinColumn(name=“学生id”)
私人名单学生课程;
我该怎么办?更改hibernate的查询(在哪里找到它?)或者在映射级别是否存在问题?错误解释如下:
抛出:错误[org.hibernate.engine.jdbc.spi.SqlExceptionHelper](http-localhost-127.0.0.1-8080-2)列“学生id”不能为空
根据您的其他映射,我可以肯定studentId是表的主键,因此,如果您不知道新实体必须具有的id,则必须标记该字段以使hibernate映射它:
将studentId标记为id
,并添加自动生成的值:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long studentId;
Hibernate定义了五种类型的标识符生成策略:
- 自动-标识列、序列或表格取决于基础数据库
- 表格-保存id的表格
- 标识-标识栏
- 序列序列
- 身份复制–该身份是从另一个实体复制的
- 我的第一个猜测是,在这一行中
result = em.merge(entity);
实体
对象没有导致异常的setstudentId属性。因此,您需要自行设置或自动生成,正如另一个答案所示。问题在于StudentId主键的序列创建配置。你必须在这里做两件事
在数据库中创建一个序列,如下所示
CREATE SEQUENCE STUDENT_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
在hibernate配置中指定序列名称
@Id
@SequenceGenerator(name = "StudentSeq", sequenceName="STUDENT_SEQ", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "StudentSeq")
@Column(name = "Student_id", nullable = false)
private Long StudentId;
您应该在问题中包含stacktrace您是否正在代码中执行setStudentClasses(newClasses)
替换托管集合?TrimallStrattBytes
在做什么?stacktrace的相关部分已经在这里@meskobalazs。我已经编辑了新方法M.Deinum的帖子
@Id
@SequenceGenerator(name = "StudentSeq", sequenceName="STUDENT_SEQ", allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "StudentSeq")
@Column(name = "Student_id", nullable = false)
private Long StudentId;