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...
-
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– Jens Schauder2022年10月05日 14:02:07 +00:00Commented Oct 5, 2022 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– user10328362022年10月06日 16:16:43 +00:00Commented Oct 6, 2022 at 16:16
-
I put your comment into your question, because I think it is very important to it.Jens Schauder– Jens Schauder2022年10月07日 06:56:20 +00:00Commented Oct 7, 2022 at 6:56
3 Answers 3
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.
Comments
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);
Comments
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.