Guest User

Untitled

a guest
Sep 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. @MappedSuperclass
  2. public class BaseEntity implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4.  
  5. @Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
  6. @Column(name = "id", nullable = false)
  7. private long id;
  8.  
  9. @UpdateTimestamp @Column(name = "update_timestamp", nullable = false)
  10. @Temporal(TemporalType.TIMESTAMP)
  11. private Date updateTimestamp;
  12.  
  13. public long getId() {
  14. return id;
  15. }
  16.  
  17. public void setId(long id) {
  18. this.id = id;
  19. }
  20.  
  21. public Date getUpdateTimestamp() {
  22. return updateTimestamp;
  23. }
  24.  
  25. public void setUpdateTimestamp(Date updateTimestamp) {
  26. this.updateTimestamp = updateTimestamp;
  27. }
  28. }
  29.  
  30. @Entity @Table(name = "USER_ACCOUNT")
  31. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  32. @AttributeOverride(name = "id", column = @Column(name="user_id"))
  33. public class UserAccount extends BaseEntity {
  34. private static final long serialVersionUID = 1234567890L;
  35.  
  36. @Column(name = "username", unique = true) //username has to be unique
  37. private String username;
  38.  
  39. @Column(name = "first_name")
  40. private String firstName;
  41.  
  42. @Column(name = "last_name")
  43. private String lastName;
  44.  
  45. @Column(name = "age")
  46. private int age;
  47.  
  48. @Column(name = "gender")
  49. private String gender;
  50.  
  51. @Column(name = "password")
  52. private String password;
  53.  
  54. @OneToMany(mappedBy = "userAccountRolePk.role", cascade = CascadeType.ALL)
  55. private Set<UserAccountRole> userAccountRoles = new HashSet<>();
  56.  
  57. public Set<UserAccountRole> getUserAccountRoles() {
  58. return userAccountRoles;
  59. }
  60.  
  61. public void setUserAccountRoles(Set<UserAccountRole> r) {
  62. this.userAccountRoles = r;
  63. }
  64.  
  65. public void addUserAccountRoles(UserAccountRole r) {
  66. this.userAccountRoles.add(r);
  67. }
  68.  
  69. public String getPassword() {
  70. return password;
  71. }
  72.  
  73. public void setPassword(String password) {
  74. this.password = password;
  75. }
  76.  
  77. public String getUsername() {
  78. return username;
  79. }
  80.  
  81. public void setUsername(String username) {
  82. this.username = username;
  83. }
  84.  
  85. public String getFirstName() {
  86. return firstName;
  87. }
  88.  
  89. public void setFirstName(String firstName) {
  90. this.firstName = firstName;
  91. }
  92.  
  93. public String getLastName() {
  94. return lastName;
  95. }
  96.  
  97. public void setLastName(String lastName) {
  98. this.lastName = lastName;
  99. }
  100.  
  101. public int getAge() {
  102. return age;
  103. }
  104.  
  105. public void setAge(int age) {
  106. this.age = age;
  107. }
  108.  
  109. public String getGender() {
  110. return gender;
  111. }
  112.  
  113. public void setGender(String gender) {
  114. this.gender = gender;
  115. }
  116. }
  117.  
  118. @Entity
  119. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  120. @AttributeOverride(name = "id", column = @Column(name="role_id"))
  121. public class Role extends BaseEntity {
  122. private static final long serialVersionUID = 1234567893L;
  123.  
  124. @Column(name = "name")
  125. private String name;
  126.  
  127. @Column(name = "description")
  128. private String description;
  129.  
  130. /* if true, then cannot be updated by users or rest api */
  131. @Column(name = "immutable")
  132. private boolean immutable;
  133.  
  134. @OneToMany(mappedBy = "rolePriviledgePk.role", cascade = CascadeType.ALL)
  135. private Set<RolePriviledge> rolePriviledges = new HashSet<>();
  136.  
  137. public Set<RolePriviledge> getRolePriviledges() {
  138. return rolePriviledges;
  139. }
  140.  
  141. public void setRolePriviledges(Set<RolePriviledge> rolePriviledges) {
  142. this.rolePriviledges = rolePriviledges;
  143. }
  144.  
  145. public void addRolePriviledges(RolePriviledge rolePriviledge) {
  146. this.rolePriviledges.add(rolePriviledge);
  147. }
  148.  
  149. public String getName() {
  150. return name;
  151. }
  152.  
  153. public void setName(String name) {
  154. this.name = name;
  155. }
  156.  
  157. public String getDescription() {
  158. return description;
  159. }
  160.  
  161. public void setDescription(String description) {
  162. this.description = description;
  163. }
  164.  
  165. public boolean isImmutable() {
  166. return immutable;
  167. }
  168.  
  169. public void setImmutable(boolean immutable) {
  170. this.immutable = immutable;
  171. }
  172.  
  173. public static long getSerialversionuid() {
  174. return serialVersionUID;
  175. }
  176. }
  177.  
  178. @Entity
  179. @Table(name = "USERACCOUNT_ROLES")
  180. @AssociationOverrides(
  181. { @AssociationOverride(name = "userAccountRolePk.userAccount",
  182. joinColumns = @JoinColumn(name = "USER_ID")),
  183. @AssociationOverride(name = "userAccountRolePk.role",
  184. joinColumns = @JoinColumn(name = "ROLE_ID"))
  185. })
  186. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  187. public class UserAccountRole extends BaseEntity {
  188. //composite primary key
  189. @EmbeddedId
  190. private UserAccountRolePk userAccountRolePk = new UserAccountRolePk();
  191.  
  192. //additional fields if any (updateTimeStamp inherited from BaseEntity)
  193. private boolean immutable;
  194.  
  195. public UserAccountRolePk getUserAccountRolePk() {
  196. return userAccountRolePk;
  197. }
  198.  
  199. public void setUserAccountRolePk(UserAccountRolePk pk) {
  200. this.userAccountRolePk = pk;
  201. }
  202.  
  203. @Transient
  204. public UserAccount getUserAccount() {
  205. return getUserAccountRolePk().getUserAccount();
  206. }
  207.  
  208. public void setUserAccount(UserAccount u) {
  209. getUserAccountRolePk().setUserAccount(u);
  210. }
  211.  
  212. @Transient
  213. public Role getRole() {
  214. return getUserAccountRolePk().getRole();
  215. }
  216.  
  217. public void setRole(Role p) {
  218. getUserAccountRolePk().setRole(p);
  219. }
  220.  
  221. public boolean isImmutable() {
  222. return immutable;
  223. }
  224.  
  225. public void setImmutable(boolean immutable) {
  226. this.immutable = immutable;
  227. }
  228. }
  229.  
  230. @Embeddable
  231. public class UserAccountRolePk {
  232. @ManyToOne(cascade = CascadeType.ALL)
  233. private UserAccount userAccount;
  234.  
  235. @ManyToOne(cascade = CascadeType.ALL)
  236. private Role role;
  237.  
  238. public Role getRole() {
  239. return role;
  240. }
  241.  
  242. public void setRole(Role role) {
  243. this.role = role;
  244. }
  245.  
  246. public UserAccount getUserAccount() {
  247. return userAccount;
  248. }
  249.  
  250. public void setUserAccount(UserAccount x) {
  251. this.userAccount = x;
  252. }
  253.  
  254. public boolean equals(Object o) {
  255. if(o == null || o.getClass() != this.getClass()) {
  256. return false;
  257. }
  258.  
  259. UserAccountRolePk pk = (UserAccountRolePk) o;
  260. if(this.getRole().equals(pk.getRole()) && this.getUserAccount().equals(pk.getUserAccount())) {
  261. return true;
  262. }
  263.  
  264. return false;
  265. }
  266.  
  267. public int hashCode() {
  268. return new Long(this.getRole().getId() + this.getUserAccount().getId()).hashCode();
  269. }
  270. }
  271.  
  272. java.lang.IllegalArgumentException: This class [class demo.rbacapp.entity.UserAccountRole] does not define an IdClass
  273. at org.hibernate.jpa.internal.metamodel.AbstractIdentifiableType.getIdClassAttributes (AbstractIdentifiableType.java:183)
  274. at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation$IdMetadata.<init> (JpaMetamodelEntityInformation.java:253)
  275. at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init> (JpaMetamodelEntityInformation.java:84)
  276. at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation (JpaEntityInformationSupport.java:68)
  277. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation (JpaRepositoryFactory.java:173)
  278. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository (JpaRepositoryFactory.java:106)
  279. at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository (JpaRepositoryFactory.java:88)
  280. at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository (RepositoryFactorySupport.java:198)
  281. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn (RepositoryFactoryBeanSupport.java:277)
  282. at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet (RepositoryFactoryBeanSupport.java:263)
  283. at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet (JpaRepositoryFactoryBean.java:101)
  284. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods (AbstractAutowireCapableBeanFactory.java:1687)
  285. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1624)
  286. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:555)
Add Comment
Please, Sign In to add comment