Immutable Value objects and JPA

旧街凉风 提交于 2019-11-30 17:39:27

You won't be able to do that using standard JPA annotations and an embeddable object, because the object will have to be created using a default constructor, and the value set via reflection.

You could however use a Hibernate custom type. Read this part of the Hibernate reference documentation, where there is an example Money type, which is instantiated using a constructor with arguments, and could thus be immutable.

Probably the easiest solution which works with a little older versions like 3.5 of Hibernate is to implement org.hibernate.usertype.UserType. There is quite a few methods in it but for immutable types you can extract most of them to common superclass:

package com.acme;

import java.io.Serializable;

import org.hibernate.usertype.UserType;

public abstract class AbstractImmutableType
  implements UserType {

 public AbstractImmutableType() {
  super();
 }

 public boolean isMutable() {
  return false;
 }

 public Serializable disassemble(Object value) {
  return (Serializable) value;
 }

 public Object assemble(Serializable cached, Object owner) {
  return cached;
 }

 public Object deepCopy(Object value) {
  return value;
 }

 public Object replace(Object original, Object target,
   Object owner) {
  return original;
 }

 public boolean equals(Object x, Object y) {
  if (x != null && y != null) {
   return x.equals(y);
  }
  // Two nulls are equal as well
  return x == null && y == null;
 }

 public int hashCode(Object x) {
  if (x != null) {
   return x.hashCode();
  }
  return 0;
 }
}

And you can use it like this:

package com.acme;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class CurrencyType extends AbstractImmutableType {

 public static final String TYPE = "com.acme.CurrencyType";

 private static final int[] SQL_TYPES = {
    Types.VARCHAR
 };

 public CurrencyType() {
  super();
 }

 public Object nullSafeGet(ResultSet rs, String[] names,
    Object owner) throws SQLException {
  String value = rs.getString(names[0]);
  if (rs.wasNull()) {
   return null;
  }
  return Currency.valueOf(value);
 }

 public void nullSafeSet(PreparedStatement st, Object value,
    int index) throws SQLException {
  if (value != null) {
   st.setString(index, ((Currency)value).getCode());
  } else {
   st.setNull(index, SQL_TYPES[0]);
  }
 }

 public Class<?> returnedClass() {
  return Currency.class;
 }

 public int[] sqlTypes() {
  return SQL_TYPES;
 }
}

Longer explanation for this code you can find here

For JPA to be able to create objects via reflection, you have to have a default constructor, but it doesn't have to be public. I also like to keep my fields final, but this might be too restrictive for reflection -- you'll have to try.

I'd suggest dropping the final field modifier and adding a private default constructor with a short comment (so you still know why that no-op constructor is there next week):

public final class EmailAddress {
    private String value; // no final modifier

    private EmailAddress() {
        // for JPA
    }

    public EmailAddress(String value) {
        this.value = value;
    }
...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!