Java 在Spring、Hibernate和PostgreSQL中插入时在列中生成时间戳
我正在学习Spring、Hibernate和PostgreSQL。我有一个具有时间戳字段的实体。此时间戳的数据库列称为Java 在Spring、Hibernate和PostgreSQL中插入时在列中生成时间戳,java,spring,postgresql,hibernate,auto-generate,Java,Spring,Postgresql,Hibernate,Auto Generate,我正在学习Spring、Hibernate和PostgreSQL。我有一个具有时间戳字段的实体。此时间戳的数据库列称为datecreated 此时间戳应自动生成,但不会自动生成。插入记录时,只生成空值,而不是日期 我尝试了互联网上多种解决方案的组合,但没有成功。我试过这个: 这就是我得到的: 如何让它生成时间戳? 这是一个项目文件Image.java模型、pom.xml、import.sql ddl、hibernate.cfg.xml文件: java pom.xml h
datecreated
此时间戳应自动生成,但不会自动生成。插入记录时,只生成空值,而不是日期
我尝试了互联网上多种解决方案的组合,但没有成功。我试过这个:
这就是我得到的: 如何让它生成时间戳? 这是一个项目文件Image.java模型、pom.xml、import.sql ddl、hibernate.cfg.xml文件: java
pom.xml
hibernate.cfg.xml
班级,哈佛商学院
真的
jdbc:postgresql://localhost:5432/postgres
org.postgresql.Driver
用户32
pa$$字
org.hibernate.dialogue.postgresql95dialogue
1.
创造
真的
真的
5.
20
2.
1800
150
如果您使用JPA或本机hibernate方法从应用程序中进行插入,prePersist和其他@createTimeStamp钩子可以工作。如果您只是从应用程序上下文之外运行insert,那么hibernate就不可能知道正在运行什么DML,因此永远不会调用挂钩。如果您希望在运行insert时从外部执行该操作,那么create的时间戳应该自动填充,您需要添加表级DDL指令。
例如:altertable mytable ALTER COLUMN datecreated SET DEFAULT now()代码>
现在,关于hibernate的自动DDL功能,xml属性create
从实体定义而不是DML生成DDL。因此,您还可以将默认时间戳值嵌入相关实体的列定义中,如下所示:
@Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
这将确保在创建表时,使用hibernate设置的auto DDL属性应用定义
希望这能澄清,如果您使用JPA或本机hibernate方法在应用程序中进行插入,prePersist和其他@createTimeStamp钩子可以工作。如果您只是从应用程序上下文之外运行insert,那么hibernate就不可能知道正在运行什么DML,因此永远不会调用挂钩。如果您希望在运行insert时从外部执行该操作,那么create的时间戳应该自动填充,您需要添加表级DDL指令。
例如:altertable mytable ALTER COLUMN datecreated SET DEFAULT now()代码>
现在,关于hibernate的自动DDL功能,xml属性create
从实体定义而不是DML生成DDL。因此,您还可以将默认时间戳值嵌入相关实体的列定义中,如下所示:
@Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
这将确保在创建表时,使用hibernate设置的auto DDL属性应用定义
希望这能澄清您的意思是什么时候import.sql applicated dateCreated值不在表中?您是如何获得import.sql文件的?当您在管理数据库交互(通常是DAO)的类中调用persiste()或save()时,日志中生成的就是这个吗?@code\uyes@EdBighands此文件是项目的一部分。该项目基于教程。该文件在教程中是这样描述的:“3.填充数据库表定义实体后,可以将import.sql放在项目结构的resources文件夹下,以便Hibernate可以在表中填充sql语句。”您的意思是当import.sql applied dateCreated值不在表中时?您是如何获得import.sql文件的?当您在管理数据库交互(通常是DAO)的类中调用persiste()或save()时,日志中生成的就是这个吗?@code\uyes@EdBighands此文件是项目的一部分。该项目基于教程。该文件在教程中是这样描述的:“3.填充数据库表定义实体后,可以将import.sql放在项目结构的resources文件夹下,以便Hibernate可以在表中填充sql语句。”
@CreationTimestamp
@Column
private Date dateCreated = new Date();
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date dateCreated = new Date();
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date dateCreated = new Date();
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date dateCreated = new Date();
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date dateCreated = new Date();
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date dateCreated;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date dateCreated;
@PrePersist
protected void onCreate() {
dateCreated = new Date();
}
package com.howtodoinjava.demo.spring.domain;
import javax.persistence.*;
import java.util.Date;
import org.hibernate.annotations.CreationTimestamp;
@Entity
@Table(name = "images")
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 300)
private String title;
@Column(nullable = false)
private String url;
@Column(nullable = false)
private String author;
@CreationTimestamp
@Column
private Date dateCreated = new Date();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return dateCreated;
}
public void setDate(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Image() {}
public Image(Long id, String title, String url, String author) {
this.id = id;
this.title = title;
this.url = url;
this.author = author;
}
@Override
public String toString() {
return "Post{" +
"id=" + id +
", title='" + title + '\'' +
", url='" + url + '\'' +
", author=" + author +
", date=" + dateCreated +
'}';
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bitMiners</groupId>
<artifactId>pdf-app</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspect.version>1.9.2</aspect.version>
<jackson.version>2.9.8</jackson.version>
<hibernate.version>5.4.2.Final</hibernate.version>
<hibernate.validator.version>6.0.13.Final</hibernate.validator.version>
<c3p0.version>0.9.5.2</c3p0.version>
</properties>
<dependencies>
<!-- servlets and jps -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.8</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>jboss</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate-C3P0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- for rest services -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- For Aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<!-- To Send Email -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--logging-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<!-- PostgreSQL -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- To use the plugin goals in your POM or parent POM -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-- ...
INSERT INTO images (title, url, author) VALUES ('lanszaft', 'https://i.imgur.com/sZ64fVI.jpg', 'Oskar Kamiński');
INSERT INTO images (title, url, author) VALUES ('kobieta w kapeluszu', 'https://i.imgur.com/spMsvHe.png', 'Pablo Picasso');
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.archive.autodetection">class,hbm</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/postgres</property> <!-- BD Mane -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">user32</property>
<property name="hibernate.connection.password">pa$$word</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<property name="hibernate.connection.pool.size">1</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<property name="hibernate.jdbc.lob.non_contextual_creation">true</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">150</property>
<mapping class="com.howtodoinjava.demo.spring.domain.User"/>
<mapping class="com.howtodoinjava.demo.spring.domain.Authority"/>
<mapping class="com.howtodoinjava.demo.spring.domain.AuthorityType"/>
<mapping class="com.howtodoinjava.demo.spring.domain.Image"/>
</session-factory>
</hibernate-configuration>
@Column(name="datecreated", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")