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
82.6k35 gold badges198 silver badges379 bronze badges
asked Oct 4, 2022 at 23:46
3
  • 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. Commented 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... Commented Oct 6, 2022 at 16:16
  • I put your comment into your question, because I think it is very important to it. Commented Oct 7, 2022 at 6:56

3 Answers 3

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.

answered Oct 7, 2022 at 7:12
Sign up to request clarification or add additional context in comments.

Comments

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);
answered Oct 5, 2022 at 0:13

Comments

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.

answered Oct 4, 2022 at 23:59

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.