GoogleCloudPlatform / GoogleCloudPlatform/spring-cloud-gcp
Override field name in subclass
- Dominant language
- Java
- Stars
- 551
- Forks
- 349
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 14
Description
**Describe the solution you'd like**
We have 2 kinds under one namespace, one is `cat`, one is `dog`. In the cat kind, there's a column `catName` and in the dog kind, there's a column `dogName`.
In the java code, we have 3 classes.
```
class Animal {
String name;
}
```
```
class Dog extends Animal {}
```
```
class Cat extends Animal {}
```
For now, when we call `datastoreTemplate.save(cat)`, the `name` column in datastore will be `name`. We would like to achieve this: when using `datastoreTemplate.save(cat)`, the `name` field will become `catName` in datastore.
From the doc, we found an annotation `@Field(name="catName")` that can customize the field name. However in this case, the annotation cannot work in the subclass, since we won't hide the fields in the superclass.
```
class Cat extends Animal {
@Field(name = "catName")
String name; // not acceptable
}
```
We don't find any annotation that can be used on `getter()`. We think it'd better if the java client can achieve something like
```
class Cat extends Animal {
@Override
@Field(name = "catName")
public String getName() {
return super.getName();
}
}
```
I wonder if this can be a feature in the future? Or is there any solution for this case? Thanks a lot!
Contributor guide
Research direction
No repository file, test, or entry point is named. Start by locating the Java datastore field-mapping support for inherited fields and the @Field annotation, then determine whether subclass-specific names are supported; done should include a defined mapping behavior and coverage for Cat and Dog without redeclaring the superclass field.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100