微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

因此,我有一个正在从事学术研究的项目,要求我们向其中添加Log4j和JUnit工具。 因此,在我添加了log4j行并测试了delete方法后,它开始工作了,但随后将不再删除任何内容

如果有人可以帮助我检测到问题,这是很好的,因为我尝试更改代码,但删除仍然没有发生。

这是我的服务 '''

[4019392.626796] Memory cgroup out of memory: Kill process 4178127 (apache2) score 1137 or sacrifice child
[4019392.636520] Killed process 4178127 (apache2) total-vm:143960kB,anon-RSS:22856kB,file-RSS:10472kB,shmem-RSS:28228kB

'''

这是测试班

'''

  @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 ; }
        }
            
        
        }

'''

这是存储库 '''

 @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));
    
    

}

 }

'''

这是我作为JUnit测试运行控制台时显示内容

'''

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> {

 }

'''

这是正确的JUnit结果,因为没有发生删除,因为应该在删除后发现它为null,但是假定的删除对象仍然存在。

'''

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.

'''

我的实体

'''

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)

'''

更新

所以我添加了新部门而未为其分配企业,并且删除又恢复了工作,所以我认为问题出在部门实体与企业实体之间的关联上

企业实体

'''

@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;
    }
    
    

}

'''

更新:

因此,此问题已解决,我刚刚添加了另一种方法来从Department(disaffectDepartfromEnt)删除企业,并且删除操作在此之后完美完成了

解决方法

我尝试了您的代码,因此可以删除。尽管我需要删除您在部门实体上的一些关系映射。您可以尝试在单独的行上提取deptRepoistory.findById(depId).get()(设置新变量),检查/记录是否返回一些结果吗?您可以使用“ isPresent”进行检查,然后进行删除呼叫。

,

已解决:

因此,我添加了新部门而未为其分配企业,并且删除又恢复了正常工作,因此我认为问题出在部门实体与企业实体之间的关联上 因此,现在这个问题已解决,我只是添加了另一种方法来从Department(disaffectDepartfromEnt)删除企业,然后删除操作就可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。