Last active
December 19, 2015 18:39
-
-
Save MagnusSmith/6000393 to your computer and use it in GitHub Desktop.
Starting with a simple entity UserAccount
If you create an interface for a repository like below then Spring Data will create a proxy for you to inject into your service.
It will automatically give you save, find, findAll, findOne, count methods for free (ie you don't have to do any implementation code). Since the UserAccount entity has a usern…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//JPA enitiy called UserAccount | |
@Entity | |
public class UserAccount { | |
@Id | |
private Long id; | |
private String username; | |
...... | |
} | |
// create an interface that Spring Data will use to create a proxy | |
public interface UserAccountRepository extends JpaRepository<UserAccount, Long> { | |
public UserAccount findByUsername(String username); | |
} | |
// now in my service class | |
@Service("userAccountService") | |
public class UserAccountServiceImpl implements UserAccountService { | |
@Inject | |
private UserAccountRepository userRepos; | |
public UserAccount find(String username){ | |
return userRepos.findByName(userName); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment