Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name="usertable")
- @SuppressWarnings("serial")
- @Searchable
- public class User implements Serializable {
- @GeneratedValue(generator="userIdSeq")
- @SequenceGenerator(name="userIdSeq", sequenceName="usertable_id_seq")
- @SearchableId
- @Id
- int id;
- @SearchableProperty
- String userId; // 'Unique identifier. It is formed using this fashion: ''prefix:identifier''
- String userName; // Name used to log in the application.
- String email; // User's email
- @ManyToMany
- @JoinTable( name="usergroup",
- joinColumns=@JoinColumn(name = "userid", referencedColumnName="userId"),
- inverseJoinColumns=@JoinColumn(name = "groupid")
- )
- List<Group> groups;
- ...
- }
- // Group
- @Entity
- @Table(name="grouptable")
- public class Group implements Serializable {
- @Id
- String groupId // Unique identifier for group.
- String description // Brief text describing the group purposes.
- @ManyToMany(mappedBy="groups")
- @Cascade(SAVE_UPDATE)
- List<User> users
- ....
- }
- @Test
- @Transactional
- public void testGetUserByEmail(){
- Session session = factory.getCurrentSession();
- String email = "sarah.silverman@comedycentral.com";
- User user = createUser();
- user.setWeCanContact(true);
- user.setPartnersCanContact(false);
- user.setAgreedTerms(false);
- user.setReferer("Laura");
- String groupId = "AARP";
- String description = "Appletalk Address Resolution Protocol";
- Group group1 = new Group();
- group1.setGroupId(groupId);
- group1.setDescription(description);
- session.save(group1);
- session.flush();
- user.getGroupLists().add(group1);
- session.saveOrUpdate(user);
- User testUser = userRepository.getUserByEmail(email);
- assertNotNull(testUser);
- assertEquals("It's not the correct email", email, testUser.getEmail());
- }
- private User createUser(){
- Session session = factory.getCurrentSession();
- String userId = "UB:2010";
- String userPassword = "sarah";
- String realName = "Sarah Silverman";
- String email = "sarah.silverman@comedycentral.com";
- User user = new User();
- user.setUserId(userId);
- user.setUserName(email);
- user.setUserPassword(userPassword);
- user.setRealName(realName);
- user.setEmail(email);
- List<Group> emptyGroupLists = new ArrayList<Group>();
- user.setGroupLists(emptyGroupLists);
- Group group = new Group();
- group.setGroupId("ABC");
- group.setDescription("A for apple");
- user.getGroupLists().add(group);
- session.save(group);
- session.save(user);
- session.flush();
- return user;
- }
- // Repository Method to get user from email
- //
- // @NamedQuery(name="user.findByEmail",
- // query="SELECT u FROM User u " +
- // "LEFT JOIN FETCH u.groupLists " +
- // "WHERE upper(u.email) = :email")
- public User getUserByEmail(String email) {
- Session session = sessionFactory.getCurrentSession();
- Query query = session.getNamedQuery("user.findByEmail");
- query.setParameter("email", email.toUpperCase());
- User user = (User)query.uniqueResult();
- return user;
- }
Add Comment
Please, Sign In to add comment