SSH annotation is missing when the database is inserted in time

Source: Internet
Author: User
When a CS structure application is created, a time string is uploaded from the client to the server, and the time is lost in seconds. The client is developed for Android, and the server side is annotated with SSH. So I found various answers in Baidu. The first one is to add the @ Temporal (TemporalType. TIMESTAMP) Annotation on the get method of the object class, and then add it decisively.

When a CS structure application is created, a time string is uploaded from the client to the server, and the time is lost in seconds. The client is developed for Android, and the server side is annotated with SSH. So I found various answers in Baidu. The first one is to add the @ Temporal (TemporalType. TIMESTAMP) Annotation on the get method of the object class, and then add it decisively.

When a CS structure application is created, a time string is uploaded from the client to the server, and the time is lost in seconds. The client is developed for Android, and the server side is annotated with SSH.

So I found various answers in Baidu. The first one is to add @ Temporal (TemporalType. TIMESTAMP) annotation, decisively added, and the results reported an error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.hpsvse.traffic.entity.Income.incomeDate

The error occurs because @ Temporal only supports java. util. Date or java. util. Calendar parameters. Then, the type of the returned value is java. SQL. Timestamp, Which is changed to java. util. Date. The result is that although no error is reported, the data is still not inserted to the database in minutes and seconds.

..................

After a long tangle, I suddenly saw a bright spot and found that the length was not long enough or the date format I passed in was incorrect, because my prefix was (the second was missing ), the following two possibilities have been attempted:

The first one is to change the value of length to 50, and it does not have any effect, as shown below:

@Temporal(TemporalType.TIMESTAMP)@Column(name = "income_date", length = 50)
Then, try the second one: 

After adding two zeros to the end of the time string 10:22:00, the request server is converted to the standard yyyy-MM-dd HH-mm-ss format. The result is a miracle. To sum up, it was a farce in the past, because we made up less than two zeros after the time.


FinallySummaryIt is actually easy to insert data into the database:

The first method defines a java. util. date type, and then add @ Temporal (TemporalType. TIMESTAMP) annotation, and then change the length of the attribute length to a little longer, it feels that 19 is enough. The Code is as follows:

private Date incomeDate;@Temporal(TemporalType.TIMESTAMP)@Column(name = "income_date", length = 19)public Date getIncomeDate() {return this.incomeDate;}public void setIncomeDate(Date incomeDate) {this.incomeDate = incomeDate;}
The second method directly defines a java. SQL. Timestamp type, as follows:
private Timestamp incomeDate;@Column(name = "income_date", length = 19)public Timestamp getIncomeDate() {return this.incomeDate;}public void setIncomeDate(Timestamp incomeDate) {this.incomeDate = incomeDate;}
Note that the input value must be in the standard format of yyyy-MM-dd HH-mm-ss (for example, 10:22:00, otherwise, the time, minute, and second will still be lost ~

Here, I would like to thank another cool-man for his blog guidance. Http://blog.sina.com.cn/s/blog_82a09f100101a76j.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.