views:

48

answers:

2

Hi,

In one of my hibernate classes I've a Timestamp variable. Now everything was alright when I used only the hibernate stuff on the server. But now I'm implementing webservice with wsgen.

I get an error because Timestamp doesn't got an no-args default constructor.

Message;

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.

The Class:

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



}

Anyone know how to solve this, without using an other kind of variable then Timestamp.

A: 

you will need to create a custom marshaller/unmarshaller for that type since it does not have a no-arg constructor which is needed in wsgen ( @XmlJavaTypeAdapter )

The annotations requires a class as parameter, the class must extend from an abstract class XmlAdapter. XmlAdapter defines methods for adapting a bound type to a value type or the other way around.

Redlab
I've really no idea how to do this, kinda hate java but it's an assignment so.. Anyway you know if there is anything like this out there?
Julian
A: 

I'm not sure why you're against using something other than Timestamp. The following code will give you the same database structure and it doesn't try to expose a SQL class where a SQL class has no business being exposed:

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;
    }
}
Jeff
Just tried that, but now there is no time in the mysql database. And after trying something my date went to: 12/14/3910 8:10:00 PMThats 1900 years above what I asked for.
Julian
Can you debug your code to see where the Date is getting messed up? My guess would be in the web service layer. If that's the case, you may want to try mapping the fields as Calendar objects. Calendar fields (with the @Temporal annotation) SHOULD still play nice with Hibernate and they're probably the better choice for web service serialization.
Jeff
Got it working!!! When I make an Reservering object I fill the date with a calendar.getTime().getTime(). Fixed now
Julian
uuhmm, if u're coming from a java.util.Calendar you can call getTimeInMillis() on the calendar object, instead of first getting a Date object and getting the milliseconds then. Saves you one call
Redlab