Spring Hateoas 链接和 JPA 持久性

Spring Hateoas links and JPA persistence

我有一个 JPA 实体,我正在为 link 构建 hateoas:

@Entity
public class PolicyEvent extends ResourceSupport` 

我希望生成的 hateoas links 保留在 PolicyEvent table 中。 JPA 似乎没有在 PolicyEvent table 中为资源支持中的 _links 成员创建列。

是否有某种方法可以通过 JPA 保留 links 成员,或者我的方法本身不正确(不应保留 Hateoas link 数据)。

谢谢

我建议不要混合使用 JPA 实体和 HATEOAS 公开资源。以下是我的典型设置:

数据对象:

@Entity
public class MyEntity {
    // entity may have data that
    // I may not want to expose
}

存储库:

public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
    // with my finders...
}

HATEOAS 资源:

public class MyResource {
    // match methods from entity 
    // you want to expose
}

服务实现(界面未显示):

@Service
public class MyServiceImpl implements MyService {
    @Autowired
    private Mapper mapper; // use it to map entities to resources
    // i.e. mapper = new org.dozer.DozerBeanMapper();
    @Autowired
    private MyEntityRepository repository;

    @Override
    public MyResource getMyResource(Long id) {
        MyEntity entity = repository.findOne(id);
        return mapper.map(entity, MyResource.class);
    }
}

最后,暴露资源的控制器:

@Controller
@RequestMapping("/myresource")
@ExposesResourceFor(MyResource.class)
public class MyResourceController {
    @Autowired
    private MyResourceService service;
    @Autowired
    private EntityLinks entityLinks;

    @GetMapping(value = "/{id}")
    public HttpEntity<Resource<MyResource>> getMyResource(@PathVariable("id") Long id) {
        MyResource myResource = service.getMyResource(id);
        Resource<MyResource> resource = new Resource<>(MyResource.class);
        Link link = entityLinks.linkToSingleResource(MyResource.class, profileId);
        resource.add(link);
        return new ResponseEntity<>(resource, HttpStatus.OK);
    }
}

@ExposesResourceFor 注释允许您在控制器中添加逻辑以公开不同的资源对象。