2

I have an annotated entity object with custom table and field names which i use with Spring Data JDBC (not JPA). Smth like:

@Data
@Table("custom_record_table")
public class Record {
    @Id
    @Column("id_field")
    Long id;
    String name;
    @Column("name_short")
    String shortName;
}

I'd like to get a map of properties to fields. Smth like:

{"id":"id_field","name":"name","shortName":"name_short"}

What's the proper way to get it?

For context: I plan to use this map to construct queries to load many-to-one refs along with main table. Now I get this map with plain reflections API scanning for fields and their annotations. It works, but I am feeling like inventing a bicycle...

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
user1032836
  • 55
  • 1
  • 7
  • Could you give more context about why you want this in map form? I'm asking because the only way I currently see is rather hacky. – Jens Schauder Oct 05 '22 at 14:02
  • I plan to use this map to construct queries to load many-to-one refs along with main table. Now i get this map with plain reflections api scanning for fields and their annotations. It works, but i am feeling like inventing a bicycle... – user1032836 Oct 06 '22 at 16:16
  • I put your comment into your question, because I think it is very important to it. – Jens Schauder Oct 07 '22 at 06:56

3 Answers3

2

What you are looking for is the JdbcMappingContext. It should be available as a bean, so you can simply autowire it in your application.

JdbcMappingContext mappingContext // ... autowired

Map<String, String> propToCols = new HashMap<>();
mappingContext.getRequiredPersistentEntity(Record.class).forEach(
    rpp -> propToCols.put(rpp.getName(), rpp.getColumnName().getReference() 
);

I wrote this without IDE so it will contain mistakes. There are also special things to consider when you have references and stuff, but this should get you started.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

What if you create a static function inside Record?

@Data
@Table("custom_record_table")

public class Record 
{
    @Id
    @Column("id_field")
    Long id;
    String name;
    @Column("name_short")
    String shortName;

   static Map<String, String> getMap()
   {
       return Map.ofEntries  ( 
                 Map.entry("id", "id_field"),
                 Map.entry("name", "name"),
                 Map.entry("shortName","name_short")
              );
   }

  /* returning map would look like: 
     {"id":"id_field","name":"name","shortName":"name_short"} */
}

Note that this would get you a Map, in which you can retrieve the field values by the field keys you know.

map.get("id") will return id_field, and so on. This may be useful when you need to know which is the name that references the fields.

BUT, if you just want an String representation, even a JSON, it would be best to create a JSON from those fields.

Anyway, if I understood correctly, you want a Map object, so the first solution should do it.

aran
  • 10,978
  • 5
  • 39
  • 69
  • I suspect, spring data already has a way to get such map from annotations and configured naming conventions thus it's able to generate sql from that info. Separate map is an easy way, but then you should maintain the same info in two places. – user1032836 Oct 05 '22 at 07:27
0

you want to convert your objects to JSON. they are different libraries for it. people prefer Jackson library the most

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.ObjectWriter; 

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
Praneeth Kumar
  • 284
  • 1
  • 15