How can I save UTF-8 strings in an ObjectDB JPA database?

520 views Asked by At

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?

2

There are 2 answers

1
ObjectDB On BEST ANSWER

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.

0
HosseyNJF On

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"));