0

I want to store articles in my table where I want the published date to be generated automatically and stored in the database. The column publishedDate in my article table shows NULL whenever I add the article. I want the date to be automatically inserted.How can I do so? I have the following code in my entity class and JSP file

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="java.util.Date" %>
<jsp:useBean id="now" class="java.util.Date" />

<c:url value="/admin/article/save" var="url"></c:url>
            <form:form class="form-horizontal" action="${url}" modelAttribute="article"
                method="POST" enctype="multipart/form-data" style="margin-top:5rem;" >
                <fieldset>

                <form:hidden path="articleViews" value="${article.articleViews}" />
                <form:hidden path="publishedDate" id="publishedDate" value="${now}" />
                <form:hidden path="articleId" value="${article.articleId}" />
import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.springframework.web.multipart.MultipartFile;

@Entity
@Table
public class Article implements Serializable,Comparator<Article>{


    /**
     * 
     */
    private static final long serialVersionUID = -4756552789390263788L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long articleId;
    private String articleHeading;
    @Column(columnDefinition = "TEXT")

    private String articleIntroduction;
    @Column(columnDefinition = "TEXT")

    private String articleDescription;
    @Column(columnDefinition = "TEXT")

    private String articleSummary;
    @Transient
    private MultipartFile aImage;

   private String articleCategory;

   @Generated(GenerationTime.ALWAYS)
  @Column(name="publishedDate" ,columnDefinition = "TIMESTAMP")
     private Date publishedDate ;


    public long getArticleViews() {
        return articleViews;
    }
    public void setArticleViews(long articleViews) {
        this.articleViews = articleViews;
    }

        @Column( nullable = false, columnDefinition = "bigint(20) default 0")
   private long articleViews = 0;
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56

1 Answers1

0

Duplication of this post, use @PrePersist and @PreUpdate
Or also combine @CreationTimestamp and @UpdateTimestamp

IQbrod
  • 2,060
  • 1
  • 6
  • 28