title: How to Create a Predicate object and pass it manually in code to the Spring CrudRepository
body: <p>I have a method that should perform persist action on an object that is in from the RestController, but before that it should make sure that the Object name is unique , so I am trying to use the .exists(Predicate predicate) method in the spring CrudRepository class.</p>
  
  <p>but I don't know how to create this Predicate Object, which is usually passed from the request url to the JPA directly, I need to know what is the syntax of creating a Predicate in the code.</p>
  
  <p>This is my restController </p>
  
  <pre><code>@RestController("MyObjectRestController")
  @RequestMapping(value = "/v1.0/myObject", produces = "application/json")
  public class MyObjectRestController extends BaseRestController&lt;MyObject&gt; {
  
      @Autowired
      private MyObjectService service;
  
  public ResponseEntity&lt;?&gt; save(@RequestBody MyObject body)
              throws NotFoundException {
              return service.saveNew(body);
      }
  }
  </code></pre>
  
  <p>and this is the MyObjectService Class where I want to use the exists(Preidicate) before the calling the save method.</p>
  
  <pre><code>@Service
  public class MyObjectService extends BaseService&lt;MyObject&gt; {
      @Autowired
      private MyObjectRepository repository;
  
  public Long saveNew(MyObject work){
          if(repository.exists(predicateObject )) // here is my problem
              System.out.println("This name exists, can't save your object");
          return work.getId();
      }
  }
  </code></pre>
  
  <p>My repository which is inherited from CrudRepository</p>
  
  <pre><code>@Repository
  public interface MyObjectRepository extends BaseRepository&lt;MyObject&gt;  {
  
      public MyObject findByName(String name);
  
  }
  </code></pre>
  
  <p>The BaseRepository</p>
  
  <pre><code>@NoRepositoryBean
  public interface BaseRepository&lt;S extends BaseEntity&gt; extends PagingAndSortingRepository&lt;S, Long&gt;, QueryDslPredicateExecutor&lt;S&gt;
  {
  
  }
  </code></pre>