[英]Default value for an embedded field in Hibernate. java.sql.SQLException: Field 'url' doesn't have a default value
I have an abstract class A
, class B
and C
are subclasses of A
.我有一个抽象类
A
,类B
和C
是A
子类。 Class B
has an embedded field d
. B
类有一个嵌入字段d
。 Now when I try to save an instance of B
everything is fine.现在,当我尝试保存
B
的实例时,一切都很好。 However on creating an instance of C
and saving it, I get an error :但是,在创建
C
实例并保存它时,出现错误:
java.sql.SQLException: Field 'string' doesn't have a default value.
Eg classes:例如类:
public abstract class A {
}
@Embeddable
public class D {
private String string;
}
public class B extends A{
@Embedded
@NotNull
private D d;
}
public class C extends A {
}
The error you got is java.sql.Exception
telling you that:你得到的错误是
java.sql.Exception
告诉你:
`Field 'string' doesn't have a default value.`
From the wording I assume you are using MySQL, and what the error means is, that your string
field was not populated at insertion time and declared as not null.从措辞上我假设您使用的是 MySQL,并且错误的含义是,您的
string
字段在插入时未填充并声明为非空。 You would receive the exact same error if you were to insert a record into that table and leave the string
column empty.如果您将记录插入该表并将
string
列留空,您将收到完全相同的错误。
What is causing your problem in the end is that you have the same table used for both classes, the string
column added for the sake of object b
and not populated in case of using c
.最终导致您出现问题的原因是您为两个类使用了相同的表,为对象
b
添加的string
列在使用c
情况下未填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.