0

I have two entities:

@Entity
@Table(name = "animals")
public class Animal extends BaseEntity {
    private String nickname;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "species_id", nullable = false)
    private Species species;
}

and

@Entity
@Table(name = "species")
public class Species extends BaseEntity {
    private String scientificName;
    private Integer animalsPerHouse;
}

How to get Animal with specific Species field, scientificName for example? How to tell to Hibernate that I need only specific fields of nested entity?

Desired animal:

  {
    "id": 1,
    "nickname": "Locuroumee",
    "species": {
      "id": 161130,
      "scientificName": "Anguilla bicolor",
  }

Actual animal:

  {
    "id": 1,
    "nickname": "Locuroumee",
    "species": {
      "id": 161130,
      "scientificName": "Anguilla bicolor",
      "animalsPerHouse": 4
  }

I already spent much time with projections, aliases, but it doesn't help

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
hardcoder
  • 3
  • 2
  • I have similar problem to [this](http://stackoverflow.com/questions/12105757/complex-hibernate-projections) – hardcoder Jun 07 '16 at 16:27

1 Answers1

0

There are entity graphs for such tasks.

Dynamic fetching via JPA entity graph

JPA 2.1 Entity Graph – Part 1: Named entity graphs

JPA 2.1 Entity Graph – Part 2: Define lazy/eager loading at runtime

But, looks like, Hibernate doesn't support partial fields loading, opposite EclipseLink.

You can try to use a custom result transformer for it: How to transform a flat result set using Hibernate

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Projection serves to partial fields loading. And it works) And it may work with nested entity fields... but I don't know how – hardcoder Jun 07 '16 at 16:25
  • @hardcoder Please, see an example here: http://stackoverflow.com/a/36361630/3405171 – v.ladynev Jun 07 '16 at 16:27
  • already saw) they use custom transformer. But apparently there are solution without it. – hardcoder Jun 07 '16 at 16:33
  • @hardcoder Which the solution? Please, add it to the question. Without a transformer, using projections, you will have `List`. – v.ladynev Jun 07 '16 at 16:35
  • I use **aliasToBean** transformer to avoid this. Maybe you know how to add projection to nested entity fields? – hardcoder Jun 07 '16 at 16:38
  • @hardcoder You can't use `aliasToBean` for nested fields. Please, see an example, as I have told you: there is the `createAlias()` method there, to add projections to the nested fields – v.ladynev Jun 07 '16 at 16:42
  • Tried yours **FluentHibernateResultTransformer** - is a solution)) Strangely, our teamlead hinted that custom transformer or projections are suitable for this problem – hardcoder Jun 07 '16 at 17:09
  • @hardcoder I am very glad to help you. I have some other interesting utilities classes, like a naming strategy. And I am going to extend `FluentHibernateResultTransformer` to support HQL nested projections. – v.ladynev Jun 07 '16 at 17:17