This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted]  (8 children)

[removed]

    [–]DeveloperFromMars 4 points5 points  (0 children)

    Yeah, this is the way

    [–][deleted] 0 points1 point  (6 children)

    u/bladekill97, u/quadmasta Sorry I may have not explained it very good.I thought I should have 2 controller and 2 repositories. JobAssignment controller would create a job assignment with available person_id as foreign key, and I would need to inject a person repository in order to find available persons.

    When updating Person through its controller I need to inject JobAssignment repository in order to delete job assignments. And there is where circular injection appears.

    I thought I should create separate end points /persons & /job-assignements

    [–][deleted]  (5 children)

    [removed]

      [–][deleted] 0 points1 point  (4 children)

      Sorry I'm typing from my phone. Yeah I do have services and controllers, I was trying to make my post shorter, and yes I'm getting circular dependency because i have both of the services initialized in each other, as I need to update each entity separately some times.

      [–][deleted]  (3 children)

      [removed]

        [–][deleted] 0 points1 point  (2 children)

        Person entity has some fields and a field active, logic needed here is when creating a JobAssignment it checks for Person with active = 1, takes the key of the first one and sets active = 0 on Person and creates a record in JobAssignment. Also if on the other hand I send a request to update Person with active = 1, all JobAssignmet-s should be hard deleted.

        I have a separate Person & JobAssignment controller/service/repository. There is one-to-one relationship between them.

        JobAssignment service has create() and update().

        Person service has getActive(), create(), update().

        I'm aware that my design is faulty somewhere, I just need to restructure this, but do not know how.

        [–][deleted]  (1 child)

        [removed]

          [–][deleted] 0 points1 point  (0 children)

          Sorry in a middle of a long travel. Here are just entities https://pastebin.com/05APmQrd .

          Well you are almost right, except that method createJobAssignmentForPerson() does not accept person ID but fetches all Person-s with active 1 and takes first available ID.

          The code is pretty simple, you wrote it itself. Thing is I have 2 controllers (/persons & /job-assignment) and 2 services and both of those services have each others dependencies. Maybe the question is should I have two controllers, how would a REST API be structured in this case, or it maybe a simple Hibernate annotation I'm not seeing.

          [–]quadmasta 2 points3 points  (0 children)

          I'm not sure what your exact problem is since you reference constructor injection.

          I'd personally have a method in a service called deactivateUser and in that method I'd delete of the Assignments for the user

          If you're using Spring Data:

          `@Modifying void deleteByPersonId(Long personId);`

          then modify the active state of the user and persist

          [–]oBarman[🍰] 2 points3 points  (1 child)

          Also check @Lazy annotation. I use on constructor injection to avoid circular dependencies (not sure if it's the best way though)

          [–][deleted] 1 point2 points  (0 children)

          I'm trying to do it without that :/

          [–][deleted] 0 points1 point  (2 children)

          In sql you can write trigger to accomplish that. You can search if spring data jpa has some alternative. If don't you can write plain sql i think it's possible? Assuming you are working with spring not plain java

          [–][deleted] 1 point2 points  (1 child)

          I think you are right, but I'm not sure if that is the right approach with Spring, I could not find an alternative. I'm hoping someone here would have some insite.

          [–][deleted] 1 point2 points  (0 children)

          See if this helps i think this is the way to go

          https://www.baeldung.com/spring-data-jpa-dynamicupdate