使用TimestampAdapter解决JAXB无法映射Timestamp类型的有关问题
使用TimestampAdapter解决JAXB无法映射Timestamp类型的问题
当Jaxb中存在Timestamp类型(任何Bean中包含Timestamp)的话,容器启动时会出现一个运行时错误,说Timestamp没有一个默认的构造器。解决这个问题的办法如下:
(1)写一个Timestamp Adapter:
(2)在Bean的get方法中加入:
当Jaxb中存在Timestamp类型(任何Bean中包含Timestamp)的话,容器启动时会出现一个运行时错误,说Timestamp没有一个默认的构造器。解决这个问题的办法如下:
(1)写一个Timestamp Adapter:
import java.util.Date; import java.sql.Timestamp; import javax.xml.bind.annotation.adapters.XmlAdapter; public class TimestampAdapter extends XmlAdapter<Date, Timestamp> { public Timestamp unmarshal(Date val) throws Exception { return new Timestamp(val.getTime()); } public Date marshal(Timestamp val) throws Exception { return new Date(val.getTime()); } }
(2)在Bean的get方法中加入:
@Column(name = "TIME_UPDATED", length = 19) @XmlJavaTypeAdapter(TimestampAdapter.class) //加入这一行 public Timestamp getTimeUpdated() { return this.timeUpdated; }