这就是情况
@Transient
private Map<String, String> choices = null;
@Column(name = "choices", nullable = false)
public String getChoices() {
return gson.toJson(choices);
}当插入记录时,上面写着
java.sql.SQLException:字段“选择”没有默认值
我的调试SQL
DEBUG SQL:109 -
insert
into
question
(description, id)
values
(?, ?)此处,选择字段被忽略。
数据库详细信息:
CREATE TABLE `question` (<br/>
`id` varchar(50) COLLATE utf8_bin NOT NULL,<br/>
`description` varchar(1000) COLLATE utf8_bin NOT NULL,<br/>
`choices` text COLLATE utf8_bin NOT NULL,<br/>
`answers` varchar(100) COLLATE utf8_bin NOT NULL<br/>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br/>
<br/>
ALTER TABLE `question`<br/>
ADD PRIMARY KEY (`id`);<br/>有人能帮我解释一下为什么这样的字段会被忽略吗?谢谢。
更新实体类:
@Entity
@Table(name = "question")
public class Question {
@Id
private String id = null;
@Column(name = "description", nullable = false)
private String description = null;
@Transient
@Column(name = "choices", nullable = false)
private Map<String, String> choices = null;
public String getChoices() {
return gson.toJson(choices);
}
@Transient
private Set<String> answers = null;
@Transient
private Gson gson = new GsonBuilder().create();
public Question() {
this.id = UUID.randomUUID().toString();
}
...
}//End of class发布于 2016-01-12 13:24:50
在create中,您已经标记了选项列null。choices text COLLATE utf8_bin NOT NULL,因此成为例外。
当您持久化问题实体类时,您没有为选择提供值,因此它们不包含在结果sql中。要么为选择提供值,要么在数据库中的列上删除null约束。
在JPA中注释属性javax.persistence.Transient将其标记为非持久性,并且不会包含在结果的SQL中。
https://stackoverflow.com/questions/34735271
复制相似问题