简体   繁体   English

如何使用Spring Data Rest在GET调用中获取EmbeddedId对象

[英]How to fetch embeddedId object in GET call using Spring Data Rest

I am using Spring 1.3.3 and I am unable to get the associated object for embedded class using GET. 我正在使用Spring 1.3.3,并且无法使用GET获取嵌入式类的关联对象。 It returns the URI instead of object while using projection. 使用投影时,它返回URI而不是对象。

   {
      "employeeId": {
        "empId": 4,
        "_links": {
          "address": {
            "href": "http://localhost:8082/address/1"
          },
          "zipcode": {
            "href": "http://localhost:8082/zipcode/2"
          }
        }
      },
      "empId": 4,
      "name": "ap",

    },
    "_links": {
      "self": {
        "href": "http://localhost:8082/employee/com.blah.blah.Employee@64f11498"
      },
      "adNetworkParam": {
        "href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498{?projection}",
        "templated": true
      },
      "demandSource": {
        "href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498/address"
      },
      "targetCharacteristic": {
        "href": "http://localhost:8082/employee/com.blah.blah.EmployeeId@64f11498/zipcode"
      }
    }
  }

My domain classes as follows.. 我的域类如下。

Employee.java Employee.java

@Entity @实体

@Table(name = "employee")
public class Employee implements Serializable {
    private EmployeeId employeeId;
    private Address address;
    private Zipcode zipcode;
    private int empId;
    private String name;

    //getter
    ...
    @MapsId("address")
    @ManyToOne
    @JoinColumn(name = "address_id")
    public Address getAddress() {
        return address;
    }

    @MapsId("zipcode")
    @ManyToOne
    @JoinColumn(name = "zipcode_id")
    public Zipcode getZipcode() {
        return zipcode;
    }

    // setter
    .....

Address.java Address.java

@Entity
@Table(name="address")
public class Address implements Serializable {
    private int id;
    private String streetName;
    private String cityName

Zipcode.java Zipcode.java

@Entity
@Table(name = "zipcode")
public class Zipcode implements Serializable {
    @Id
    @GeneratedValue
    private int id;
    private String code;

EmployeeId.java EmployeeId.java

@Embeddable
public class EmployeeId implements Serializable {
    private Address address;
    private Zipcode zipcode;
    private int empId;

My Projection is as follows, 我的预测如下

@Projection(name = "employeeProjection", types = { Employee.class })
public interface EmployeeProjection {

    Address getAddress();

    Zipcode getZipcode();

    int getEmpId();

    String getName();
}

Employee Search repository class as follows, 员工搜索存储库类如下,

@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {
    List<Employee> findByNameContaining(@Param("name") @RequestParam("name") String name);

    Employee findById(@Param("employeeId") @RequestParam("employeeId") Integer employeeId);

} 

How to return associated object instead of URI for embedded class?? 如何为嵌入式类返回关联的对象而不是URI?

Kindly provide your inputs. 请提供您的输入。

You should use excerpts in your repository definition. 您应该在存储库定义中使用摘录。 I can guess according to your GET result, that the Employee repository is as following: 我可以根据您的GET结果来猜测Employee存储库如下:

interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {}

Change it as following: 进行如下更改:

@RepositoryRestResource(excerptProjection = EmployeeProjection.class)
interface EmployeeRepository extends CrudRepository<Employee, EmployeeId> {} 

This will cause to zipCode and address to be inlined. 这将导致zipCode和地址被内联。 Note, that links to inlined resources will still appear in the projection. 请注意,指向内联资源的链接仍将出现在投影中。

The employee model should be modified and to be defined with @OneToOne annotation above embedded members: 员工模型应进行修改,并通过嵌入成员上方的@OneToOne注释进行定义:

@Table(name = "employee")
public class Employee implements Serializable {
    private EmployeeId employeeId;
    @OneToOne
    private Address address;
    @OneToOne
    private Zipcode zipcode;
    private int empId;
    private String name;

You can read more in spring doc . 您可以在spring doc中阅读更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Data Rest在GET调用中获取自引用对象 - How to fetch Self Referencing object in GET call using Spring Data Rest 如何使用 @EmbeddedId 使用 Spring Data REST 和 ConversionService? - How to use Spring Data REST and ConversionService using @EmbeddedId? 如何在Spring Data REST中公开@EmbeddedId转换器 - How to expose @EmbeddedId converters in Spring Data REST 如何在spring-boot数据休息时在POST json中传递@EmbeddedId - How to pass @EmbeddedId in POST json in spring-boot data rest 如何使用Spring Data Rest在POST调用中保存嵌入式对象 - How to save embedded object in POST call using Spring Data Rest 如何让休眠状态急切地获取 EmbeddedId? - How do I get hibernate to eagerly fetch EmbeddedId? 如何使用 Spring Data / Hibernate 级联保持 @OneToMany 与 @EmbeddedId 的关系 - How do I cascade persist an @OneToMany relationship with an @EmbeddedId using Spring Data / Hibernate Spring JPA - 如何使用复合键(EmbeddedID)保存对象 - Spring JPA - How to Save Object with a Composite Key (EmbeddedID) 如何使用spring rest api从休眠的两个表中获取数据并在单个对象中返回URL - How to fetch data from two tables in hibernate with spring rest api and return in single object to url 如何使用Spring Data REST在OneToMany关系中添加对象 - How to add an object in a OneToMany relationship using Spring Data REST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM