Skip to content

DATACMNS-1699 - Add Embedded annotation. #480

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

Closed
wants to merge 2 commits into from

Conversation

christophstrobl
Copy link
Member

Add core annotation for embedded object support and consider a potential prefix when using FieldNamingStrategies.

The actual store specific implementation is done in the individual modules that make sure the mapping context provides means to detect embedded types and provide this information to the PersistentPropertyPathFactory now using PersistentEntitiesinstead of theTypeInformation` to construct mapped paths.

Add core annotation for embedded object support and consider a potential prefix when using FieldNamingStrategies.

The actual store specific implementation is done in the individual modules that make sure the mapping context provides means to detect embedded types and provide this information to the PersistentPropertyPathFactory now using PersistentEntities instead of the TypeInformation to construct mapped paths.
*/
default boolean isNullable() {

return (isEmbedded() && findAnnotation(Embedded.class).onEmpty().equals(OnEmpty.USE_NULL))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a property is not embedded, it is never nullable. If that is intended, it should be described in the documentation and the superfluous parens around the first && term should be removed.

Or the expression should get fixed. Also we should have tests for the not embedded case.

@dcominottim
Copy link

dcominottim commented Jan 13, 2021

It'd be really nice if we could further customize this annotation with more parameters or pair it with another annotation to achieve the same effect from @Embedded + @AttributeOverride from javax.persistence. This way, the name of the inline fields could be customized in/by the parent object and the embeddable objects could be more re-used. (Real world example: I need such customization to create [DDD] Value Objects that can be re-used in/by different entities/documents when using MongoDB.)

@mp911de
Copy link
Member

mp911de commented Jan 13, 2021

Embedded objects are conceptually stable regarding their property to field/column/… names. @Embedded allows you to provide a prefix to customize physical naming. An example use-case:

class Address {
  String street, city;
}

class Order {

  @Embedded(prefix = "shipping_")
  Address shipping;

  @Embedded(prefix = "billing_")
  Address billing;
}

Rendering fields shipping_street, shipping_city, billing_street, and so on.

@dcominottim care to elaborate how else you'd like to customize field names?

@dcominottim
Copy link

dcominottim commented Jan 13, 2021

Embedded objects are conceptually stable regarding their property to field/column/… names. @Embedded allows you to provide a prefix to customize physical naming. An example use-case:

class Address {
  String street, city;
}

class Order {

  @Embedded(prefix = "shipping_")
  Address shipping;

  @Embedded(prefix = "billing_")
  Address billing;
}

Rendering fields shipping_street, shipping_city, billing_street, and so on.

@dcominottim care to elaborate how else you'd like to customize field names?

Thanks for the reply, @mp911de. I had overlooked the 'prefix' param. It seems to work mostly fine for embedded objects that aggregate multiple properties (such as your Address example). I think it should often work for value objects that encapsulate a single property as well (e.g.: class CompanyName { String companyName; }), although it'd still be useful for the parent to be able to fully override that -- in JPA, it'd be

class Company {
  @Embedded
  @AttributeOverride(
    name = "companyName", // name of the target attribute
    column = @Column(name = "CMP_NAME") // name that will be used for persistence
  )
  CompanyName name;
}

Think of a scenario in which the company you work for has complex rules for data modeling and the data governance team may request you to use specific column/attribute names for certain tables/documents -- and they don't play well with Java/Spring Data attribute naming conventions. I am in such scenario right now and need to use @AttributeOverride in JPA projects with strong object orientation/encapsulation/DDD patterns.

On a separate note, does Spring Data fully support underscore in attribute names nowadays (as per your prefix example)? It didn't use to. Sorry if too obvious. #126

@oooplz
Copy link

oooplz commented Jan 20, 2021

Thank you for open source contribution. The feature still be watched?

@mp911de mp911de added the status: superseded An issue that has been superseded by another label Feb 8, 2021
@mp911de
Copy link
Member

mp911de commented Feb 8, 2021

Superseded by #2294.

@mp911de mp911de closed this Feb 16, 2021
@mp911de mp911de deleted the issue/DATACMNS-1699 branch May 17, 2023 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants