2

I'm using ObjectDB to store my objects. But I want to store an object that is not annotated with the @Entity tag, because the objects are created outside my package (In a library) and I do not want to clone the entire library into my project, only to add an annotation. This is the class I want to persist:

package org.telegram.telegrambots.api.objects;

import com.fasterxml.jackson.annotation.JsonProperty;

import org.telegram.telegrambots.api.interfaces.BotApiObject;

/**
 * @author Ruben Bermudez
 * @version 3.0
 * This object represents a Telegram user or bot.
 */
public class User implements BotApiObject {

    private static final String ID_FIELD = "id";
    private static final String FIRSTNAME_FIELD = "first_name";
    private static final String ISBOT_FIELD = "is_bot";
    private static final String LASTNAME_FIELD = "last_name";
    private static final String USERNAME_FIELD = "username";
    private static final String LANGUAGECODE_FIELD = "language_code";

    @JsonProperty(ID_FIELD)
    private Integer id; ///< Unique identifier for this user or bot
    @JsonProperty(FIRSTNAME_FIELD)
    private String firstName; ///< User‘s or bot’s first name
    @JsonProperty(ISBOT_FIELD)
    private Boolean isBot; ///< True, if this user is a bot
    @JsonProperty(LASTNAME_FIELD)
    private String lastName; ///< Optional. User‘s or bot’s last name
    @JsonProperty(USERNAME_FIELD)
    private String userName; ///< Optional. User‘s or bot’s username
    @JsonProperty(LANGUAGECODE_FIELD)
    private String languageCode; ///< Optional. IETF language tag of the user's language

    public User() {
        super();
    }

    public Integer getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getUserName() {
        return userName;
    }

    public String getLanguageCode() {
        return languageCode;
    }

    public Boolean getBot() {
        return isBot;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", isBot=" + isBot +
                ", lastName='" + lastName + '\'' +
                ", userName='" + userName + '\'' +
                ", languageCode='" + languageCode + '\'' +
                '}';
    }
}

And this is the BotApiObject class, although it has nothing important:

package org.telegram.telegrambots.api.interfaces;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @author Ruben Bermudez
 * @version 1.0
 * An object from the Bots API received from Telegram Servers
 */
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public interface BotApiObject extends Serializable {
}

I know that I can create a clone of this class, annotate it with @Entity and use an adapter to convert them, But this is a waste. I want to know if there are any better ways to persist/read/do whatever with a not annotated class?

HosseyNJF
  • 449
  • 1
  • 7
  • 17
  • 1
    When you say "persist", persist WHERE? In an RDBMS is not possible using JPA without annotations or an `orm.xml` defining the class as an entity. With an `orm.xml` file defining which classes are entities you can do it without touching the classes themselves, and then basic JPA applies –  Sep 11 '17 at 15:23
  • @DN1 Thank you! I really didn't note that an XML file option exists. – HosseyNJF Sep 12 '17 at 15:02
  • Just put it under "META-INF" and it should be pulled in automatically, as per the JPA spec –  Sep 12 '17 at 15:14
  • Do the META-INF gets loaded in IntelliJ's debugger? I think that get included in a build. @DN1 – HosseyNJF Sep 12 '17 at 15:17

2 Answers2

2

As DN1 suggested in a comment you can use an orm.xml file instead of JPA annotations. For every JPA annotation there is an XML replacement.

For example, add a META-INF/orm.xml file:

<?xml version="1.0" encoding="UTF-8" ?>

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0">

 <entity class="org.telegram.telegrambots.api.objects.User" metadata-complete="true" />

</entity-mappings>
ObjectDB
  • 1,294
  • 7
  • 8
  • Is it possible with ObjectDB? How to tell the library to load the xml file? Does it autoload? – HosseyNJF Sep 12 '17 at 14:58
  • If it is located at META-INF/orm.xml then it should be loaded automatically (not just by ObjectDB, by every JPA implementation). – ObjectDB Sep 12 '17 at 16:52
-1

One option is to use a JSON framework to marshal the object to JSON and persist that. Then, use the same framework to unmarshal the JSON back to an object. Given that the object is sets all its fields final, you will have to use some Reflection-based framework to create instances.

Steve11235
  • 2,673
  • 1
  • 15
  • 18