How to get entity relation with annotations using test class?

I have created a Spring -boot simple application with two objects both Company.javaand User.java. These two are related @OneToMany. And I have a test file for generating a typescript file with printing these two entity attributes. Here is my test case.

@Inject
RepositoryRestMvcConfiguration configuration;

@Test
public void getEndPoints() {
    configuration.resourceMappings().forEach(c -> {

        String className = c.getDomainType().getName();
        try {
            Class<?> entityClass = Class.forName(className);
            Field[] fields = entityClass.getDeclaredFields();

            File tsClassDir = new File("data/tsClass");
            File tsClass = new File(tsClassDir, entityClass.getSimpleName() + ".ts");

            if (!tsClass.getParentFile().exists()) {
                tsClass.getParentFile().mkdirs();
            }

            tsClass.createNewFile();

            String code = "export interface " + entityClass.getSimpleName() + "{\n";

            for (Field field : fields) {
                try {
                    NotNull notNullAnnotation = field.getDeclaredAnnotation(NotNull.class);
                    Class<?> filedClass = Class.forName(field.getType().getName());
                    if (notNullAnnotation == null){
                        code += "\t" + field.getName() + "?: " + filedClass.getSimpleName().trim() + ";" + "\n";
                    }else{
                        code += "\t" + field.getName() + ": " + filedClass.getSimpleName().trim() + ";" + "\n";
                    }                       

                } catch (Exception e) {
                    // TODO: handle exception
                }

                // System.err.println(field.getName());
            }

            code += "}";

            Files.write(tsClass.toPath(), code.getBytes());

            System.err.println(code);
        } catch (Exception e) {
            // TODO: handle exception
        }
    });
}

After a test run, I got the result below.

export interface User{
  userName: String;
  password: String;
  email: String;
  company?: Company;
}
export interface Company{
  name: String;
  email: String;
  users?: Set;
}

But I need to print that Companyand Useris related @OneToManyto the typescript file. How to do it?

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1666452/


All Articles