Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringDataJPA:为什么findOne(id)在内部执行删除查询?_Java_Spring_Hibernate_Spring Data Jpa - Fatal编程技术网

Java SpringDataJPA:为什么findOne(id)在内部执行删除查询?

Java SpringDataJPA:为什么findOne(id)在内部执行删除查询?,java,spring,hibernate,spring-data-jpa,Java,Spring,Hibernate,Spring Data Jpa,我有一个应用程序,使用@manytomy注释将两个不同的实体链接到一个父实体。我不确定我是否正确地实施了它。但当我在父实体的存储库上执行.findOne()来查找它时,首先它正确地获取数据,但同时它也在内部执行删除查询,从映射表(JPA在添加@manytomy注释后自行创建的表)中删除记录。以下是生成的SQL查询和代码段: Java类 @Entity @Table(name = "inventory") public class Inventory { @Id Long id;

我有一个应用程序,使用
@manytomy
注释将两个不同的实体链接到一个父实体。我不确定我是否正确地实施了它。但当我在父实体的存储库上执行
.findOne()
来查找它时,首先它正确地获取数据,但同时它也在内部执行删除查询,从映射表(JPA在添加@manytomy注释后自行创建的表)中删除记录。以下是生成的SQL查询和代码段:

Java类

@Entity
@Table(name = "inventory")
public class Inventory {

    @Id
    Long id;

    // many other properties

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Stream> streams;

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Subject> subjects;
}  
@Transactional
public class InventoryService {

  public Inventory getInventoryById(Integer id) {
      Inventory inv = invRepository.findOne(id);
      if(inv == null) {
            throw new NotFoundException("Entity not found id: "+id);
      }
      return inv;
  }
} 
Hibernate: select streams0_.inventory_id as inventor1_1_0_, streams0_.streams_id as streams_2_2_0_, stream1_.id as id1_6_1_, stream1_.stream as stream2_6_1_ from inventory_stream streams0_ inner join stream stream1_ on streams0_.streams_id=stream1_.id where streams0_.inventory_id=?
Hibernate: delete from inventory_stream where inventory_id=?
Hibernate: select subjects0_.inventory_id as inventor1_1_0_, subjects0_.subjects_id as subjects2_3_0_, subject1_.id as id1_7_1_, subject1_.subjectName as subjectN2_7_1_ from inventory_subject subjects0_ inner join subject subject1_ on subjects0_.subjects_id=subject1_.id where subjects0_.inventory_id=?
Hibernate: delete from inventory_subject where inventory_id=?  
控制台登录eclipse

@Entity
@Table(name = "inventory")
public class Inventory {

    @Id
    Long id;

    // many other properties

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Stream> streams;

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Subject> subjects;
}  
@Transactional
public class InventoryService {

  public Inventory getInventoryById(Integer id) {
      Inventory inv = invRepository.findOne(id);
      if(inv == null) {
            throw new NotFoundException("Entity not found id: "+id);
      }
      return inv;
  }
} 
Hibernate: select streams0_.inventory_id as inventor1_1_0_, streams0_.streams_id as streams_2_2_0_, stream1_.id as id1_6_1_, stream1_.stream as stream2_6_1_ from inventory_stream streams0_ inner join stream stream1_ on streams0_.streams_id=stream1_.id where streams0_.inventory_id=?
Hibernate: delete from inventory_stream where inventory_id=?
Hibernate: select subjects0_.inventory_id as inventor1_1_0_, subjects0_.subjects_id as subjects2_3_0_, subject1_.id as id1_7_1_, subject1_.subjectName as subjectN2_7_1_ from inventory_subject subjects0_ inner join subject subject1_ on subjects0_.subjects_id=subject1_.id where subjects0_.inventory_id=?
Hibernate: delete from inventory_subject where inventory_id=?  
更新

@Entity
@Table(name = "inventory")
public class Inventory {

    @Id
    Long id;

    // many other properties

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Stream> streams;

    @ManyToMany(fetch=FetchType.LAZY)
    private List<Subject> subjects;
}  
@Transactional
public class InventoryService {

  public Inventory getInventoryById(Integer id) {
      Inventory inv = invRepository.findOne(id);
      if(inv == null) {
            throw new NotFoundException("Entity not found id: "+id);
      }
      return inv;
  }
} 
Hibernate: select streams0_.inventory_id as inventor1_1_0_, streams0_.streams_id as streams_2_2_0_, stream1_.id as id1_6_1_, stream1_.stream as stream2_6_1_ from inventory_stream streams0_ inner join stream stream1_ on streams0_.streams_id=stream1_.id where streams0_.inventory_id=?
Hibernate: delete from inventory_stream where inventory_id=?
Hibernate: select subjects0_.inventory_id as inventor1_1_0_, subjects0_.subjects_id as subjects2_3_0_, subject1_.id as id1_7_1_, subject1_.subjectName as subjectN2_7_1_ from inventory_subject subjects0_ inner join subject subject1_ on subjects0_.subjects_id=subject1_.id where subjects0_.inventory_id=?
Hibernate: delete from inventory_subject where inventory_id=?  
已从applicationContext.xml(entityManagerFactory)中删除以下属性:


符合事实的

并从
FetchType.LAZY
更改为
FetchType.EAGER
,解决了问题,但产生了另一个问题,即当返回
库存的单个bean时,它在
列表和
列表中返回重复数据。我不认为初始问题的根本原因是
enable\u lazy\u load\u no\u trans

能否再次交叉检查。我刚刚尝试在我的存储库中调用findOne(如您所述创建了多对多),但它没有触发任何删除查询。@Bala,嘿,我再次检查并更新了问题。@agpt,您有没有关于执行删除查询的解决方案?@hrdkisback啊,我现在真的不记得了,但正如您在问题(更新)中看到的,我一定是沿着那条路走的。除此之外,我没有任何其他更新。好的,谢谢您的快速回复。;)