日历到日期映射

发布于 2024-10-10 15:28:25 字数 4825 浏览 2 评论 0原文

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

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

我能做什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-10-17 15:28:25

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

还可以尝试使用替代 Calendar 而不是 calendar_date

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

倾`听者〃 2024-10-17 15:28:25

解决办法如下:

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开始的! -.-

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文