jukenduit
jukenduit

Reputation: 393

Find by where NOT current row

I am developing a small cms and I am using spring data jpa to do my database stuff.

When I add a new page, I want to make sure the slug doesn't already exist, for that purpose I added a method to my repository:

public interface PageRepository extends JpaRepository<Page, Integer> {

    Page findBySlug(String slug);

}

That works fine when adding.

However when editing a page, I want to check that the slug doesn't already exist but NOT for the current page, how can I do that? I guess I could somehow pass the current row id or something like that, but how would I do that?

Upvotes: 2

Views: 262

Answers (2)

marok
marok

Reputation: 2146

You can write

Page findBySlugAndIdNot(String slug,Long id)

where id is name of your identifier in entity with proper type. Look at documentation

Upvotes: 2

corroborator
corroborator

Reputation: 321

you may try custom query like :

@Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM Page p WHERE p.slug = :slug and p.pageId!=pageId")
public boolean existsBySlugInPage(@Param("slug") String slug, @Param("pageId") Integer pageId);

Upvotes: 0

Related Questions