Implementing Spring Data JPA repositories

With the domain model implemented successfully, JpaRepository for it can be implemented using the Spring Data JPA. The specialty of this is that there is no need to implement anything. Just writing an interface that extends from the JpaRepository interface would be sufficient to expose methods to find one, find all, save, delete, and so on. The following code shows TweetRepository:

public interface TweetRepository extends JpaRepository<Tweet, Integer> {

List<Tweet> findByTweetUser_ScreenNameOrContentContains(String
screenName, String mention);

}

Since tweets need to be listed for a particular logged in user or any tweets that mention them (with the @ symbol in front of the name), the findByTweetUser_ScreenNameOrContentContains method is implemented, which will generate the appropriate query to search the tweets. Apart from this, all the default findAll, findById, delete, and save methods will be available.

The following code shows UserRepository:

public interface UserRepository extends JpaRepository<User, String> {
User findByScreenName(String screenName);
}

In the preceding code, there is a method by the name of findByScreenName, where screenName is a property of the User class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169