Spring Rest - 实体图注释已获取已忽略且完整的实体

时间:2016-11-07 04:55:30

标签: hibernate spring-mvc jpa jackson spring-data-jpa

在使用Jersey开发REST服务时,我发现实体图注释被忽略,我得到了完整的对象。代码如下。

@Entity
@Table(name = "CONCERN")
@XmlRootElement
@EntityListeners(ConcernListener.class)
@NamedEntityGraph(name="concern.search", attributeNodes={
        @NamedAttributeNode("referenceId"),
        @NamedAttributeNode("name"),
        @NamedAttributeNode("mobileNumber"),
        @NamedAttributeNode("email"),
        @NamedAttributeNode("concernDetail"),
        @NamedAttributeNode(value = "concernType", subgraph = "concernType")},
        subgraphs = @NamedSubgraph(name = "concernType", attributeNodes = {@NamedAttributeNode("descEn"),@NamedAttributeNode("descAr")})
    )
public class Concern {

    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "TXN_REPORT_CONCERN_SEQ")
    @SequenceGenerator(name = "CONCERN_SEQ", sequenceName = "TXN_REPORT_CONCERN_SEQ", allocationSize = 0)
    private Long id;

    @Basic(optional = false)
    @Size(min = 1, max = 30)
    @Column(name = "REFERENCE_ID", nullable = false, length = 30)
    private String referenceId;
.......

存储库代码

public interface ReportConcernRepository extends CrudRepository<Concern, Long> {


    @EntityGraph(value = "concern.search", type = EntityGraphType.FETCH)
    @Query("SELECT c FROM Concern c")
    public List<Concern> readAll();
}

泽西休息服务

@GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("findAll")
    public List<Concern> getAllConcerns(){
        return reportConcernService.getAllConcerns();
    }

输出JSON

{"id":240,"referenceId":"671606","name":"TestM","mobileNumber":"777777","email":"1","concernDetail":"1","latlong":"100_100","address":"this is a test address","concertTypestr":null,"concernType":{"id":1,"descEn":"Concern Type","descAr":"Concern Type(AR)"},"statusType":{"id":1,"statusEn":"Open","statusAr":"Open (AR)"},"concernAttachment":{"id":240,"attachment":"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGIAdcDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9/KKKKACiiigAooooAKKKKACi ......

可以看出整个实体已经加载而不是属性。可能是什么原因?

1 个答案:

答案 0 :(得分:0)

我可能为派对来得太迟了,但我遇到了类似的问题。将Entity Graph用于您描述的任务似乎很自然,但不幸的是,这不起作用。原因就是Jackson在序列化过程中调用了你实体中的每个getter,所以在一天结束时你在Entity Graph中包含哪些属性并没有关系(其他的只是延迟加载),因为Jackson会得到它们无论如何。我发现只有一个简单的解决方案:@JsonIgnore绝对不像Entity Graph那样灵活。此外,并非所有JPA ORM都已准备好支持100%实体图,请参阅https://hibernate.atlassian.net/browse/HHH-9270