Using JPA and JAXB Annotations in the Same Object

from: https://objectpartners.com/2010/01/25/using-jpa-and-jaxb-annotations-in-the-same-object/

要点:The trick is to separate the two types of annotations and only use the JPA annotation on the member declaration and the JAXB annotation on the accessor (get) method.

Recently, I started working on some projects where I had to prototype various service end point technologies, including REST and AMF.  During this process, I worked out a fairly nice prototype project template that makes setting up a project fairly simple and quick.  I plan to write about the template more fully in a forthcoming post, but for now wanted to share a small gotcha that took me a bit to figure out.

I’m using JPA for persistence and JAXB to produce XML to some of the end points.  I’ve also been using annotations to minimize the amount of XML configuration that I have to do.  Things were going really well, until I wanted to customize the XML representation of one of my domain object members as well as persist it.  The member was a date and I wanted a simple month-day-year display rather than JAXB’s default locale formatting.  I started out with:

@Entity
@XmlRootElement
public class Person implements Serializable {

  @Temporal(TemporalType.DATE)
  private Date birthDate;

  public Date getBirthDate() {
    return birthDate;
  }

  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }
}

This worked but when I retrieved my XML via the REST end point my dates looked like:

<birthDate>1987-11-01T00:00:00-06:00</birthDate>

Kind of ugly.  Since formatting dates is quite common in the problem domain that these technologies would eventually be used in, I decided to add an example of it to the prototype.  I wrote a quick date adapter class:

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DateAdapter extends XmlAdapter<String, Date> {
 // the desired format
 private String pattern = "yyyy-MM-dd";

 public String marshal(Date date) throws Exception {
 return new SimpleDateFormat(pattern).format(date);
 }

 public Date unmarshal(String dateString) throws Exception {
 return new SimpleDateFormat(pattern).parse(dateString);
 }
}

And simplistically tried to set it up by adding the annotation to the member declaration.

@Entity
@XmlRootElement
public class Person implements Serializable {

  @Temporal(TemporalType.DATE)
  @XmlJavaTypeAdapter(value=DateAdapter.class)
  private Date birthDate;

  public Date getBirthDate() {
    return birthDate;
  }

  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }
}

When I went to marshall the object, I hit an IllegalAnnotationException.

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "birthDate"

It took a bit to figure out what was going on, and there was surprisingly little information or examples that tried to do this sort of thing when I searched around.   Eventually, I figured out what might be obvious to some.  The trick is to separate the two types of annotations and only use the JPA annotation on the member declaration and the JAXB annotation on the accessor (get) method.  When I altered my code to this:

@Entity
@XmlRootElement
public class Person implements Serializable {

  @Temporal(TemporalType.DATE)
  private Date birthDate;

  @XmlJavaTypeAdapter(value=DateAdapter.class)
  public Date getBirthDate() {
      return birthDate;
  }

  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }

}

Everything started working as expected.  Hopefully this can be helpful to anyone that encounters a similar situation.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值