Uploaded image for project: 'Cayenne'
  1. Cayenne
  2. CAY-1123

Add UUID support

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0
    • None
    • None
    • PostgreSQL

    Description

      To work properly with PostgreSQL's UUID columnt type I use next class which extends ExtendedType:

      package sandbox.orm.cayenne;

      import java.sql.CallableStatement;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.Types;
      import java.util.UUID;

      import org.apache.cayenne.CayenneRuntimeException;
      import org.apache.cayenne.access.types.ExtendedType;
      import org.apache.cayenne.map.DbAttribute;
      import org.apache.cayenne.validation.ValidationResult;

      public class UUIDType implements ExtendedType {

      @Override
      public String getClassName()

      { return UUID.class.getName(); }

      @Override
      public Object materializeObject(ResultSet rs, int index, int type)
      throws Exception {
      UUID uuid = null;
      switch (type) {
      case Types.NULL:
      uuid = null;
      break;
      default:
      try

      { uuid = UUID.fromString(rs.getObject(index).toString()); } catch (IllegalArgumentException e) { throw new CayenneRuntimeException( "Expected an instance of java.util.UUID, instead got " + uuid.getClass().getName() + ", column index: " + index); }
      }
      return uuid;
      }

      @Override
      public Object materializeObject(CallableStatement rs, int index, int type)
      throws Exception {
      UUID uuid = null;
      switch (type) {
      case Types.NULL:
      uuid = null;
      break;
      default:
      try { uuid = UUID.fromString(rs.getObject(index).toString()); }

      catch (IllegalArgumentException e)

      { throw new CayenneRuntimeException( "Expected an instance of java.util.UUID, instead got " + uuid.getClass().getName() + ", column index: " + index); }

      }
      return uuid;
      }

      @Override
      public void setJdbcObject(PreparedStatement statement, Object value,
      int pos, int type, int precision) throws Exception {
      if (value == null)

      { statement.setNull(pos, type); }

      else if (value instanceof UUID)

      { statement.setObject(pos, value); }

      else

      { throw new IllegalArgumentException("Expected java.util.UUID, got " + value.getClass().getName()); }

      }

      @Override
      public boolean validateProperty(Object source, String property,
      Object value, DbAttribute dbAttribute,
      ValidationResult validationResult)

      { return true; }

      }

      Then I register it with Configuration.getSharedConfiguration().getDomain().getNode("MyNode").getAdapter().getExtendedTypes().registerType(new UUIDType());

      Wouldn't it better to have same functionality in Cayenne core?

      Thanks,
      Artyom

      Attachments

        1. UUIDType.java
          2 kB
          Artyom Sokolov

        Activity

          People

            andrus Andrus Adamchik
            unnutz Artyom Sokolov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: