Skip to content

Add support for embedded entities #288

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

Open
jackdpeterson opened this issue Jan 25, 2020 · 9 comments
Open

Add support for embedded entities #288

jackdpeterson opened this issue Jan 25, 2020 · 9 comments
Labels
type: enhancement A general enhancement

Comments

@jackdpeterson
Copy link
Contributor

jackdpeterson commented Jan 25, 2020

I'm looking at implementing a read and write converters for an embedded object in a row that may exist in two forms.

The specific example is a MonetaryAmount that contains two fields (amount, currency code) in order to properly generate a Monetary Amount. The parent object's row contains two instances of this object differed by its prefix (e.g., MSRP, and say Shipping Cost, could be more but just for keeping things simple in this issue, let's assume that there are two instances of this type in the row). Is there a way to annotate an object similar to how JPA supports embeddables w/ a prefix declaration w/ a converter to distinguish the fields?

Sample Entity:

// some imports
import javax.money.MonetaryAmount;
/// other imports

public class Product {

    @Id
    @GtinConstraint
    @Getter
    private String id;

    private String title;

    private String description;

    private MonetaryAmount listPrice;

    private MonetaryAmount shippingCost;
}
@jackdpeterson jackdpeterson changed the title How to convert an embedded object from a row? How to convert multiple embedded object of the same type from a row? Jan 25, 2020
@jackdpeterson jackdpeterson changed the title How to convert multiple embedded object of the same type from a row? How to convert multiple embedded objects of the same type from a row? Jan 25, 2020
@mp911de
Copy link
Member

mp911de commented Jan 25, 2020

We don’t support embeddables yet. It would make sense to add support for these.

@jackdpeterson
Copy link
Contributor Author

jackdpeterson commented Jan 27, 2020

Looking at the Converter<Row, T> implementation I don't see a good spot for the converter to be column-aware. Perhaps this kind of logic would be most appropriate in the ResultProcessor?

At the moment, my workaround is to keep my "entities" (Really, data bags given the lack of rich behavior / complex mapping support) as flat as possible and then have a separate DTO w/ a static factory method to enrich the output of REST API's output w/ the upstream flat objects pre-assembled into their richer structure as part of a pipeline for final output. This feels very brittle and sub-optimal in terms of building a sane business process, but I guess that's the current state of things. I'd definitely like to see some kind of embeddable mapping in-place so less data transformation steps need to be managed by the end-programmer. Definitely looking forward to some opinions / a direction or perhaps a better workaround than I'm doing. Thanks for the great repo, I know this is a lot of work to build and we're not quite at 1.0.0 =)

@mp911de
Copy link
Member

mp911de commented Jan 28, 2020

The Row mapping function accepts a Row and can return anything: A scalar value (i.e. String, Long, …), a Map (as in ColumnMapRowMapper) or a complex object. Spring Data R2DBC is using the mapping function to materialize an object from the Row.

Following the embeddable idea, you can represent nested objects in a flat row by using columns with prefixes. That's how Spring Data JDBC provides @Embedded support.

Embedded entity support requires a read- and a write-part to properly work. Adding this kind of functionality requires a lot of decoration if that aspect is handled outside of the Spring Data R2DBC bits. Likely, we will have to extend DefaultReactiveDataAccessStrategy and MappingR2dbcConverter.

@mp911de mp911de changed the title How to convert multiple embedded objects of the same type from a row? Add support for embedded entities Jan 28, 2020
@mp911de mp911de added the type: enhancement A general enhancement label Jan 28, 2020
@hantsy
Copy link

hantsy commented May 11, 2022

The official document includes an embedded example( Person address is an embedded Address) in the projection,https://docs.spring.io/spring-data/r2dbc/docs/1.4.x/reference/html/#projections, but I tried to use a Record to reproduce it in my example project(updated to Spring Data R2dbc 1.5.0-RC1), it does not work, see: https://github.com/hantsy/spring-r2dbc-sample/blob/master/bookstore/src/test/java/com/example/demo/RepositoryTest.java#L113

@Mannclaudiu
Copy link

Mannclaudiu commented May 25, 2022

Hello. Recently, I had to deal with embedded entities in the context of applying DDD principles. I wanted to store value objects in entity tables, and I solved it by implementing WritingConverter and ReadingConverter.
Maybe my comment will be useful for someone who want to achieve same thing (I was browsing the internet for days looking for an idea). If anyone is interested, I can provide instructions.

Is interesting that while using webflux, we still have the annotation org.springframework.data.relational.core.mapping.Embedded, but the Embeddable does not exist. I do not see the purpose of Embedded as standalone.

@geliotropv
Copy link

Greetings, I ran into the same problem with embedded, you can get around it with the help of converters, but I want to know if support is planned in the near future or will it not be?

@mp911de
Copy link
Member

mp911de commented Jul 6, 2022

In general, we want to support embeddables with R2DBC, however, we are currently busy with other things.

@holmofy
Copy link

holmofy commented May 5, 2023

When can @Embedded be implemented

@fengyutingg
Copy link

Hello. Recently, I had to deal with embedded entities in the context of applying DDD principles. I wanted to store value objects in entity tables, and I solved it by implementing WritingConverter and ReadingConverter. Maybe my comment will be useful for someone who want to achieve same thing (I was browsing the internet for days looking for an idea). If anyone is interested, I can provide instructions.

Is interesting that while using webflux, we still have the annotation org.springframework.data.relational.core.mapping.Embedded, but the Embeddable does not exist. I do not see the purpose of Embedded as standalone.

hi~ how to work around by acheive WritingConverter and ReadingConverter? could you please give me some examples? thanx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

7 participants