Guest User

Untitled

a guest
Feb 14th, 2018
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. @Entity
  2. @Table(name="usertable")
  3. @SuppressWarnings("serial")
  4. @Searchable
  5. public class User implements Serializable {
  6.  
  7. @GeneratedValue(generator="userIdSeq")
  8. @SequenceGenerator(name="userIdSeq", sequenceName="usertable_id_seq")
  9. @SearchableId
  10. @Id
  11. int id;
  12.  
  13. @SearchableProperty
  14. String userId; // 'Unique identifier. It is formed using this fashion: ''prefix:identifier''
  15. String userName; // Name used to log in the application.
  16. String email; // User's email
  17.  
  18. @ManyToMany
  19. @JoinTable( name="usergroup",
  20. joinColumns=@JoinColumn(name = "userid", referencedColumnName="userId"),
  21. inverseJoinColumns=@JoinColumn(name = "groupid")
  22. )
  23. List<Group> groups;
  24.  
  25. ...
  26. }
  27.  
  28. // Group
  29. @Entity
  30. @Table(name="grouptable")
  31. public class Group implements Serializable {
  32. @Id
  33. String groupId // Unique identifier for group.
  34. String description // Brief text describing the group purposes.
  35.  
  36. @ManyToMany(mappedBy="groups")
  37. @Cascade(SAVE_UPDATE)
  38. List<User> users
  39.  
  40. ....
  41. }
  42.  
  43. @Test
  44. @Transactional
  45. public void testGetUserByEmail(){
  46. Session session = factory.getCurrentSession();
  47.  
  48. String email = "sarah.silverman@comedycentral.com";
  49. User user = createUser();
  50. user.setWeCanContact(true);
  51. user.setPartnersCanContact(false);
  52. user.setAgreedTerms(false);
  53. user.setReferer("Laura");
  54.  
  55. String groupId = "AARP";
  56. String description = "Appletalk Address Resolution Protocol";
  57. Group group1 = new Group();
  58. group1.setGroupId(groupId);
  59. group1.setDescription(description);
  60. session.save(group1);
  61. session.flush();
  62. user.getGroupLists().add(group1);
  63. session.saveOrUpdate(user);
  64.  
  65. User testUser = userRepository.getUserByEmail(email);
  66. assertNotNull(testUser);
  67. assertEquals("It's not the correct email", email, testUser.getEmail());
  68. }
  69. private User createUser(){
  70. Session session = factory.getCurrentSession();
  71. String userId = "UB:2010";
  72. String userPassword = "sarah";
  73. String realName = "Sarah Silverman";
  74. String email = "sarah.silverman@comedycentral.com";
  75.  
  76. User user = new User();
  77. user.setUserId(userId);
  78. user.setUserName(email);
  79. user.setUserPassword(userPassword);
  80. user.setRealName(realName);
  81. user.setEmail(email);
  82.  
  83. List<Group> emptyGroupLists = new ArrayList<Group>();
  84. user.setGroupLists(emptyGroupLists);
  85. Group group = new Group();
  86. group.setGroupId("ABC");
  87. group.setDescription("A for apple");
  88. user.getGroupLists().add(group);
  89. session.save(group);
  90. session.save(user);
  91.  
  92. session.flush();
  93. return user;
  94. }
  95. // Repository Method to get user from email
  96. //
  97. // @NamedQuery(name="user.findByEmail",
  98. // query="SELECT u FROM User u " +
  99. // "LEFT JOIN FETCH u.groupLists " +
  100. // "WHERE upper(u.email) = :email")
  101.  
  102. public User getUserByEmail(String email) {
  103. Session session = sessionFactory.getCurrentSession();
  104. Query query = session.getNamedQuery("user.findByEmail");
  105. query.setParameter("email", email.toUpperCase());
  106. User user = (User)query.uniqueResult();
  107. return user;
  108. }
Add Comment
Please, Sign In to add comment