日历到日期映射

时间:2011-01-05 15:20:22

标签: java mysql hibernate date calendar

我遇到了Hibernate的问题。我试图将Calendar对象映射到Date,我在表结构中使用了它。

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

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

但是一个简单的equals不起作用(没有结果,但是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);

我该怎么办?

2 个答案:

答案 0 :(得分:1)

Hibernate类型日历,calendar_date MAP java.util.Calendar到SQL类型TIMESTAMP和DATE。你就在那里。 但是你需要显示date0的类型以及它的构造或检索方式。

还可以尝试使用替换日历而不是calendar_date

答案 1 :(得分:1)

解决方案如下:

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基础! -.-