如何在JPA中保留未使用@Entity标记注释的类对象?

时间:2017-09-11 14:25:55

标签: java jpa annotations objectdb

我正在使用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注释它并使用适配器来转换它们,但这是浪费。我想知道是否有更好的方法来坚持/阅读/做一些没有注释的课程?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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