【问题标题】:Unable to locate Attribute with the given name [trackers] on this ManagedType无法在此 ManagedType 上找到具有给定名称 [trackers] 的属性
【发布时间】:2020-03-19 18:39:04
【问题描述】:

我正在尝试为 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 存储库。通过这个,我试图为选择用户提供所有与以下相关的跟踪器:

@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”工作正常。它返回所有没有跟踪器的客户(相应的 Lazy-Fetch)。

但是“/customers/1/withTrackers”返回以下异常:

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

【问题讨论】:

    标签: java spring hibernate lazy-initialization


    【解决方案1】:

    哦,这是一个愚蠢的错误。在用户中,我写了“trakers”。但是在 CrudUserRepository 的属性路径“跟踪器”中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多