Delete of an entity in DB with spring

0

Good afternoon, I have an information system with a DB in Mysql. The connection with her and the mapping was made with Spring.

I am creating the services and I have a very important question. I put in context, I have two Ranger Entities and Curriculum, with a 1: 1 bidirectional relationship. When I delete an instance of Curriculum in the DB, I verify that this instance does not exist, however the Ranger curriculum attribute has not been set to null, but it still has the previously deleted curriculum. Should not spring do it on its own? Do I have to manually do a ranger.setCurriculum = null?

I leave the delete code:

public void delete(final Curriculum curriculum) {
    Ranger principal;
    Curriculum result;
    Assert.isTrue(curriculum.getId() != 0);
    principal = this.rangerService.findByPrincipal();
    Assert.notNull(principal);
    result = this.findCurriculumByPrincipal();
    Assert.notNull(result);
    Assert.isTrue(curriculum.equals(result));
    this.curriculumRepository.delete(curriculum);
    principal.setCV(null);
}

The findCurriculumByPrincipal takes the curriculum of the logged Ranger from the DB. I know that maybe it is not enough information but it is a conceptual doubt, I want to know if spring clears the Ranger FK when deleting the Curriculum

I have done a test with JUnit for the delete that succeeds when in the delete method I put the principal.setCV () = null;

@Test
public void testDeleteCurriculumOfPrincipal() {
    Curriculum result;
    super.authenticate("ranger1");
    result = this.curriculumService.findCurriculumByPrincipal();
    Assert.notNull(result);
    this.curriculumService.delete(result);
    result = this.curriculumService.findCurriculumByPrincipal();
    Assert.isNull(result);
    super.unauthenticate();

}
    
asked by Ray 11.11.2017 в 20:37
source

1 answer

0

First make sure that you do not rollback your tests, so that what you do is reflected in BBDD.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ClassCofigurationInCaseIsNeeded.class)
@Transactional
@Rollback(false) ->> Para ver cambios en BBDD "No hagas rollback despues del test"
public class TestCustomName {...}

On the other hand you have to have a correct JPA mapping in your entities, for example for a bidirectional 1: 1 - > B has an FK to A:

public class A{ 
    @Id
    @Column(name="A_ID")
    private Long id; 

    // Asegúrate de que el NO-Propietario de la FK (A) tiene CascadeType.ALL
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "a")
    private B b; 

    ...
}
public class B{  
    @Id
    @Column(name="B_ID")
    private Long id;  

    @OneToOne
    @JoinColumn(name = "B_ID", referencedColumnName = "A_ID")
    private Lead lead; 

    ...
}

By correctly inserting your cascade all (it does not have to be all there can be exclusive only to deletions) you should get the value of null that you expect in BBDD. I hope this helps you with your problem. Greetings.

    
answered by 21.12.2017 в 09:53