I found this documentation about entity-graphs... after read it, it gave me the idea that you can used entity-graphs to retrieve only a subset of @Basic
fields of a given entity (Until now, I have used entity-graphs to retrieve relationships EAGERLY, i.e, for example, load an Employee[including all its attributes] and its associated Department[including all its attributes])...
So, I decided to try this using a small test:
@Entity
@Table(name = "employee")
@NamedEntityGraphs({
@NamedEntityGraph(
name = "OnlyName",
attributeNodes = @NamedAttributeNode(value = "name")
)
})
public class Employee implements Serializable {
...
@Id
@Column(name = "code", updatable = false)
private Long code;
@Basic(fetch = FetchType.LAZY)
@Column(name = "name", nullable = false)
private String name;
@Basic(fetch = FetchType.LAZY)
@Column(name = "last_name", nullable = false)
private String lastName;
@Lob @Basic(fetch = FetchType.LAZY)
@Column(name = "picture", nullable = false)
private byte[] picture;
public Employee() {
super();
}
...
}
Then, to retrieve my entity, I used the following code:
private Employee retrieveFromDatabase(long code) {
EntityGraph<Employee> graph; // Material Entity Graph
Map<String, Object> map = new HashMap<>();
graph = (EntityGraph<Employee>) this.em.createEntityGraph("OnlyName");
map.put("javax.persistence.fetchgraph", graph);
return this.em.find(Employee.class, code, map);
}
Now, this code always returns an Employee with all the fields fetched from the database; even the Hibernate logs show a query that selects all employee fields:
Hibernate: select employee0_.code as code1_0_0_, employee0_.last_name as last_name2_0_0_, employee0_.name as name3_0_0_, employee0_.picture as picture4_0_0_ from employee employee0_ where employee0_.code=?
)
When I was expecting a query like this: select employee0_.code as code1_0_0_, mployee0_.name as name3_0_0_ from employee employee0_ where employee0_.code=?
So, what I'm doing wrong? Does this feature is not supported by Hibernate??
NOTE: For the test, I was using hibernate 5.0.10 and wildfly10 ...
Thanks!