English 中文(简体)
How to persist an entity which contains a field of user type using JPA2
原标题:

I m looking for a way to persist an entity which contains a field of a user type. In this particular example I would like to persist the ts field as number of milliseconds.

import org.joda.time.DateTime;

@Entity
public class Foo {

  @Id
  private Long id;

  private DateTime ts;
}
最佳回答

JPA does not have the ability to register custom property types, you ll have to use provider specific stuff:

问题回答

Since it s not a JPA defined supported type you rely on implementation specifics. DataNucleus has a plugin for JodaTime that would allow your desired persistence.

Either you can use those provider specific stuffs or you can make use of @PostPersist, @PostUpdate, @PostLoad callback methods with a surrogate @Transient field.

http://www.java2s.com/Tutorial/Java/0355__JPA/EntityListenerPostLoad.htm will give you some idea.

Please feel comfortable to get in touch for any further clarification.

One solution is to use non-column properties and encapsulate them with getters/setters.

To tell JPA to use getters/setters instead of directly accesing private fields, you must annotate @Id on public Long getId() instead of private Long id. When doing this, just remember to use @Transient for every getter that does not correspond directly to a column.

The following example would create a Date column named myDate, while the application would have DateTime getTs() and setTs() methods available for it. (not sure about DateTime API, so please forgive minor errors :))

import org.joda.time.DateTime;

@Entity
public class Foo {

  private Long id;

  private DateTime ts;

  @Id
  public Long getId() { return id; }

  public void setId(Long id) { this.id = id; }



  // These should be accessed only by JPA, not by your application;
  // hence they are marked as protected

  protected Date getMyDate() { return ts == null ? null : ts.toDate(); }

  protected void setMyDate(Date myDate) {
    ts = myDate == null ? null : new DateTime(myDate);
  }



  // These are to be used by your application, but not by JPA;
  // hence the getter is transient (if it s not, JPA will
  // try to create a column for it)

  @Transient
  public DateTime getTs() { return ts; }

  public void setTs(DateTime ts) { this.ts = ts; }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签