Example usage for org.springframework.data.repository.core EntityInformation getId

List of usage examples for org.springframework.data.repository.core EntityInformation getId

Introduction

In this page you can find the example usage for org.springframework.data.repository.core EntityInformation getId.

Prototype

@Nullable
ID getId(T entity);

Source Link

Document

Returns the id of the given entity or null if none can be obtained.

Usage

From source file:org.lightadmin.core.web.support.DynamicPersistentEntityResourceAssembler.java

/**
 * @see DATAREST-269 (https://jira.spring.io/browse/DATAREST-269)
 *///from ww w .ja va2s .  c  om
@Override
public Link getSelfLinkFor(Object instance) {
    Assert.notNull(instance, "Domain object must not be null!");

    Repositories repositories = repositories(this);

    Class instanceType = instance.getClass();
    PersistentEntity<?, ?> entity = repositories.getPersistentEntity(instanceType);

    if (entity == null) {
        throw new IllegalArgumentException(
                String.format("Cannot create self link for %s! No persistent entity found!", instanceType));
    }

    EntityInformation<Object, Serializable> entityInformation = repositories
            .getEntityInformationFor(instanceType);
    Serializable id = entityInformation.getId(instance);

    if (id == null) {
        return entityLinks(this).linkToCollectionResource(entity.getType()).withSelfRel();
    }

    return entityLinks(this).linkToSingleResource(entity.getType(), id).withSelfRel();
}