[英]Spring Boot - created_at cannot be null. Spring JPA
Whenever I am trying to save an object to my DB, I keep getting the error Column 'created_at' cannot be null.每当我尝试将对象保存到我的数据库时,我都会收到错误列“created_at”不能为空。
Here is my audit model:这是我的审计模型:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
value = {"createdAt", "updatedAt"},
allowGetters = true
)
public abstract class AuditModel implements Serializable {
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
@Column(name = "created_at", nullable = false, updatable = false)
@JsonIgnore
private Date createdAt;
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
@Column(name = "updated_at", nullable = false)
private Date updatedAt;
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
Here is an example of a model that extends it:这是扩展它的模型的示例:
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Category extends AuditModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Item Name is required.")
private String categoryName;
@NotNull
private String categoryDescription;
@Nullable
@JsonIgnore
@OneToMany(mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Item> items;
public Category(String categoryName, String categoryDescription) {
this.categoryName = categoryName;
this.categoryDescription = categoryDescription;
}
And here is the temporary CommandLine bean that is performing some tests for me:这是为我执行一些测试的临时命令行 bean:
@Configuration
public class ItemConfig {
@Autowired
ItemRepository itemRepository;
@Autowired
CategoryRepository categoryRepository;
@Value("${STORE.ENV}")
private String env;
@Bean
CommandLineRunner itemRunner(ItemRepository itemRepository) {
return args -> {
System.out.println("true");
Category cueCategory = new Category
("Cues",
"This category contains all items relating to billiard cues. This includes yada, yadada, and yada."
);
categoryRepository.save(cueCategory);
Item item = new Item("Test Cue", 700, cueCategory);
itemRepository.save(item);
};
}
}
At first, I was creating a blank object then setting all the values with setters.起初,我创建了一个空白对象,然后使用 setter 设置所有值。 I thought that maybe it all needed to happen in one fel-swoop for the created_at to register with a proper date, so I added some constructors.
我想,也许这一切都需要一蹴而就,使 created_at 注册到正确的日期,所以我添加了一些构造函数。 That still didn't work.
那还是不行。 Please let me know if you see anything glaring!
如果你看到任何明显的东西,请告诉我!
You should add @EnableJpaAuditing
annotation.您应该添加
@EnableJpaAuditing
注释。
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
// ...
@Configuration
@EnableJpaAuditing
public class ItemConfig {
// ...
}
You can fix this issue by modifying your createdAt and updatedAt properties like below and also, modify your getter and setters.你可以通过修改你的 createdAt 和 updatedAt 属性来解决这个问题,如下所示,也可以修改你的 getter 和 setter。
@CreationTimestamp
@Column(name = "created_at", updatable = false)
private Timestamp createdAt;
@UpdateTimestamp
@Column(name = "updated_at")
private Timestamp updatedAt;
You do explicitly set created_at
column to be non-nullable with the @Column
annotation, via nullable = false
:您确实使用
@Column
注释通过nullable = false
将created_at
列显式设置为不可为空:
@Column(name = "created_at", nullable = false, updatable = false)
private Date createdAt;
If you want it to accept null
values, just set it true
or just simply drop it, because nullable
is optional and the default value for it is true
.如果您希望它接受
null
值,只需将其设置为true
或只是简单地删除它,因为nullable
是可选的,并且它的默认值为true
。
@Column(name = "created_at", updatable = false)
private Date createdAt;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.