当前位置: 首页 > 知识库问答 >
问题:

在此ManagedType上找不到具有给定名称[trackers]的属性

徐瑞
2023-03-14

我试图提供应用程序在Spring与懒惰取实体之间的关系。

模型"用户":

     Entity
 @Table(name = "users")
 @Component
 public class User {

   @Id
   @SequenceGenerator(name = "userseq", sequenceName = "userseq", allocationSize = 1)
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userseq")
   private Integer id;

   // Some fields/getters/setters 

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
   private List<Tracker> trakers;

 }

“跟踪器”模型

 @Entity
 @Table(name = "trackers")
 @Component
 public class Tracker {

    @Id
    @SequenceGenerator(name = "tracker_seq", sequenceName = "tracker_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tracker_seq")
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable=false, updatable=false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private User user;

// Some fields/getters/setters 
}

继承了带有@EntityGraph的JPA存储库。通过这一点,我试图为select用户提供与以下相关的所有跟踪器:

@Repository
@Transactional(readOnly = true)
public interface CrudUserRepository extends JpaRepository<User, Integer> {

   @EntityGraph (attributePaths = {"trackers"}, type = EntityGraph.EntityGraphType.LOAD)
   @Query("SELECT u FROM User u WHERE u.id=?1")
   User getByIdWithTrackers(int id);
}

存储库类:

 @Repository
 public class AnketUserRepository implements UserRepository {

   @Autowired
   private CrudUserRepository crudRepository;

   @Override
   public User getByIdWithoutTrackers(int id) {
       return crudRepository.findById(id).orElse(null);
   }

   @Override
   public User getByIdWithTrackers(int id){
       return crudRepository.getByIdWithTrackers(id);
    }

}

和控制器:

@RestController ("userRestController")
@RequestMapping(value = UserRestController.USER_URL, produces = 
MediaType.APPLICATION_JSON_VALUE)
public class UserRestController extends AbstractUserController {

    public static final String USER_URL = "/customers";

    @GetMapping("/{id}")
    public User getByIdWithoutTrackers(@PathVariable int id) {
        return super.getByIdWithoutTrackers(id);
    }

    @GetMapping("/{id}/withTrackers")
    public User getByIdWithTrackers(@PathVariable int id) {
        return super.getByIdWithTrackers(id);
    }
 }

查询“/customers/1”可以正常工作。它返回没有跟踪器的所有客户(相应地,延迟获取)。

但是"/顾客/1/with Tracker"返回以下异常:

  lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [trackers] on this ManagedType [ru.spb.model.User]"}

共有1个答案

廉鸿运
2023-03-14

哦,这是个愚蠢的错误。在用户界面中,我写“追踪者”。但在CrudUserRepository的AttributePath“追踪者”中。

 类似资料: