-
Notifications
You must be signed in to change notification settings - Fork 617
Feature request: allow more entity mapping customization #2640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What do you want to change there. Maybe we can figure out an alternative approach for your situation. |
In this class, I would like to to overwrite |
I tried several ideas based on your problem description. One option I would like to discuss is the use of projections (https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#projections): |
First of all, thanks for trying
Do you mean I could not add sth like this in my project (definitely yes)? Or do you mean that it would not be possible to add sth like this to this repo? Because that would be exactly what I planned to do if the mapping context would not be final. You can find a prototype implementation of that here (relevant commit). Of course, if I would do that by overwriting, I would need to wrap the generated PersistentProperty to "overwrite"
I actually thought of this. ByteBuddy calls this "type rebasing", I could do that before the classes are passed to the MappingContext by providing a bean which does this. However, I like to not modify existing classes using bytecode manipulation (to me, this is just asking for weird behavior), and (sadly) it was a team decision to not go for that. Regarding the use of projections: |
I don't understand @meistermeier why are you saying As far as I understand @nk-coding it is the fact that they want to not annotate things even more but add a filter to the mapping context. It basically is not about overriding the mapping context but adding machinery to |
Let me clarify a bit what I meant / want: |
Yes, exactly. It was only about |
…ties. This change introduces the concept of `PersistentPropertyCharacteristics` and `PersistentPropertyCharacteristicsProvider`. The latter can be registered implicitly as a `@Bean` with the mapping context or via a custom mapping context. It allows checking the properties and their owner for indicators like type and so on to treat them as transient or read only. Examples have been added to the `Neo4jMappingContextTest` and `PersistentPropertyCharacteristicsIT`. This closes #2640.
I have added a PR with some ideas. Would that work for you, @nk-coding ? @Bean
public PersistentPropertyCharacteristicsProvider persistentPropertyCharacteristicsProvider() {
return (property, owner) -> {
if (property.getType().equals(Double.class)) {
return PersistentPropertyCharacteristics.treatAsTransient();
}
return PersistentPropertyCharacteristics.useDefaults();
};
} another example: @Test
void characteristicsShouldBeApplied() {
Neo4jMappingContext neo4jMappingContext1 = Neo4jMappingContext.builder().withPersistentPropertyCharacteristicsProvider((property, owner) -> {
if (owner.getUnderlyingClass().equals(UserNode.class)) {
if (property.getName().equals("name")) {
return PersistentPropertyCharacteristics.treatAsTransient();
} else if (property.getName().equals("first_name")) {
return PersistentPropertyCharacteristics.treatAsReadOnly();
}
}
if (property.getType().equals(ConvertibleType.class)) {
return PersistentPropertyCharacteristics.treatAsTransient();
}
return PersistentPropertyCharacteristics.useDefaults();
}).build();
Neo4jMappingContext neo4jMappingContext2 = Neo4jMappingContext.builder().build();
Neo4jPersistentEntity<?> userEntity = neo4jMappingContext1.getPersistentEntity(UserNode.class);
assertThat(userEntity.getPersistentProperty("name")).isNull(); // Transient properties won't materialize
assertThat(userEntity.getRequiredPersistentProperty("first_name").isTransient()).isFalse();
assertThat(userEntity.getRequiredPersistentProperty("first_name").isReadOnly()).isTrue();
Neo4jPersistentEntity<?> entityWithConvertible = neo4jMappingContext1.getPersistentEntity(EntityWithConvertibleProperty.class);
assertThat(entityWithConvertible.getPersistentProperty("convertibleType")).isNull();
entityWithConvertible = neo4jMappingContext2.getPersistentEntity(EntityWithConvertibleProperty.class);
assertThat(entityWithConvertible.getPersistentProperty("convertibleType")).isNotNull();
} |
Yes, that would work for me perfectly. |
…ties. (#2645) This change introduces the concept of `PersistentPropertyCharacteristics` and `PersistentPropertyCharacteristicsProvider`. The latter can be registered implicitly as a `@Bean` with the mapping context or via a custom mapping context. It allows checking the properties and their owner for indicators like type and so on to treat them as transient or read only. Examples have been added to the `Neo4jMappingContextTest` and `PersistentPropertyCharacteristicsIT`. This closes #2640.
…ties. (#2645) This change introduces the concept of `PersistentPropertyCharacteristics` and `PersistentPropertyCharacteristicsProvider`. The latter can be registered implicitly as a `@Bean` with the mapping context or via a custom mapping context. It allows checking the properties and their owner for indicators like type and so on to treat them as transient or read only. Examples have been added to the `Neo4jMappingContextTest` and `PersistentPropertyCharacteristicsIT`. This closes #2640.
Our pleasure, @nk-coding I added it from 6.3.x onwards, releases are planned for January 13th (2021.2.7 and 2022.0.1). Snapshots will be available by the end of the day I guess. If there's stuff missing, there's a bit of time slot to still add them. |
Hi,
I wanted to ask if there is any chance to mark the
Neo4jMappingContext
as not final.I'm asking as I have a use case where I need to modify the creation of the PersistentEntity slightly, and this would be doable by subclassing the mapping context.
I can totally understand if you don't want to support this, however as there is really no way around this, I though I can at least give it a try asking.
The text was updated successfully, but these errors were encountered: