简体   繁体   English

为什么这个简单的删除方法在 springBoot 项目中不再起作用?

[英]why is this simple delete method won't work anymore in springBoot project?

So I have this project I'm working on for academic purpose, we are asked to add Log4j and JUnit tool to it .所以我有一个我正在从事学术目的的项目,我们被要求向其中添加 Log4j 和 JUnit 工具。 So After I added the log4j lines and tested the delete method at first it was working but then it won't delete anything anymore.因此,在我添加 log4j 行并首先测试删除方法后,它可以正常工作,但随后将不再删除任何内容。

it would be great if someone could help me detect the problem because I tried changing the code but still the deleting isn't happening.如果有人可以帮助我检测问题,那就太好了,因为我尝试更改代码但仍然没有删除。

so this is my service '''所以这是我的服务'''

  @Service
    public class DepartementServiceImpl implements IDepartementService {
    
        
        @Autowired
        DepartementRepository deptRepoistory;
        
        private static final Logger l = LogManager.getLogger(DepartementServiceImpl.class);
    
         @Transactional
    
        public int deleteDepartementById(int depId) {
            try {
              l.info("In deleteDepartmentById  :  ");
              
              l.info(" department id= " + depId);
              
              deptRepoistory.delete(deptRepoistory.findById(depId).get());
            
              l.info("Out of deleteDepartmentById.  ");
               return 0 ; 
              
            } 
            catch (Exception e) {
                
                l.error("erreur In deleteDepartementById() : could not be found " + e); 
 return -1 ; }
        }
            
        
        }

''' '''

and this is the testing class这是测试类

''' '''

 @RunWith(SpringRunner.class)

  @SpringBootTest

  public class DepartementTest {

@Autowired
IDepartementService DepService; 

@Autowired
DepartementRepository deptRepoistory;  
@Test
public void testDeleteDepartementById(){
    int depId = 30 ;
        
        DepService.deleteDepartementById(depId);
        
        assertNull(DepService.getDepartmentById(depId));
    
    

}

 }

''' '''

and this is the repository '''这是存储库'''

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import tn.esprit.spring.entities.Departement;

@Repository
public interface DepartementRepository extends CrudRepository<Departement, Integer> {

 }

''' '''

and this what console show when I run it as JUnit test :当我将它作为 JUnit 测试运行时,控制台显示的是:

''' '''

2020-nov.-02 10:48:09.237  [32m[INFO ][0;39m [36mtn.esprit.spring.DepartementTest[0;39m -    [34mStarted DepartementTest in 4.861 seconds (JVM running for 5.623)[0;39m 
2020-nov.-02 10:48:09.358  [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m -  [34mIn deleteDepartmentById  :  [0;39m 
2020-nov.-02 10:48:09.368  [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m -  [34m department id= 30[0;39m 
Hibernate: select departemen0_.id as id1_1_0_, departemen0_.entreprise_id as entrepri3_1_0_, departemen0_.name as name2_1_0_, entreprise1_.id as id1_4_1_, entreprise1_.name as name2_4_1_, entreprise1_.raison_social as raison_s3_4_1_ from departement departemen0_ left outer join entreprise entreprise1_ on departemen0_.entreprise_id=entreprise1_.id where departemen0_.id=?
Hibernate: select departemen0_.entreprise_id as entrepri3_1_0_, departemen0_.id as id1_1_0_, departemen0_.id as id1_1_1_, departemen0_.entreprise_id as entrepri3_1_1_, departemen0_.name as name2_1_1_ from departement departemen0_ where departemen0_.entreprise_id=?
2020-nov.-02 10:48:09.458  [32m[INFO ][0;39m [36mtn.esprit.spring.services.DepartementServiceImpl[0;39m -  [34mOut of deleteDepartmentById.

''' '''

and this is the JUnit results which is right because the delete didn't happen, because it is supposed to find it null after deleting but the supposed deleted object is still there.这是 JUnit 结果,这是正确的,因为删除没有发生,因为它应该在删除后发现它为空,但假定的已删除对象仍然存在。

''' '''

java.lang.AssertionError: expected null, but was:<tn.esprit.spring.entities.Departement@2c1a8529>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotNull(Assert.java:755)
    at org.junit.Assert.assertNull(Assert.java:737)
    at org.junit.Assert.assertNull(Assert.java:747)
    at tn.esprit.spring.DepartementTest.testDeleteDepartementById(DepartementTest.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

''' '''

My Entity我的实体

''' '''

@Entity
public class Departement implements Serializable {

    private static final long serialVersionUID = -357738161698377833L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    
    private String name;
    
    //@JsonManagedReference 
    @JsonIgnore
    @ManyToMany
    private List<Employe> employes;
    
    @OneToMany(mappedBy="departement")
    private List<Mission> missions;
    
    @ManyToOne
    private Entreprise entreprise;

    public Departement() {
        super();
    }
    
    public Departement(String name) {
        this.name = name;
    }
    
    

}

''' '''

Update更新

So I Added new department without assigning an enterprise to it and the delete is back to work so I assume that the problem lays with the association between the Departement Entity and the Enterprise Entity所以我添加了新部门而不分配企业,删除又恢复工作,所以我认为问题出在部门实体和企业实体之间的关联上

Entreprise Entity企业实体

''' '''

@Entity
public class Entreprise implements Serializable{
    
    /**
     * 
     */
    private static final long serialVersionUID = 3152690779535828408L;

    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    
    private String name;
    
    
    private String raisonSocial;
    
    @OneToMany(mappedBy="entreprise", 
            cascade = {CascadeType.PERSIST, CascadeType.REMOVE}, 
            fetch=FetchType.EAGER)
    private List<Departement> departements = new ArrayList<>();
....
}

''' '''

UPDATE :更新 :

So this problem is cleared , I just added another method to remove the entreprise from the Department( disaffectDepartfromEnt) and the delete worked perfectly after that所以这个问题解决了,我只是添加了另一种方法从部门中删除企业(disaffectDepartfromEnt),之后删除工作完美

I tried your code and i was able to delete.我试过你的代码,我能够删除。 Although i need to remove some of your relational mapping on your Department entity.虽然我需要删除你部门实体上的一些关系映射。 Can you try to extract deptRepoistory.findById(depId).get() on a separate line(set a new variable), check/log if it returns some result?您可以尝试在单独的行上提取 deptRepoistory.findById(depId).get()(设置一个新变量),检查/记录它是否返回一些结果? You can check using 'isPresent' then do the delete call.您可以使用“isPresent”进行检查,然后进行删除调用。

Solved:解决了:

So I Added new department without assigning an enterprise to it and the delete is back to work so I assume that the problem lays with the association between the Departement Entity and the Enterprise Entity So now this problem is cleared , I just added another method to remove the entreprise from the Department( disaffectDepartfromEnt) and the delete worked perfectly after that.所以我在没有分配企业的情况下添加了新部门并且删除恢复工作所以我假设问题出在部门实体和企业实体之间的关联所以现在这个问题已经解决了,我只是添加了另一种方法来删除来自部门的企业(disaffectDepartfromEnt)和删除工作完美无缺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM