简体   繁体   English

我可以在@Document中具有Class类型的字段吗?

[英]Can I have a field in a @Document that is of the Class type?

I want to persist a document to MongoDB. 我想将文档保留到MongoDB。 I'm using Spring-Data-MongoDB to declare @Document s. 我正在使用Spring-Data-MongoDB声明@Document There is one particular document that is giving me problems. 有一个特定的文件给我带来了问题。 Below is an example the problematic document that has a field of the type java.lang.Class . 下面是一个有问题的文档示例,该文档的字段类型为java.lang.Class

@Document
public class Value {
    @Id
    private Class<? extends I> clazz;
    private Integer value;
    ...
}

The error that I get is below. 我得到的错误如下。

Caused by: java.lang.SecurityException: Can not make a java.lang.Class constructor accessible

My current solution is to use a String instead of Class . 我当前的解决方案是使用String而不是Class I would appreciate any help on solving this issue of not being to use fields that are of the type Class . 对于解决不使用Class类型字段的问题,我将不胜感激。

  1. Why would you want to store the Class like that and not just the name? 为什么要这样存储Class而不仅仅是名称?
  2. You could add a converter to convert the Class to a String when you save it and back to a Class when you load your entity. 您可以添加一个转换器,以在保存类时将其转换为字符串,并在加载实体时将其转换为类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以有一个班级字段吗 <?> 类型必须扩展另一个类和一个接口? - Can I have a class field that is the Class<?> of a type that must extend another class and an interface? 我们是否可以避免将所有字段映射到 springdata 中的实体类以进行弹性搜索,因为我在 json 文档中有 100 多个字段? - Can we avoid mapping all the fields to entity class in springdata for elasticsearch, as I have more than 100 field in the json document? 父 class 可以有一个字段,其通用类型是当前后代的字段吗? - Can a parent class have a field, whose generic type is that of the current descendant? JavaPoet:如何添加类型为class的字段,但是我只有类名,而没有类? - JavaPoet : How to add a field of type class , but I have only the class name, not the class? 使用Firestore,如何使用java类序列化文档中的引用类型字段? - With Firestore, how do I serialize a reference type field in a document with a java class? 我班上怎么有两个相同类型的对象? - How can I have 2 objects of the same type in my class? 一个类可以拥有自己类型的字段吗? - Is it okay for a class to have a field of its own type 如何为动态类型与声明的类型不同的类字段编写吸气剂? - How can I write a getter for a class field whose dynamic type is different from declared type? 我们可以有一个泛型的类类型吗? - Can we have a class type for a generic? 我可以从未被子类覆盖的方法内部访问子对象的字段吗? - Can I have access to a field of a child object from inside a method which is not overridden by the child class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM