Получить родительский объект из репозитория данных Spring - PullRequest
0 голосов
/ 21 января 2019

Как я могу получить родительский Entity объект от SubEntity объекта.

subEntityRepository.findById(id), а затем getEntity() вернуть null объект сущности.

@Entity
public class Entity {
    @Id
    private Long id;
    @Reference
    private List<SubEntity> subEntity;


    public Building(List<SubEntity> subEntity) {
        this.subEntity = subEntity;
    }

    //getters and setters
}

@Entity
public class SubEntity {
    @Id
    private Long id;

    @Reference
    private Entity entity;

    //getters and setters
}

public interface EntityRepository extends DatastoreRepository<Entity, Long> {}


public interface SubEntityRepository extends DatastoreRepository<SubEntity, Long>{}

P.S. Это БД Google DataStore! Нет OneToMany, другие аннотации JPA недоступны.

1 Ответ

0 голосов
/ 21 января 2019
@Entity
@Table(name="entity")
public class Entity {

    @Id
    private Long id;

    @OneToMany(mappedBy="entity")
    private List<SubEntity> subEntity;

    ....
}

@Entity
@Table(name="subEntity")
public class SubEntity {
    @Id
    private Long id;

    @ManyToOne
    @JoinColumn(name = "entity")
    private Entity entity;

    //getters and setters
}
...