title: Refresh and fetch an entity after save (JPA/Spring Data/Hibernate)
body: <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 = &quot;something&quot;)
  public class Something implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;name&quot;)
      private String name;
  
      @Column(name = &quot;owner&quot;)
      private String owner;
  
      @ManyToOne
      private Property property;
  
      // getters and setters
  
      @Override
      public String toString() {
          return &quot;Something{&quot; +
              &quot;id=&quot; + getId() +
              &quot;, name='&quot; + getName() + &quot;'&quot; +
              &quot;, owner='&quot; + getOwner() + &quot;'&quot; +
              &quot;, property=&quot; + getProperty() +
              &quot;}&quot;;
      }
  </code></pre>
  <p><strong>Property:</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;property&quot;)
  public class Property implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = &quot;shape&quot;)
      private String shape;
  
      @Column(name = &quot;color&quot;)
      private String color;
  
      @Column(name = &quot;dimension&quot;)
      private Integer dimension;
  
      // getters and setters
  
      @Override
      public String toString() {
          return &quot;Property{&quot; +
              &quot;id=&quot; + getId() +
              &quot;, shape='&quot; + getShape() + &quot;'&quot; +
              &quot;, color='&quot; + getColor() + &quot;'&quot; +
              &quot;, dimension='&quot; + getDimension() + &quot;'&quot; +
              &quot;}&quot;;
      }
  }
  </code></pre>
  <p>This is the <code>SomethingRepository</code> (Spring):</p>
  <pre><code>@SuppressWarnings(&quot;unused&quot;)
  @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(&quot;/api&quot;)
  public class SomethingResource {
  
      private final SomethingRepository somethingRepository;
  
      public SomethingResource(SomethingRepository somethingRepository) {
          this.somethingRepository = somethingRepository;
      }
  
      @PostMapping(&quot;/somethings&quot;)
      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>{
    &quot;name&quot;: &quot;MyName&quot;,
    &quot;owner&quot;: &quot;MySelf&quot;,
    &quot;property&quot;: {
      &quot;id&quot;: 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>{
    &quot;id&quot;: 1,
    &quot;name&quot;: &quot;MyName&quot;,
    &quot;owner&quot;: &quot;MySelf&quot;,
    &quot;property&quot;: {
      &quot;id&quot;: 1,
      &quot;shape&quot;: null,
      &quot;color&quot;: null,
      &quot;dimension&quot;: 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(&quot;/api&quot;)
  @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(&quot;/somethings&quot;)
      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>{
    &quot;id&quot;: 4,
    &quot;name&quot;: &quot;MyName&quot;,
    &quot;owner&quot;: &quot;MySelf&quot;,
    &quot;property&quot;: {
      &quot;id&quot;: 1,
      &quot;shape&quot;: &quot;Rectangle&quot;,
      &quot;color&quot;: &quot;Red&quot;,
      &quot;dimension&quot;: 50
    }
  }
  </code></pre>
  <p>Is there an automatic method/annotation, with JPA or Spring or Hibernate, in order to have the &quot;complete&quot; persisted entity?</p>
  <p>I would like to avoid to declare the <code>EntityManager</code> in every REST or Service class, or I want avoid to call the <code>.findOne(Long)</code> method everytime I want the new refreshed persisted entity.</p>