2

I'm trying to save some UTF-8 strings in the ObjectDB database with the following code:

ResourceBundle entries = Utf8ClassLoader.getBundle("localization/language", "fa-IR"); // fa-IR is a UTF-8 and RTL language
Enumeration<String> keys = entries.getKeys();
for (String key = keys.nextElement(); keys.hasMoreElements(); key = keys.nextElement()) {
    ResourceEntity entity = new ResourceEntity();
    entity.setId(new ResourceEntity.PKC(key, locale));
    entity.setValue(entries.getObject(key));
    resourceDAO.persistOrUpdate(entity);
}

The model:

@Entity
public class ResourceEntity {
    @EmbeddedId
    private PKC id;

    private Object value;

    // Getters and setters omitted

    @Embeddable
    public static class PKC {
        String key;
        String locale;

        public PKC() {}

        public PKC(String key, String locale) {
            this.key = key;
            this.locale = locale;
        }

        // Getters and setters omitted
    }
}

localization/language_fa_IR.properties exists and opens properly.

The DAO's persistOrUpdate method is nothing more than an EntityManager.persist function within a transaction. (and does EntityManager.merge If the key exists)

But when I open the ObjectDBViewer, I see this: Screenshot

How can I save the strings without changing characters?

HosseyNJF
  • 449
  • 1
  • 7
  • 17

2 Answers2

1

There was no problem with the ObjectDB actually, the ResourceBundle was returning a not-UTF string. I solved it with this line:

entity.setValue(new String(entries.getString(key).getBytes("ISO-8859-1"), "UTF-8"));

HosseyNJF
  • 449
  • 1
  • 7
  • 17
1

ObjectDB as an object database just retrieves the objects with their content, including string fields, as you store them. So usually the encoding is not relevant for using ObjectDB, as it just preserves the state of your strings in memory.

It may, however, affect your ability to view special characters in the Explorer. For this you may also try setting an encoding in the Explorer using Tools > Options > General > Encoding.

ObjectDB
  • 1,294
  • 7
  • 8
  • Thank you, there was actually nothing wrong with the ObjectDB, only the ResourceBundle was giving a not-UTF-8 string and I fixed this in my answer. – HosseyNJF Sep 14 '17 at 13:15