开发者

Calendar to Date mapping

I have a problem 开发者_运维技巧with Hibernate. I tried to map a Calendar object to Date, which I used in my table structure.

In the reference guide of Hibernate you can find the informations about types mapping, where you will find something like that "calendar_date".

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

But a simple equals doesn't work (no result, but phpMyAdmin gives me with the same query results):

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

Here is the Maturity class:

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 generation:

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);

What can I do?


the Hibernate types calendar, calendar_date MAP java.util.Calendar to SQL types TIMESTAMP and DATE. You are right there. But you need to show what is the type of date0 and how its constructed or retrieved.

Also try the alterenative Calendar instead of the calendar_date


The solution is following:

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);

Because month is 0-based! -.-

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜