Skip to content

Instantly share code, notes, and snippets.

@MagnusSmith
Last active December 19, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnusSmith/6000393 to your computer and use it in GitHub Desktop.
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…
//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