title: entity refresh during save/update using SPRING DATA JPA
body: <p>I am using Spring Data JPA in Spring Boot.</p>
  
  <p>After an entity saved to database using Spring Data Jpa Repository, I am not getting whole entity. i.e., getting only the values which I passed to save().</p>
  
  <p>So, I have checked google/SO and then added entityManager.refresh(organization). Now I am getting whole entity and mapped entities too.</p>
  
  <p>Enterprise entity:</p>
  
  <pre><code>@Entity
  @Table(name = "enterprise")
  public class Enterprise extends BaseEntity implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "enterprise_id")
      private Long enterpriseId;
      @Column(name = "enterprise_external_name", length = 150, unique = true)
      @NotNull
      private String enterpriseExternalName;
      @Column(name = "enterprise_internal_name", length = 150, unique = true)
      @NotNull
      private String enterpriseInternalName;
      @OneToMany(mappedBy = "enterprise")
      @JsonIgnoreProperties(allowSetters = true, value = { "enterprise" })
      private Set&lt;Organization&gt; organizations;
      //getter and setters
  }
  </code></pre>
  
  <p>Organization entity:</p>
  
  <pre><code>@Entity
  @Table(name = "organization")
  public class Organization extends BaseEntity implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "organization_id")
      private Long organizationId;
      @Column(name = "organization_name", length = 150, unique = true)
      @NotBlank(message = "OrganizationName can't be empty !")
      private String organizationName;
      @ManyToOne
      @JoinTable(name = "enterprise_organization", joinColumns = {
              @JoinColumn(name = "organization_id") }, inverseJoinColumns = { @JoinColumn(name = "enterprise_id") })
      @JsonIgnoreProperties(allowSetters = true, value = { "organizations" })
      private Enterprise enterprise;
      //getters and setters
  }
  </code></pre>
  
  <p>Controller class:</p>
  
  <pre><code>@PostMapping
  public ResponseEntity&lt;ValidationResponse&gt; addOrganization(@Valid @RequestBody Organization organization)
  {
      ValidationResponse response = new ValidationResponse();
      response.setMessage(organizationService.addOrganization(organization));
  
      return new ResponseEntity&lt;ValidationResponse&gt;(response, HttpStatus.CREATED);
  }
  </code></pre>
  
  <p>Service class:</p>
  
  <pre><code>@Service
  @Transactional(propagation = Propagation.REQUIRED)
  public class OrganizationServiceImpl implements OrganizationService {
      @PersistenceContext
      private EntityManager entityManager;
      @Autowired
      private OrganizationRepository organizationRepository;      
      public Organization addOrganization(Organization organization) {
          organization.setCreatedDate(Calendar.getInstance());
          organizationRepository.save(organization);
          entityManager.refresh(organization);
          return organization;
      }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>@Repository
  public interface OrganizationRepository extends JpaRepository&lt;Organization, Long&gt; {
  }
  </code></pre>
  
  <p>Request body:</p>
  
  <pre><code>{
      "organizationName":"org5",
      "enterprise":{
          "enterpriseId":2
      }
  }
  </code></pre>
  
  <p>Response If I don't include <strong>entityManager.refresh(organization)</strong>:</p>
  
  <pre><code>{
      "success": true,
      "message": {
          "createdBy": 0,
          "updatedBy": 0,
          "createdDate": 1510140158654,
          "updatedDate": null,
          "organizationId": 14,
          "organizationName": "org5",
          "enterprise": {
              "createdBy": 0,
              "updatedBy": 0,
              "createdDate": null,
              "updatedDate": null,
              "enterpriseId": 2,            
              "enterpriseName": null,
              "active": true
          },
          "active": true
      },
      "errors": null
  }
  </code></pre>
  
  <p>But the problem I found was, its hitting the database again and selecting the data. It will be a performance issue if I have more mappings and also in every service class method I need to add entityManager.refresh().
  Is this only way to refresh the entity after save/update ? Or can we avoid this by adding any configuration etc.. ? Please suggest.</p>
  
  <p>Thanks.</p>