2

我正在使用ObjectDB来存储我的对象。但是我想存储一个没有用@Entity标签注释的对象,因为这些对象是在我的包之外(在库中)创建的,我不想将整个库克隆到我的项目中,只是为了添加注释。这是我要坚持的课程:

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 + '\'' +
                '}';
    }
}

这就是BotApiObject类,虽然它没有什么重要的:

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 {
}

我知道我可以创建这个类的克隆,使用它进行注释@Entity并使用适配器来转换它们,但这是一种浪费。我想知道是否有更好的方法来持久化/读取/对未注释的类进行任何操作?

4

2 回答 2

2

正如 DN1 在评论中建议的那样,您可以使用 orm.xml 文件而不是 JPA 注释。对于每个 JPA 注释,都有一个 XML 替换。

例如,添加一个 META-INF/orm.xml 文件:

<?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>

于 2017-09-12T14:29:48.503 回答
-1

一种选择是使用 JSON 框架将对象编组为 JSON 并将其持久化。然后,使用相同的框架将 JSON 解组回对象。鉴于该对象将其所有字段设置为 final,您将不得不使用一些基于反射的框架来创建实例。

于 2017-09-11T14:47:34.087 回答