【问题标题】:Calendar to Date mapping日历到日期映射
【发布时间】:2011-01-05 15:20:21
【问题描述】:

我遇到了休眠问题。我尝试将日历对象映射到我在表结构中使用的日期。

在 Hibernate 的参考指南中,您可以找到有关类型映射的信息,您会在其中找到类似“calendar_date”的内容。

<property generated="never" lazy="false" name="dateB" type="calendar_date" />

但是一个简单的等号不起作用(没有结果,但是 phpMyAdmin 给了我相同的查询结果):

List<Maturity> result = session.createCriteria(Maturity.class).add(Restrictions.eq("dateB", date0)).list();

这是成熟度类:

package datahandler.objects;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Maturity {

 final int DAY = 0;
 final int MONTH = 1;
 final int YEAR = 2;
 final int HOUR = 3;
 final int MIN = 4;

 @Id
 @GeneratedValue
 private int id;
 @Temporal(TemporalType.DATE)
 private Calendar dateB = Calendar.getInstance();
 private Calendar dateE = Calendar.getInstance();
 private Calendar timeB = Calendar.getInstance();
 private Calendar timeE = Calendar.getInstance();
 private String name;
 private String description;

 public Maturity() {
  // this.id = 0; // Sicherheitseintrag für leeren Termin-Datensatz!!!
 }

 public Maturity(int id, Calendar dateB, Calendar dateE, String name,
   String description) {
  this.id = id;
  this.timeB = dateB;
  this.dateB = dateB;
  this.timeE = dateE;
  this.dateE = dateE;
  this.name = name;
  this.description = description;
 }

 public Maturity(Calendar dateB, Calendar dateE, String name,
   String description) {
  this.timeB = dateB;
  this.dateB = dateB;
  this.timeE = dateE;
  this.dateE = dateE;
  this.name = name;
  this.description = description;
 }

 public Maturity(int id, int dayB, int monB, int yearB, int hourB, int minB,
   int dayE, int monE, int yearE, int hourE, int minE, String name,
   String description) {
  this.id = id;
  this.timeB.set(0, 0, 0, hourB, minB, 0);
  this.dateB.set(yearB, monB - 1, dayB);
  this.timeE.set(0, 0, 0, hourE, minE, 0);
  this.dateE.set(yearE, monE - 1, dayE);
  this.name = name;
  this.description = description;
 }

 public Maturity(int dayB, int monB, int yearB, int hourB, int minB,
   int dayE, int monE, int yearE, int hourE, int minE, String name,
   String description) {
  this.timeB.set(0, 0, 0, hourB, minB, 0);
  this.dateB.set(yearB, monB - 1, dayB);
  this.timeE.set(0, 0, 0, hourE, minE, 0);
  this.dateE.set(yearE, monE - 1, dayE);
  this.name = name;
  this.description = description;
 }

 // Standard Get- Methoden
 public int getId() {
  return id;
 }

 public Calendar getDateB() {
  return dateB;
 }

 public Calendar getTimeB() {
  return timeB;
 }

 public Calendar getDateE() {
  return dateE;
 }

 public Calendar getTimeE() {
  return timeE;
 }

 public String getName() {
  return name;
 }

 public String getDescription() {
  return description;
 }


 // Standard Set- Methoden
 public void setId(int id) {
  this.id = id;
 }

 public void setDateB(Calendar dateB) {
  this.dateB = dateB;
 }

 public void setTimeB(Calendar timeB) {
  this.timeB = timeB;
 }

 public void setDateE(Calendar dateE) {
  this.dateE = dateE;
 }

 public void setTimeE(Calendar timeE) {
  this.timeE = timeE;
 }

 public void setName(String name) {
  this.name = name;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 // Spezielle Get- Methoden für Calendar (Array:
 // [DAY][MONTH][YEAR][HOUR][MIN])
 public int[] getDateB_M() {
  return new int[] { dateB.get(Calendar.DAY_OF_MONTH),
    dateB.get(Calendar.MONTH) + 1, dateB.get(Calendar.YEAR),
    timeB.get(Calendar.HOUR_OF_DAY), timeB.get(Calendar.MINUTE) };
 }

 public int[] getDateE_M() {
  return new int[] { dateE.get(Calendar.DAY_OF_MONTH),
    dateE.get(Calendar.MONTH) + 1, dateE.get(Calendar.YEAR),
    timeE.get(Calendar.HOUR_OF_DAY), timeE.get(Calendar.MINUTE) };
 }

 // Spezielle Set- Methoden für Calendar
 public void setDateB(int day, int month, int year, int hour, int min) {
  this.timeB.set(0, 0, 0, hour, min);
  this.dateB.set(year, (month - 1), day);
 }

 public void setDateE(int day, int month, int year, int hour, int min) {
  this.timeE.set(0, 0, 0, hour, min);
  this.dateE.set(year, (month - 1), day, hour, min);
 }

 public void setDateB(int day, int month, int year) {
  this.dateB.set(year, (month - 1), day, 0, 0);
 }

 public void setDateE(int day, int month, int year) {
  this.dateE.set(year, (month - 1), day, 0, 0);
 }
}

date0 代:

Calendar rangeStart = Calendar.getInstance();
int dayB, monB, yearB;

dayB = this.readGetParameterAsInteger("startDay");
monB = this.readGetParameterAsInteger("startMonth");
yearB = this.readGetParameterAsInteger("startYear");

rangeStart.set(yearB, monB, dayB, 0, 0, 0);

我能做什么?

【问题讨论】:

  • 我认为您将需要显示比这更多的代码 - 可能是 MaturityRestrictions 类。
  • 限制由 Hibernate 给出。 :)
  • 定义“不起作用”。它会给你一个运行时错误吗?查询是否返回奇数数据? “date0”的类型是什么?
  • @H3llGhost:尝试启用org.hibernate.type 日志记录类别以查看JDBC 参数值:docs.jboss.org/hibernate/core/3.6/reference/en-US/html/…。此外,它可能依赖于数据库,因为我无法在 HSQL 上重现它。
  • @codelark:不起作用意味着没有结果。 Date0 是一个日历对象。 @axtavt:我添加了以下行 log4j.logger.org.hibernate.test=info log4j.logger.org.hibernate.tool.hbm2ddl=debug log4j.logger.org.hibernate.type=debug 但没有更多信息在控制台上。

标签: java mysql hibernate date calendar


【解决方案1】:

Hibernate 类型 calendar、calendar_date 将 java.util.Calendar 映射到 SQL 类型 TIMESTAMP 和 DATE。你就在那儿。 但是您需要显示 date0 的类型以及它是如何构造或检索的。

还可以尝试替代日历而不是 calendar_date

【讨论】:

    【解决方案2】:

    解决方法如下:

    Calendar rangeStart = Calendar.getInstance();
    int dayB, monB, yearB;
    
    dayB = this.readGetParameterAsInteger("startDay");
    monB = this.readGetParameterAsInteger("startMonth");
    yearB = this.readGetParameterAsInteger("startYear");
    
    rangeStart.set(yearB, monB-1, dayB, 0, 0, 0);
    

    因为月份是从 0 开始的! -.-

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多