Spring Data JDBC - Many to Many Relationships

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • Learn how to implement Many to Many relationships with Spring Data JDBC - what are the options and what are the tradeoffs.
    Additional reading:
    - spring.io/blog...
    In my channel I discuss things I believe are relevant to either Java & Spring beginners, or advanced developers who want to stay up to date. Feedback is very much welcome, either here in comments or on Twitter / maciejwalkowiak
    --
    Icons:
    Icons made by www.freepik.com from www.flaticon.com is licensed by CC 3.0 BY (creativecommons...)

Комментарии • 14

  • @davidljuba6698
    @davidljuba6698 5 лет назад +3

    "To wrap it up: Spring Data JDBC does not support many-to-one or many-to-many relationships. In order to model these, use IDs. This encourages a clean modularization of the domain model. It also removes a whole type of questions that one would have to solve and learn to reason about if such a mapping were possible."
    Good to know, from link you provide. Thanks for good content.

    • @SpringAcademy
      @SpringAcademy  5 лет назад

      Thanks David! What are your thoughts about this project? I think once Spring Data JDBC matures it will become a default option for using relational databases with Spring.

    • @davidljuba6698
      @davidljuba6698 5 лет назад +1

      @@SpringAcademy What I found problematic about JPA, is that, it has tendency to lead developers to tightly couple modules (in monolith applications).
      I mean, ability to create many-to-many or many-to-one relationships in way that is done in JPA,
      could be source of problem, аs nothing prevent you to do that way, where, at the end, persistence layer has all entities mixed,
      and all modules depends on it.
      So, I am not sure, why many-to-many or many-to-one is not currently implemented in Spring Data Jdbc,
      could be implementation complexity or strong principles behind this (DDD),
      but it seams to me that second one is reason, what I just learned from your video аnd link you provided, and I am happy because of that!
      Quote from your video: "There is issue with that approach (JPA many-to-many relationship), like circular dependency and no clear ownership between entities", sentence was most valuable for me.
      Data duplication and maintaining it is a new problem, and increases complexity again, but in more maintainable way (I think CQRS and ES are solving this problem).
      I was researching about this ("data duplication DDD") and found many interesting articles:
      "Instead of system B exposing methods for creating and updating customers
      that when used by System A would cause a synchronous dependency,
      Julies uses an event-based approach using a message queue to implement the publish-subscribe pattern.
      System A will then publish events by sending messages about created and updated customer,
      messages that system B listens to and updates its database.
      Using this technique system B will end up having a complete list of customers
      but only with a small amount of data compared to system A."
      ( www.infoq.com/news/2014/11/sharing-data-bounded-contexts )
      After all, everything I stated here is my opinion, so don't take it 100% correct.

    • @SpringAcademy
      @SpringAcademy  5 лет назад

      @@davidljuba6698 definitely Spring Data JDBC is more opinionated than JPA - I also like it, in the end it leads to better design and if one doesn't want to follow aggregate oriented model or do something hacky it's always possible to just use plain SQL query either with Spring Data JDBC or JOOQ. Btw, good modularity and following DDD principles is also possible with JPA but since it's so easy to just put @ManyToX many developers just do it without considering other options.
      Regarding data duplication - thanks for the link! CQRS and ES are nice tools to solve this problem, but it can be easily done even without introducing these patterns. Spring Data has a base class AbstractAggregateRoot that makes it super easy to publish events from entities and then forward them to a queue from which another service or even same service but different module can listen to.
      The only one correct answer to all the questions is "it depends" ;)

  • @tohidfla
    @tohidfla 5 лет назад +2

    now after couple of time watching I have understood this video :)))) I hope there will gonna be more videos like this from you.

  • @alexpishvanov736
    @alexpishvanov736 Год назад

    When cat has disappeared in just one glance I was scared )))

  • @teratoma.
    @teratoma. 3 года назад

    yugi spitting facts

  • @samm4515
    @samm4515 Год назад

    Thanks, but what kind of editor are you using? Visual Studio Code ? Or something else ?

  • @vishalsundararajan1228
    @vishalsundararajan1228 3 года назад

    or we could just drop many to many mapping and have duplicate entries in the book table with authorid as foreign key with book and authorid as combined unique constraint. Now this will basically covert a many to many to one to many

  • @kunalvarpe40
    @kunalvarpe40 Год назад

    @Spring Academy
    I have tried the above code for many to many relation for spring boot 2.7.5 it is not working as expected. It is giving error "java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: book_author". Anyone has any idea. I am not able to find any guide for newer version if anything is changed.

  • @rabinneupane5906
    @rabinneupane5906 5 месяцев назад

    How to make 1 book with multiple authors?

  • @Importunity
    @Importunity 3 года назад

    I'm wondering why you didnt use a composite key instead you used long author?

  • @JeaneAdix
    @JeaneAdix 4 года назад

    Hi, thank you for the video, in my code i do i have a many to many on a "code""tag"
    Where I use a Set in tags and a Set in codes. I use fetch type eager, but this is very slow.... I am using springJPA (persistencex) library, can I still use this trick?

  • @jesusveliz2150
    @jesusveliz2150 2 года назад

    Excelete muy genial sus vidos, tengo una consulta para la relación ManyToMany. Como harias para recuperar solos los libros que coincidan entre los autores.
    Por ejemplo del lado del libro: categoría=acción
    Se desea obtener todos los actores que hayan escrito un libro de acción.
    {
    Autores:{[
    {
    IdAutor:1,
    Nombre:"AA"
    },
    {
    IdAutor:2,
    Nombre:"BB"
    }
    ]},
    Libros{[
    IdLibro:1
    CategoríaLibro:"accion"
    ]}
    }
    Como puedes ver, quisiera obtener esa salida pero lo le ve la vuelta.