Package com.j256.ormlite.field

Examples of com.j256.ormlite.field.FieldConverter


    if (columnAnnotation == null && idAnnotation == null && oneToOneAnnotation == null
        && manyToOneAnnotation == null) {
      return null;
    }

    DatabaseFieldConfig config = new DatabaseFieldConfig();
    String fieldName = field.getName();
    if (databaseType.isEntityNamesMustBeUpCase()) {
      fieldName = fieldName.toUpperCase();
    }
    config.setFieldName(fieldName);

    if (columnAnnotation != null) {
      try {
        Method method = columnAnnotation.getClass().getMethod("name");
        String name = (String) method.invoke(columnAnnotation);
        if (name != null && name.length() > 0) {
          config.setColumnName(name);
        }
        method = columnAnnotation.getClass().getMethod("length");
        config.setWidth((Integer) method.invoke(columnAnnotation));
        method = columnAnnotation.getClass().getMethod("nullable");
        config.setCanBeNull((Boolean) method.invoke(columnAnnotation));
        method = columnAnnotation.getClass().getMethod("unique");
        config.setUnique((Boolean) method.invoke(columnAnnotation));
      } catch (Exception e) {
        throw SqlExceptionUtil.create("Problem accessing fields from the Column annotation for field " + field,
            e);
      }
    }
    if (idAnnotation != null) {
      if (generatedValueAnnotation == null) {
        config.setId(true);
      } else {
        // generatedValue only works if it is also an id according to {@link GeneratedValue)
        config.setGeneratedId(true);
      }
    }
    // foreign values are always ones we can't map as primitives (or Strings)
    config.setForeign(oneToOneAnnotation != null || manyToOneAnnotation != null);
    config.setDataPersister(DataPersisterManager.lookupForField(field));
    config.setUseGetSet(DatabaseFieldConfig.findGetMethod(field, false) != null
        && DatabaseFieldConfig.findSetMethod(field, false) != null);
    return config;
  }
View Full Code Here


  }

  @Test
  public void testGetFieldConverterSerializable() {
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    assertEquals(SqlType.BLOB, converter.getSqlType());
    assertTrue(converter.isStreamType());
  }
View Full Code Here

  }

  @Test(expected = SQLException.class)
  public void testObjectFieldConverterParseDefaultString() throws Exception {
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    converter.parseDefaultString(null, null);
  }
View Full Code Here

  }

  @Test(expected = SQLException.class)
  public void testObjectFieldConverterJavaToArgNonSerializable() throws Exception {
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    converter.javaToSqlArg(null, new NotSerializable());
  }
View Full Code Here

  }

  @Test
  public void testObjectFieldConverterJavaToArg() throws Exception {
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    Object object = converter.javaToSqlArg(null, "TEST");
    assertEquals(SerialBlob.class, object.getClass());
  }
View Full Code Here

    int COLUMN = 1;
    DatabaseResults results = (DatabaseResults) createMock(DatabaseResults.class);
    expect(results.getBlobStream(COLUMN)).andReturn(null);
    replay(results);
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    assertEquals(null, converter.resultToJava(null, results, COLUMN));
    verify(results);
  }
View Full Code Here

    DatabaseResults results = (DatabaseResults) createMock(DatabaseResults.class);
    String value = "NotASerializedObject";
    expect(results.getBlobStream(COLUMN)).andReturn(new ByteArrayInputStream(value.getBytes()));
    replay(results);
    DerbyEmbeddedDatabaseType type = new DerbyEmbeddedDatabaseType();
    FieldConverter converter = type.getFieldConverter(DataType.SERIALIZABLE.getDataPersister(), null);
    Object obj = converter.resultToJava(null, results, COLUMN);
    verify(results);
    assertEquals(value, obj);
  }
View Full Code Here

    }
  }

  public static <T, ID> MappedUpdateId<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo)
      throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot update-id in " + tableInfo.getDataClass()
          + " because it doesn't have an id field");
    }
    StringBuilder sb = new StringBuilder(64);
View Full Code Here

    super(tableInfo, statement, argFieldTypes);
  }

  public static <T, ID> MappedDelete<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo)
      throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot delete from " + tableInfo.getDataClass()
          + " because it doesn't have an id field");
    }
    StringBuilder sb = new StringBuilder(64);
View Full Code Here

  /**
   * This is private because the execute is the only method that should be called here.
   */
  private static <T, ID> MappedDeleteCollection<T, ID> build(DatabaseType databaseType, TableInfo<T, ID> tableInfo,
      int dataSize) throws SQLException {
    FieldType idField = tableInfo.getIdField();
    if (idField == null) {
      throw new SQLException("Cannot delete " + tableInfo.getDataClass()
          + " because it doesn't have an id field defined");
    }
    StringBuilder sb = new StringBuilder(128);
View Full Code Here

TOP

Related Classes of com.j256.ormlite.field.FieldConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.