我正在研究使用Spring Data的项目.我想creationTime使用@CreatedDate注释来填充字段,而不是使用方法@PreUpdate或@PrePersist注释(以这种方式完美地工作).当我这样做时,@CreatedDate只需将此字段留空.我使用postgresql数据库.文档不是很有帮助.
你知道我该怎么办呢?谢谢!
import org.springframework.data.annotation.CreatedDate;
@Entity
@Table(name = "software")
public class Software implements Serializable {
// ...
@Column(name = "creation_time")
@CreatedDate
private Date creationTime;
//...
}
Run Code Online (Sandbox Code Playgroud)
我的applicationContext:
<jpa:repositories base-package="path.to.dao"/>
<context:component-scan base-package="path.to.dao"/>
<context:property-placeholder location="classpath:application.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="path.to.bean"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop> …Run Code Online (Sandbox Code Playgroud) 我目前有一个实体,如下所示:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long productId;
private String productImage;
private String productTitle;
private String productDescription;
private Integer productPrice;
private Date createdAt;
private Date updatedAt;
Run Code Online (Sandbox Code Playgroud)
创建此对象后,createdAt和updatedAt的值在数据库中显示为null,并且想知道如何实现代码以便自动插入createdAt和updateAt?
我的发布方法如下:
@PostMapping("/products")
public ProductResponse createProduct(@Validated @RequestBody ProductForm productForm) {
Product product = productForm.asProduct();
Product createdProduct = productRepository.save(product);
return new ProductResponse(createdProduct, "Product created");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行以下简单示例,但出现错误。
我正在使用@CreatedDate。但是,当我使用Rest端点保存Student对象时,它不会创建日期并抛出给定的异常。
在我的休息终点,我有
@PostMapping("/students")
public Student createStudent(@Valid @RequestBody Student student){
return studentRepository.save(student);
}
Run Code Online (Sandbox Code Playgroud)
这是我的实体...
@Entity
@Table(name = "students")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, allowGetters = true)
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotBlank
private String name;
@NotBlank
private String surname;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
Run Code Online (Sandbox Code Playgroud)
错误:
2018-06-20 21:43:40.821 DEBUG 8844 --- [nio-8080-exec-1] org.hibernate.SQL
: select next_val as id_val from …Run Code Online (Sandbox Code Playgroud) 我在基于 maven 的项目中使用 Spring Boot、JUnit 4 和 Mockito 来测试我的 Spring Boot 微服务 REST API。
因此,在启动时,DataInserter 类从 owner.json 和 cars.json 加载数据。
通常,通过我的 REST 调用,一切正常,但似乎我在设置单元测试和集成测试方面有问题。
项目结构:
myapi
?
??? pom.xml
?
??? src
??? main
? ?
? ??? java
? ? ?
? ? ??? com
? ? ?
? ? ??? myapi
? ? ?
? ? ??? MyApplication.java
? ? ?
? ? ??? bootstrap
? ? ? ?
? ? ? ??? DataInserter.java
? ? ?
? ? …Run Code Online (Sandbox Code Playgroud) java ×4
spring-boot ×3
hibernate ×2
spring ×2
spring-mvc ×2
jpa ×1
junit ×1
mockito ×1
postgresql ×1