如何在 JPA 中保留未使用 @Entity 标记注释的 class 对象?
How to persist an class's object that is not annotated with @Entity tag in JPA?
我正在使用 ObjectDB 来存储我的对象。但是我想存储一个没有用 @Entity
标签注释的对象,因为这些对象是在我的包之外(在一个库中)创建的,我不想将整个库克隆到我的项目中,只是为了添加注释。这就是我要坚持的class:
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
class,虽然没有什么重要的:
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 {
}
我知道我可以创建这个 class 的克隆,用 @Entity
注释它并使用适配器转换它们,但这是一种浪费。我想知道是否有更好的方法来 persist/read/do 任何没有注释的 class?
一种选择是使用 JSON 框架将对象编组到 JSON 并持久化。然后,使用相同的框架将 JSON 解组回一个对象。鉴于该对象将其所有字段设置为最终状态,您将不得不使用一些基于反射的框架来创建实例。
正如 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>
我正在使用 ObjectDB 来存储我的对象。但是我想存储一个没有用 @Entity
标签注释的对象,因为这些对象是在我的包之外(在一个库中)创建的,我不想将整个库克隆到我的项目中,只是为了添加注释。这就是我要坚持的class:
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
class,虽然没有什么重要的:
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 {
}
我知道我可以创建这个 class 的克隆,用 @Entity
注释它并使用适配器转换它们,但这是一种浪费。我想知道是否有更好的方法来 persist/read/do 任何没有注释的 class?
一种选择是使用 JSON 框架将对象编组到 JSON 并持久化。然后,使用相同的框架将 JSON 解组回一个对象。鉴于该对象将其所有字段设置为最终状态,您将不得不使用一些基于反射的框架来创建实例。
正如 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>