2011-01-05 114 views
3

我有一個Hibernate的問題。我試圖將日曆對象映射到Date,我在表結構中使用它。日曆映射日曆

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

我能做什麼?

+1

我想你將需要展示更多的代碼 - 可能是「成熟度」和「限制」類。 – javamonkey79 2011-01-05 15:34:44

+1

限制是由Hibernate給出的。 :) – CSchulz 2011-01-05 15:49:01

+0

定義「不起作用」。它給你一個運行時錯誤?查詢是否返回奇數據?什麼是「date0」的類型? – codelark 2011-01-05 16:18:09

回答

1

該解決方案如下:因爲一個月0基於

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

! -.-

1

Hibernate類型日曆,calendar_date MAP java.util.Calendar到SQL類型TIMESTAMP和DATE。你就在那裏。 但是你需要顯示date0的類型以及它的構造或檢索方式。

也可以嘗試alterenative日曆,而不是CALENDAR_DATE的

+0

請參閱上面的內容,我嘗試了兩種方法,但都沒有成功。 – CSchulz 2011-01-05 16:14:19