我在寻找一种方式持续存在的一个实体,其中包含一个领域中的一个账户类型。在这个特定例子,我想保留 ts 场作为数毫秒。

import org.joda.time.DateTime;

@Entity
public class Foo {

  @Id
  private Long id;

  private DateTime ts;
}
有帮助吗?

解决方案

访问用户没有能力登记册的定财产类型,只有使用商定的东西:

其他提示

由于它不是一个JPA定义的支持的类型,你依靠的实现细节。 DataNucleus将具有用于JodaTime一个插件,将允许您想要的持久性。

要么就可以使用这些提供者特定的东西,也可以使用@PostPersist@PostUpdate@PostLoad回调方法用替代@Transient字段的

http://www.java2s.com/Tutorial/Java/0355__JPA/ EntityListenerPostLoad.htm 会给你一些想法。

请放心取得联系,任何进一步的说明。

一种解决方案是使用非列属性和与getter / setter方法封装它们。

要告诉JPA以使用getter / setter方法,而不是直接accesing私有字段,你必须注解@Id上的众长的getId(),而不是私人长期ID 即可。如果这样做了,只记得使用@Transient为不直接对应于一列中的每个吸气。

以下示例将创建一个名为日期列的数值指明MyDate 下,而该应用程序将具有日期时间getTs()和长方砌石()方法可用于它。 (不知道的DateTime API,所以请原谅小错误:))

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; }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top