Hibernate:POJO中的变量没有映射到.hbm文件

时间:2016-12-15 07:34:00

标签: java android mysql hibernate rest

----- -----情况

我有一个用Java构建的REST API。我使用了JerseyHibernate等技术。从我们的移动应用程序调用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>

此处,dateCreatedlastUpdated实际上是我们MySQL数据库中的timestamp

然而,无论何时我们将数据从移动应用程序发送到REST API,我们都注意到&#34; time&#34; timestamp值(例如:dateCreatedlastUpdated)实际上已更改并保存在数据库中。我们在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;
    }
}

如您所见,我们添加了新变量lastUpdatedStrdateCreatedStr。无论如何,请注意:我们没有将这些信息映射到Units.bhm.xml文件,因为这些信息与Hibernate无关。

-------- --------问题

  1. 我们打算做一个好的编程实践吗?
  2. 我们没有将变量映射到hibernate,因为它们不是数据库列,而hibernate与它无关。但是,hibernate会不知何故搞砸这些就像试图以某种方式设置值?我们进行了一次小测试。似乎没有。
  3. 仅当发送 timestamp个对象(当然,时间)从android到REST并保存到数据库时,我们才会遇到此Date更改。当将它们检索回移动设备时,我们收到了已保存的内容(例如:我们在下午12:28发送到REST但它保存为17:58。检索时我们收到了17:58)。但是,我们没有与不同国家/地区的用户核实,因此我们始终不知道是否会这样。我们不打算通过REST将这些String对象从数据库检索到移动设备,而是获取Date个对象并使用它。这样做的一个原因是我们使用Retrofit进行REST调用,我们有一个自定义GSON日期序列化程序,因此我们无论如何都必须发送Date个对象。那么,这可能会在一天内出现问题,我们是否应该检索转换为Date的{​​{1}}个对象?

1 个答案:

答案 0 :(得分:-1)

  

我们注意到时间戳值中的“时间”(例如:dateCreated,   lastUpdated)实际上已更改并保存在数据库中。我们在GMT   +5.30,所以如果我们将时间作为12.28PM发送到REST API,那么保存在DB中的是17:28

检索相同记录时从数据库获取的值是多少? 12.28PM或17.28PM?

如果检索到的值是12.28,则由PostgreSQL等数据库进行自动对话。我认为这对你没问题。