----- -----情況休眠:具有不映射到.hbm文件
我具備Java構建的REST API在POJO變量。我使用的技術如Jersey
,Hibernate
。 REST API正在從我們的移動應用程序中調用。在大多數情況下,當我們檢索數據時,REST API傳遞java beans
,而我們發送數據時移動應用程序也傳遞java beans
。
我們有很多Java Beans
和以下是一個簡單的例子,其Hibernate
映射。
Units.java
import java.util.Date;
/**
* Units generated by hbm2java
*/
public class Units implements java.io.Serializable {
private Integer idunits;
private Patient patient;
private UnitType unitType;
private String unitMeasurement;
private Date dateCreated;
private Date lastUpdated;
public Units() {
}
public Units(Patient patient, UnitType unitType, String unitMeasurement, Date lastUpdated) {
this.patient = patient;
this.unitType = unitType;
this.unitMeasurement = unitMeasurement;
this.lastUpdated = lastUpdated;
}
public Units(Patient patient, UnitType unitType, String unitMeasurement, Date dateCreated, Date lastUpdated) {
this.patient = patient;
this.unitType = unitType;
this.unitMeasurement = unitMeasurement;
this.dateCreated = dateCreated;
this.lastUpdated = lastUpdated;
}
public Integer getIdunits() {
return this.idunits;
}
public void setIdunits(Integer idunits) {
this.idunits = idunits;
}
public Patient getPatient() {
return this.patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public UnitType getUnitType() {
return this.unitType;
}
public void setUnitType(UnitType unitType) {
this.unitType = unitType;
}
public String getUnitMeasurement() {
return this.unitMeasurement;
}
public void setUnitMeasurement(String unitMeasurement) {
this.unitMeasurement = unitMeasurement;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
}
Units.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 23, 2016 3:21:00 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="beans.Units" table="units" catalog="myglukose" optimistic-lock="version">
<id name="idunits" type="java.lang.Integer">
<column name="idunits" />
<generator class="identity" />
</id>
<many-to-one name="patient" class="beans.Patient" fetch="select">
<column name="patient_idpatient" not-null="true" />
</many-to-one>
<many-to-one name="unitType" class="beans.UnitType" fetch="select">
<column name="unit_type_idunit_type" not-null="true" />
</many-to-one>
<property name="unitMeasurement" type="string">
<column name="unit_measurement" length="45" not-null="true" />
</property>
<property name="dateCreated" type="timestamp">
<column name="date_created" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="last_updated" length="19" not-null="true" />
</property>
</class>
</hibernate-mapping>
這裏,dateCreated
和lastUpdated
在我們的MySQL數據庫實際上timestamp
。我們注意到timestamp
的值(例如:dateCreated
,lastUpdated
)中的「時間」實際上已被更改並保存在數據庫中。但是,每當我們將數據從移動應用程序發送到REST API時,我們在GMT +5.30,所以如果我們將時間作爲12.28PM發送到REST API,那麼在DB中保存的是17:58。要麼我們保存在本地數據庫中,要麼保存在Amazon RDS
中,也是如此。當檢索時,我們得到了被保存的東西,17:58這是錯誤的。
我們的應用程序將被不同時區的人使用,所以我們不能堅持一個時區。
------我們的計劃是什麼------
這種情況是非常討厭,我們沒能找到一個解決方案。我們可以想到的最佳解決方案是將時間從手機發送到REST API作爲字符串,然後在服務器中將其轉換回Date
對象。
所以,我們提出以下解決方案。目前我們的java beans
代表確切的表格的領域,沒有別的。現在我們正在計劃添加更多變量,以保持我們發送的String time
。至於示例,請查看下面的內容,這是我在上面發佈的適用更改的bean。
package beans;
// Generated Sep 23, 2016 3:21:00 PM by Hibernate Tools 4.3.1
import java.util.Date;
/**
* Units generated by hbm2java
*/
public class Units implements java.io.Serializable {
private Integer idunits;
private Patient patient;
private UnitType unitType;
private String unitMeasurement;
private Date dateCreated;
private Date lastUpdated;
private String lastUpdatedStr;
private String dateCreatedStr;
public Units() {
}
public Units(Patient patient, UnitType unitType, String unitMeasurement, Date lastUpdated) {
this.patient = patient;
this.unitType = unitType;
this.unitMeasurement = unitMeasurement;
this.lastUpdated = lastUpdated;
}
public Units(Patient patient, UnitType unitType, String unitMeasurement, Date dateCreated, Date lastUpdated) {
this.patient = patient;
this.unitType = unitType;
this.unitMeasurement = unitMeasurement;
this.dateCreated = dateCreated;
this.lastUpdated = lastUpdated;
}
public Integer getIdunits() {
return this.idunits;
}
public void setIdunits(Integer idunits) {
this.idunits = idunits;
}
public Patient getPatient() {
return this.patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public UnitType getUnitType() {
return this.unitType;
}
public void setUnitType(UnitType unitType) {
this.unitType = unitType;
}
public String getUnitMeasurement() {
return this.unitMeasurement;
}
public void setUnitMeasurement(String unitMeasurement) {
this.unitMeasurement = unitMeasurement;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public Date getLastUpdated() {
return this.lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
/**
* @return the lastUpdatedStr
*/
public String getLastUpdatedStr() {
return lastUpdatedStr;
}
/**
* @param lastUpdatedStr the lastUpdatedStr to set
*/
public void setLastUpdatedStr(String lastUpdatedStr) {
this.lastUpdatedStr = lastUpdatedStr;
}
/**
* @return the dateCreatedStr
*/
public String getDateCreatedStr() {
return dateCreatedStr;
}
/**
* @param dateCreatedStr the dateCreatedStr to set
*/
public void setDateCreatedStr(String dateCreatedStr) {
this.dateCreatedStr = dateCreatedStr;
}
}
正如你所看到的,我們已經增加了新的變數lastUpdatedStr
和dateCreatedStr
。無論如何,請注意:我們沒有將這些文件映射到Units.bhm.xml
文件中,因爲這些文件無法使用Hibernate
。
-------- --------題
- 是我們正在計劃做一個良好的編程習慣?
- 我們沒有將變量映射到hibernate中,因爲它們不是數據庫列,而hibernate與它無關。但是,冬眠會以某種方式搞砸這些像試圖設置值的東西?我們進行了一個小測試。似乎沒有。
- 我們遇到這個
timestamp
只有當發送Date
對象(當然有)從android到REST並保存到數據庫。當我們收回他們回到手機,我們收到了保存的內容(例如:我們發送12:28 PM到REST,但它被保存爲17:58。當檢索我們收到17:58)。但是,我們沒有與不同國家的用戶進行覈對,所以我們不知道它是否會一直這樣。我們不打算通過REST將這些數據從數據庫檢索到String
對象,而是獲取Date
對象並使用它。其中一個原因是我們使用Retrofit
來撥打REST
來電,並且我們有一個自定義GSON
日期序列器,所以我們必須發送Date
對象。那麼,這可能會在一天內出現問題,我們是否應該檢索轉換爲String
的對象Date
?
我們得到17:28。我們在MySQL –
http://dba.stackexchange.com/questions/62466/mysql-timestamp-timezone-handling –
http://stackoverflow.com/questions/15017799/how-to-convert-utc-date-to -local-time-zone-in-mysql-select-query –