Skip to content
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

Store collections and arrays of simple types in an ARRAY column [DATAJDBC-259] #482

Closed
spring-projects-issues opened this issue Sep 8, 2018 · 4 comments
Assignees
Labels
in: core type: enhancement

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 8, 2018

Thorben Janssen opened DATAJDBC-259 and commented

When I try to persist a Book with a Set of Ids of the Author aggregate, Spring Data JDBC tries to store all ids in the authors column instead of an association table.

public class Book {public class Book {
    @Id private Long id;
    private String title;
    private LocalDate publishingDate;
    private Integer numPages;
    private Set<Long> authors;
 
    // getter and setter methods
}

 

Executed INSERT statement

2018-09-08 14:14:11.190 DEBUG 9232 --- [ main] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [INSERT INTO book (title, publishing_date, num_pages, authors) VALUES (?, ?, ?, ?)]

 


Affects: 1.0 RC2 (Lovelace)

Issue Links:

Referenced from: pull request #113

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 11, 2018

Maciej Walkowiak commented

Did you consider supporting mapping collections like this into array or JSON fields for databases that support it (for example Postgres)? 

 

Maybe there is already a way to do it but I just cannot find it

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 11, 2018

Jens Schauder commented

There is currently no JSON support. I so far I didn't really put thought into that. Mostly because I don't really understand how people use JSON with relational databases. If you have an idea, I'd be eager to hear it

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 11, 2018

Maciej Walkowiak commented

I think arrays are more often used than JSON in case of Postgres. 

I used JSON fields to store original responses from 3rd party service. Keeping it as JSON instead of serialising to string gives an option to query it with SQL.

Another usage - I am not sure how popular is it - used to be promoted by Vaughn Vernon - https://webcache.googleusercontent.com/search?q=cache:9eW9xJ_GOVsJ:https://vaughnvernon.co/%3Fp%3D942+&cd=1&hl=en&ct=clnk&gl=de

The article is gone, so I am not sure if he still stands for it ;)

 

If I am not mistaken, choosing if for example list of strings should be saved to separate table or to an array field could be determined by an annotation once DATAJDBC-239 is implemented?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jan 23, 2019

Jens Schauder commented

I thought about this a little and came to the following conclusion:

Collections (i.e. List, Set and arrays) of simple types (primitives, boxed types, String, Date ...) should be mapped to a single column of type array.

It might be useful to offer an annotation to store it in a separate table but that would be something for a separate issue. As a workaround one can always create a simple, wrapped entity

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

No branches or pull requests

2 participants