`
yacki
  • 浏览: 46320 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

使用TimestampAdapter解决JAXB无法映射Timestamp类型的问题

阅读更多
当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;
}
分享到:
评论
2 楼 CoderDream 2014-12-06  
设置set方法:

public void setTimeUpdated(@XmlJavaTypeAdapter(TimestampAdapter.class) Timestamp timeUpdated) {
this.timeUpdated=timeUpdated;
}
1 楼 freefly64 2014-02-19  

相关推荐

Global site tag (gtag.js) - Google Analytics