Using JPA and JAXB Annotations in the Same Object

转载 2015年07月11日 09:02:58

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.


Centos6.5安装Metasploit记录

内容来源于互联网,只为记录 1:安装ruby yum -y install ruby 2:根据官方文档执行,安装支持包 sudo yum install...
  • wyvbboy
  • wyvbboy
  • 2016年05月28日 20:47
  • 5009

Centos安装Metasploit小记

以前用Metasploit都是在ubuntu平台使用,kali或者Backtrack。最近买了一台Centos的VPS,因为便宜,所以VPS不怎么滴,从国内ping延迟大概在300左右,不太适合做网站...

add more columns in join table using spring, hibernate, JPA Annotations

Can we add more columns in a join table annotation? Generally I see people ending up with adding two...

Multi-Column Join in Hibernate/JPA Annotations

I have two entities which I would like to join through multiple columns. These columns are shared by...

码农小汪-spring框架学习之8- Using JSR 330 Standard Annotations @Inject and @Named

Using JSR 330 Standard Annotations这句话是什么意识呢?我们可以不对spring进行依赖,换成别的东西也是可以注入依赖的?是不是可以这么理解,这些事JavaEE自己处理...

what's annotations is in java? what's the relationship between annotation and reflection?

Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are...

FSharp.Data.SqlClient: Seamlessly integrating T-SQL and F# in the same code

http://blogs.msdn.com/b/fsharpteam/archive/2014/05/23/fsharp-data-sqlclient-seamlessly-integrating-s...
  • hadstj
  • hadstj
  • 2014年11月27日 15:17
  • 900

Type Annotations in Java 8: Tools and Opportunities

In previous versions of Java, developers could write annotations only on declarations. With Java 8, ...

Using Second Level Caching in a JPA Application

 Caching structurally implies a temporary store to keep data for quicker access later on. Second ...

Update metadata value using server object model in sharepoint 2013

1. Update the metadata value of the item in list about sharepoint 2013. 2. Save the result into log....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Using JPA and JAXB Annotations in the Same Object
举报原因:
原因补充:

(最多只允许输入30个字)