title: How to define Spring Data Repository scope to Prototype?
body: <p>I'm using Spring data jpa &amp; hibernate for data access along with Spring boot. All the repository beans are singleton by default. I want to define the scope of all my repositories to <code>Prototype</code>. How can I do that? </p>
  
  <pre><code>@Repository
  public interface CustomerRepository extends CrudRepository&lt;Customer, Long&gt; {
      List&lt;Customer&gt; findByLastName(String lastName);
  }
  </code></pre>
  
  <p><strong>Edit 1</strong></p>
  
  <p>The problem is related to domain object being shared in 2 different transactions which is causing my code to fail. I thought it is happening because repository beans are singleton. That's the reason I asked the question. Here is the detailed explanation of the scenario.</p>
  
  <p>I have 2 entities <code>User</code> and <code>UserSkill</code>. <code>User</code> has 1-* relationship with <code>UserSkills</code> with lazy loading enabled on <code>UserSkill</code> relation.</p>
  
  <p>In a <code>UserAggregationService</code>, I first make a call to fetch an individual user skill by id <code>123</code> which belongs to user with id <code>1</code>. </p>
  
  <pre><code>public class UserAggregationService {
    public List&lt;Object&gt; getAggregatedResults() {
      resultList.add(userSkillService.getUserSkill(123));
      //Throws Null Pointer Exception. See below for more details.
      resultList.add(userService.get(1));
    }
  }
  </code></pre>
  
  <p>Implementation of UserSkillService method looks like</p>
  
  <pre><code>@Override
  public UserSkillDTO getUserSkill(String id) {
      UserSkill userSkill = userSkillService.get(id);
  
      //Skills set to null avoid recursive DTO mapping. Dozer mapper is used 
      //for mapping.
      userSkill.getUser().setSkills(null);
      UserSkillDTO result = mapper.map(userSkill, UserSkillDTO.class);
  
      return result;
  }
  </code></pre>
  
  <p>In the call of user aggregation service, I call UserService to fetch userDetails. UserService code looks like</p>
  
  <pre><code>@Override
  public UserDTO getById(String id) {
      User user = userService.getByGuid(id);
  
      List&lt;UserSkillDTO&gt; userSkillList = Lists.newArrayList();
      //user.getSkills throws null pointer exception.
      for (UserSkill uSkill : user.getSkills()) {
         //Code emitted
      }
      ....
      //code removed for conciseness 
  
      return userDTO;
  }
  </code></pre>
  
  <p>UserSkillService method implementation</p>
  
  <pre><code>public class UserSkillService {
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public UserSkill get(String guid) throws PostNotFoundException {
      UserSkill skill = userSkillRepository.findByGuid(guid);
      if (skill == null) {
          throw new SkillNotFoundException(guid);
      }
      return skill;
    }
  }
  </code></pre>
  
  <p>UserService method implementation:</p>
  
  <pre><code>public class UserService {
    @Override
    @Transactional(readOnly = true)
    public User getByGuid(String guid) throws UserNotFoundException {
      User user = userRepo.findByGuid(guid);
      if (user == null) {
          throw new UserNotFoundException(guid);
      }
  
      return user;
    }
  }
  </code></pre>
  
  <p>Spring boot auto configuration is used to instantiate entity manager factory and transaction manager. In the configuration file spring.jpa.* keys are used to connect to the database.</p>
  
  <p>If I comment the below line of code, then I do not get the exception. I am unable to understand why change in the domain object is being affecting the object fetch in a different transaction.</p>
  
  <pre><code>userSkill.getUser().setSkills(null);
  </code></pre>
  
  <p>Please suggest If I have missed something.</p>