简体   繁体   中英

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

I want to persist a document to MongoDB. I'm using Spring-Data-MongoDB to declare @Document s. 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 .

@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 . I would appreciate any help on solving this issue of not being to use fields that are of the type Class .

  1. Why would you want to store the Class like that and not just the name?
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM