gpt4 book ai didi

java - Hibernate 返回 util.Date as long

转载 作者:行者123 更新时间:2023-12-01 10:29:28 24 4
gpt4 key购买 nike

我有日期为 util.Date 的域类。当我运行 jpa 方法时,从数据库中查找行。它返回长日期而不是实际日期。有人可以帮忙吗?

编辑:

域名:

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Formula;

@Entity
public class Expense {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
int intTranId;

BigDecimal fltAmt;
Date dtDate;
....
/setter getter methods

存储库:

import java.util.Date;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.ext.domain.Expense;
@Repository
public interface ExpenseRepository extends CrudRepository<Expense, Integer>{
@Query("SELECT min(dtDate) as dtDate ,Max(dtDate) as dtDate FROM Expense")
Iterable<Date> getAvlRptDates();

@Query("SELECT monthYear FROM Expense group by Year(dtDate),Month(dtDate) order by Year(dtDate),Month(dtDate)")
Iterable<String> getAvlRptRecords();


Iterable<Expense> findAll();
}

findAll 返回的值为

[{"intTranId":1064,"fltAmt":2.00,"dtDate":1454481056294,"monthYear":"2016-1","category":{"intCatId":5,"strCatName":"Grocery"}}]

最佳答案

@Column(name = "DatabaseColumnName")
@DateTimeFormat(pattern = "YYYY-MM-DD")
@Temporal(TemporalType.DATE)
private Date dtDate;

请使用 DateTimeFormat 将数据库日期与费用类实体映射

关于java - Hibernate 返回 util.Date as long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com