【问题标题】:JAVA hibernate/webservice - Problem with TimestampJAVA hibernate/webservice - 时间戳问题
【发布时间】:2010-06-22 12:57:42
【问题描述】:

在我的一个休眠类中,我有一个 Timestamp 变量。现在,当我只在服务器上使用休眠的东西时,一切都好了。但现在我正在使用 wsgen 实现 web 服务。

我收到一个错误,因为 Timestamp 没有无参数默认构造函数。

消息;

Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.sql.Timestamp does not have a no-arg default constructor.

班级:

package com.PTS42.planner;

import java.io.Serializable;
import java.sql.Timestamp;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@WebService
@Entity @Table(name="Reserveringen")
public class Reservering implements Serializable {

    @Id @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    private int id;

    private Timestamp vanaf;

    private Timestamp tot;

    @ManyToOne
    @Embedded
    private Klant klant;

    @ManyToOne
    @Embedded
    private Machine machine;

    public int getId() {
        return id;
    }

    public void setId(@WebParam(name="reservering_id")int id) {
        this.id = id;
    }

    public Klant getKlant() {
        return klant;
    }

    public void setKlant(@WebParam(name="reservering_klant")Klant klant) {
        this.klant = klant;
    }

    public Timestamp getTot() {
        return tot;
    }

    public void setTot(@WebParam(name="reservering_tot")Timestamp tot) {
        int tempint = tot.getMonth();
        tot.setMonth(tempint-1);
        this.tot = tot;
    }

    public Timestamp getVanaf() {
        return vanaf;
    }

    public void setVanaf(@WebParam(name="reservering_vanaf")Timestamp vanaf) {
        int tempint = vanaf.getMonth();
        vanaf.setMonth(tempint-1);
        this.vanaf = vanaf;
    }

    public Machine getMachine() {
        return machine;
    }

    public void setMachine(@WebParam(name="reservering_machine")Machine machine) {
        this.machine = machine;
    }




    public Reservering() {
    }

    public Reservering(@WebParam(name="reservering_constructor_vanaf") Timestamp vanaf, @WebParam(name="reservering_constructor_tot")Timestamp tot, @WebParam(name="reservering_constructor_klant")Klant klant, @WebParam(name="reservering_constructor_machine")Machine machine)
    {

        this.vanaf = vanaf;
        this.tot = tot;
        this.klant = klant;
        this.machine = machine;
    }



}

任何人都知道如何解决这个问题,而无需使用时间戳等其他类型的变量。

【问题讨论】:

  • 你的 Hibernate 类是否需要一个 Timestamp 变量? Timestamp 是围绕 java.util.Date 的特定于 SQL 的包装器。如果您不需要精确到纳秒的时间,那么您可能不需要 Timestamp 并且可以直接使用 Date 。在数据库层之外公开特定于 SQL 的类型通常被认为是不好的形式,但我不确定更改 Hibernate 类是否对此分配有效。
  • 当我在休眠对象中使用日期时。并将其放入数据库中,mysql数据库中的日期时间不占用时间(小时和分钟)。
  • 您是否使用 Hibernate 注解进行实体映射?如果是这样,您可以使用注释 @Temporal(TemporalType.TIMESTAMP) 调整数据库中的精度
  • 发布了代码,所以我可以将所有时间戳更改为日期。然后将@Temportal(TemporalType.TIMESTAMP) 放在变量上方:“van and tot”?

标签: java hibernate web-services timestamp


【解决方案1】:

我不知道你为什么反对使用时间戳以外的东西。以下代码将为您提供相同的数据库结构,并且它不会尝试在 SQL 类没有暴露业务的情况下公开 SQL 类:

package com.PTS42.planner;

import java.io.Serializable;
import java.util.Date
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@WebService
@Entity @Table(name="Reserveringen")
public class Reservering implements Serializable {

    @Id @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    private int id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date vanaf;

    @Temporal(TemporalType.TIMESTAMP)
    private Date tot;

    @ManyToOne
    @Embedded
    private Klant klant;

    @ManyToOne
    @Embedded
    private Machine machine;

    public int getId() {
        return id;
    }

    public void setId(@WebParam(name="reservering_id")int id) {
        this.id = id;
    }

    public Klant getKlant() {
        return klant;
    }

    public void setKlant(@WebParam(name="reservering_klant")Klant klant) {
        this.klant = klant;
    }

    public Date getTot() {
        return tot;
    }

    public void setTot(@WebParam(name="reservering_tot")Date tot) {
        //int tempint = tot.getMonth();
        //tot.setMonth(tempint-1);
        this.tot = tot;
    }

    public Date getVanaf() {
        return vanaf;
    }

    public void setVanaf(@WebParam(name="reservering_vanaf")Date vanaf) {
        //int tempint = vanaf.getMonth();
        //vanaf.setMonth(tempint-1);
        this.vanaf = vanaf;
    }

    public Machine getMachine() {
        return machine;
    }

    public void setMachine(@WebParam(name="reservering_machine")Machine machine) {
        this.machine = machine;
    }

    public Reservering() {
    }

    public Reservering(@WebParam(name="reservering_constructor_vanaf") Date vanaf, @WebParam(name="reservering_constructor_tot")Date tot, @WebParam(name="reservering_constructor_klant")Klant klant, @WebParam(name="reservering_constructor_machine")Machine machine)
    {

        this.vanaf = vanaf;
        this.tot = tot;
        this.klant = klant;
        this.machine = machine;
    }
}

【讨论】:

  • 刚才试了一下,现在mysql数据库没有时间了。在尝试了一些事情之后,我的约会对象去了:12/14/3910 8:10:00 PM 那比我要求的要高 1900 年。
  • 你能调试你的代码看看日期在哪里搞砸了吗?我的猜测是在 Web 服务层。如果是这种情况,您可能想尝试将字段映射为日历对象。日历字段(带有 @Temporal 注释)应该仍然可以很好地与 Hibernate 配合使用,它们可能是 Web 服务序列化的更好选择。
  • 搞定了!!!当我创建一个 Reservering 对象时,我用 calendar.getTime().getTime() 填充日期。现已修复
  • uuhmm,如果您来自 java.util.Calendar,您可以在日历对象上调用 getTimeInMillis(),而不是先获取 Date 对象然后再获取毫秒。为您节省一个电话
【解决方案2】:

您需要为该类型创建一个自定义编组器/解组器,因为它没有 wsgen (@XmlJavaTypeAdapter) 中需要的无参数构造函数

注解需要一个类作为参数,该类必须从抽象类XmlAdapter扩展。 XmlAdapter 定义了将绑定类型调整为值类型的方法,或者相反。

【讨论】:

  • 我真的不知道该怎么做,有点讨厌java,但这是一个任务,所以..无论如何你知道那里有没有这样的东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多