Refresh and fetch an entity after save (JPA/Spring Data/Hibernate) : <p>I've these two simple entities <code>Something</code> and <code>Property</code>.
The <code>Something</code> entity has a many-to-one relationship to <code>Property</code>, so when I create a new <code>Something</code> row, I assign an existing <code>Property</code>.</p>

<p><strong>Something:</strong></p>

<pre><code>@Entity
@Table(name = "something")
public class Something implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "owner")
    private String owner;

    @ManyToOne
    private Property property;

    // getters and setters

    @Override
    public String toString() {
        return "Something{" +
            "id=" + getId() +
            ", name='" + getName() + "'" +
            ", owner='" + getOwner() + "'" +
            ", property=" + getProperty() +
            "}";
    }
</code></pre>

<p><strong>Property:</strong></p>

<pre><code>@Entity
@Table(name = "property")
public class Property implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "shape")
    private String shape;

    @Column(name = "color")
    private String color;

    @Column(name = "dimension")
    private Integer dimension;

    // getters and setters

    @Override
    public String toString() {
        return "Property{" +
            "id=" + getId() +
            ", shape='" + getShape() + "'" +
            ", color='" + getColor() + "'" +
            ", dimension='" + getDimension() + "'" +
            "}";
    }
}
</code></pre>

<p>This is the <code>SomethingRepository</code> (Spring):</p>

<pre><code>@SuppressWarnings("unused")
@Repository
public interface SomethingRepository extends JpaRepository&lt;Something,Long&gt; {

}
</code></pre>

<p>Through a REST controller and a JSON, I want to create a new <code>Something</code>:</p>

<pre><code>@RestController
@RequestMapping("/api")
public class SomethingResource {

    private final SomethingRepository somethingRepository;

    public SomethingResource(SomethingRepository somethingRepository) {
        this.somethingRepository = somethingRepository;
    }

    @PostMapping("/somethings")
    public Something createSomething(@RequestBody Something something) throws URISyntaxException {
        Something result = somethingRepository.save(something);
        return result;
    }
}
</code></pre>

<p>This is the JSON in input (the <code>property</code> <code>id</code> 1 is an existing row in the database):</p>

<pre><code>{
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1
  }
</code></pre>

<p>}</p>

<p>The problem is: after the method <code>.save(something)</code>, the variable <code>result</code> contains the persisted entity, but without the fields of field <code>property</code>, validated (they are <code>null</code>):</p>

<p>Output JSON:</p>

<pre><code>{
  "id": 1,
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1,
    "shape": null,
    "color": null,
    "dimension": null
  }
}
</code></pre>

<p>I expect that they are validated/returned after the save operation.</p>

<p>To workaround this, I have to inject/declare the <code>EntityManager</code> in the REST controller, and call the method <code>EntityManager.refresh(something)</code> (or I have to call a <code>.findOne(something.getId())</code> method to have the complete persisted entity):</p>

<pre><code>@RestController
@RequestMapping("/api")
@Transactional
public class SomethingResource {

    private final SomethingRepository somethingRepository;

    private final EntityManager em;

    public SomethingResource(SomethingRepository somethingRepository, EntityManager em) {
        this.somethingRepository = somethingRepository;
        this.em = em;
    }

    @PostMapping("/somethings")
    public Something createSomething(@RequestBody Something something) throws URISyntaxException {
        Something result = somethingRepository.save(something);
        em.refresh(result);
        return result;
    }
}
</code></pre>

<p>With this workaround, I've the expected saved entith (with a correct JSON):</p>

<pre><code>{
  "id": 4,
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1,
    "shape": "Rectangle",
    "color": "Red",
    "dimension": 50
  }
}
</code></pre>

<p>Is there an automatic method/annotation, with JPA or Spring or Hibernate, in order to have the "complete" persisted entity?</p>

<p>I would like to avoid to declare the EntityManager in every REST or Service class, or I want avoid to call the .findOne(Long) method everytime I want the new refreshed persisted entity.</p>

<p>Thanks a lot,
Andrea</p>
