JAVA hibernate / webservice - 时间戳问题

时间:2010-06-22 12:57:42

标签: java hibernate web-services timestamp

在我的一个hibernate类中,我有一个Timestamp变量。当我只在服务器上使用hibernate时,一切都没问题。但现在我正在用wsgen实现webservice。

我收到错误,因为Timestamp没有no-args默认构造函数。

消息;

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



}

任何人都知道如何解决这个问题,而不使用其他类型的变量,然后使用Timestamp。

2 个答案:

答案 0 :(得分:1)

您需要为该类型创建自定义marshaller / unmarshaller,因为它没有wsgen(@XmlJavaTypeAdapter)中需要的无参数构造函数

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

答案 1 :(得分:1)

我不确定为什么你反对使用Timestamp以外的东西。以下代码将为您提供相同的数据库结构,并且它不会尝试公开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;
    }
}