Guest User

Untitled

a guest
Jul 31st, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1. Internal server error on Jackson @ResponseBody
  2. /* User contains information about a user of this site, that exists only
  3. * in the context of this site (no names, addresses).
  4. */
  5. @Entity(name="User")
  6. @Table(name="USER")
  7. @NamedQuery(
  8. name="findUserByName",
  9. query="SELECT OBJECT(u) FROM User u WHERE u.name = :name"
  10. )
  11. public class User extends AuditableEntity implements Serializable {
  12.  
  13. private static final long serialVersionUID = -1308795024222223320L;
  14.  
  15. @Id
  16. @GeneratedValue(strategy = GenerationType.AUTO)
  17. @Column(name="id")
  18. private Long id;
  19.  
  20.  
  21. @NotEmpty
  22. @MinSkipEmpty(value=6)
  23. @MaxSkipEmpty(value=32)
  24. @Column(name="name", length=32)
  25. private String name;
  26.  
  27. @NotEmpty
  28. @MinSkipEmpty(value=4)
  29. @MaxSkipEmpty(value=40)
  30. @Column(name="password", length=40)
  31. private String password;
  32.  
  33. @Column(name="salt", length=40)
  34. private String salt;
  35.  
  36.  
  37. @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST})
  38. @JoinColumn(name="person_id")
  39. private Person person;
  40.  
  41. @Column(name="last_login")
  42. private Date lastLogin;
  43.  
  44. @ElementCollection(fetch=FetchType.EAGER)
  45. @CollectionTable(name ="USER_AUTHORITY")
  46. @Column(name="authority")
  47. private List<Integer> authorities;
  48.  
  49.  
  50. public Long getId() {
  51. return id;
  52. }
  53.  
  54. public void setId(Long id) {
  55. this.id = id;
  56. }
  57.  
  58. public String getName() {
  59. return name;
  60. }
  61.  
  62. public void setName(String name) {
  63. this.name = (name == null ? name : name.trim());
  64.  
  65. }
  66.  
  67. public String getPassword() {
  68. return password;
  69. }
  70.  
  71. public void setPassword(String password) {
  72. this.password = (password == null ? password : password.trim());
  73. }
  74.  
  75. public String getSalt() {
  76. return salt;
  77. }
  78.  
  79. public void setSalt(String salt) {
  80. this.salt = salt;
  81. }
  82.  
  83. public Person getPerson() {
  84. return person;
  85. }
  86.  
  87. public void setPerson(Person person) {
  88. this.person = person;
  89. }
  90.  
  91. public Date getLastLogin() {
  92. return lastLogin;
  93. }
  94.  
  95. public void setLastLogin(Date lastLogin) {
  96. this.lastLogin = lastLogin;
  97. }
  98.  
  99. public List<Integer> getAuthorities() {
  100. return authorities;
  101. }
  102.  
  103. public void setAuthorities(List<Integer> authorities) {
  104. this.authorities = authorities;
  105. }
  106.  
  107. }
  108.  
  109. @Entity(name = "Person")
  110. @Table(name = "PERSON")
  111. public class Person extends AuditableEntity implements Serializable {
  112.  
  113. private static final long serialVersionUID = -1308795024262635690L;
  114.  
  115. @Id
  116. @GeneratedValue(strategy = GenerationType.AUTO)
  117. @Column(name = "id")
  118. private Long id;
  119.  
  120. @NotEmpty
  121. @MaxSkipEmpty(value=64)
  122. @Column(name = "firstName", length=64)
  123. private String firstName;
  124.  
  125. @NotEmpty
  126. @MaxSkipEmpty(value=64)
  127. @Column(name = "lastName", length=64)
  128. private String lastName;
  129.  
  130. @NotEmpty
  131. @Email
  132. @MaxSkipEmpty(value=256, message="")
  133. @Column(name = "email", length=256)
  134. private String email;
  135.  
  136. @DateTimeFormat(pattern="MM/dd/yyyy")
  137. @NotNull(message = "Required field")
  138. @Column(name = "date")
  139. private Date birthday;
  140.  
  141. @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST })
  142. @JoinColumn(name = "location_id")
  143. private Location location;
  144.  
  145.  
  146. public Person() {
  147.  
  148. }
  149.  
  150. public Person(String firstName, String lastName) {
  151. super();
  152. this.firstName = firstName;
  153. this.lastName = lastName;
  154. }
  155.  
  156. public Long getId() {
  157. return id;
  158. }
  159.  
  160. public void setId(Long id) {
  161. this.id = id;
  162. }
  163.  
  164. public String getFirstName() {
  165. return firstName;
  166. }
  167.  
  168. public void setFirstName(String firstName) {
  169. this.firstName = firstName;
  170. }
  171.  
  172. public String getLastName() {
  173. return lastName;
  174. }
  175.  
  176. public void setLastName(String lastName) {
  177. this.lastName = lastName;
  178. }
  179.  
  180. public String getEmail() {
  181. return email;
  182. }
  183.  
  184. public void setEmail(String email) {
  185. this.email = email;
  186. }
  187.  
  188. public Location getLocation() {
  189. return location;
  190. }
  191.  
  192. public void setLocation(Location location) {
  193. this.location = location;
  194. }
  195.  
  196. public Date getBirthday() {
  197. return birthday;
  198. }
  199.  
  200. public void setBirthday(Date birthday) {
  201. this.birthday = birthday;
  202. }
  203.  
  204. @Override
  205. public String toString() {
  206.  
  207. return super.toString() + " name = " + firstName + " " + lastName
  208. + " id = " + id;
  209. }
  210.  
  211. @Override
  212. public int hashCode() {
  213. final int prime = 31;
  214. int result = 1;
  215. result = prime * result
  216. + ((firstName == null) ? 0 : firstName.hashCode());
  217. result = prime * result + ((id == null) ? 0 : id.hashCode());
  218. result = prime * result
  219. + ((lastName == null) ? 0 : lastName.hashCode());
  220. return result;
  221. }
  222.  
  223. @Override
  224. public boolean equals(Object obj) {
  225. if (this == obj)
  226. return true;
  227. if (obj == null)
  228. return false;
  229. if (getClass() != obj.getClass())
  230. return false;
  231. Person other = (Person) obj;
  232. if (firstName == null) {
  233. if (other.firstName != null)
  234. return false;
  235. } else if (!firstName.equals(other.firstName))
  236. return false;
  237. if (id == null) {
  238. if (other.id != null)
  239. return false;
  240. } else if (!id.equals(other.id))
  241. return false;
  242. if (lastName == null) {
  243. if (other.lastName != null)
  244. return false;
  245. } else if (!lastName.equals(other.lastName))
  246. return false;
  247. return true;
  248. }
  249.  
  250. }
  251.  
  252. @Entity(name = "Location")
  253. @Table(name = "LOCATION")
  254. public class Location extends AuditableEntity implements Serializable {
  255.  
  256. @Id
  257. @GeneratedValue(strategy = GenerationType.AUTO)
  258. @Column(name = "id")
  259. private Long id;
  260.  
  261. //name of person/place/thing
  262.  
  263. @Column(name = "name", length=128)
  264. String name;
  265.  
  266. //street address, p.o. box, company name, c/o
  267. @NotEmpty
  268. @MaxSkipEmpty(value=128)
  269. @Column(name = "line_1", length=128)
  270. String line1;
  271.  
  272. // apt., suite, building, floor, entrance, etc.
  273. @Column(name = "line_2", length=128)
  274. String line2;
  275.  
  276. @NotEmpty
  277. @MaxSkipEmpty(value=64)
  278. @Column(name = "city", length=64)
  279. String city;
  280.  
  281. // state, providence, region
  282. @NotEmpty
  283. @MaxSkipEmpty(value=40)
  284. @Column(name = "state", length=40)
  285. String state;
  286.  
  287. // postal code
  288. @NotEmpty
  289. @MaxSkipEmpty(value=16)
  290. @Column(name = "zip", length=16)
  291. String zip;
  292.  
  293. @Column(name = "country")
  294. String country;
  295.  
  296. public Long getId() {
  297. return id;
  298. }
  299.  
  300. public void setId(Long id) {
  301. this.id = id;
  302. }
  303.  
  304. public String getName() {
  305. return name;
  306. }
  307.  
  308. public void setName(String name) {
  309. this.name = name;
  310. }
  311.  
  312. public String getLine1() {
  313. return line1;
  314. }
  315.  
  316. public void setLine1(String line1) {
  317. this.line1 = line1;
  318. }
  319.  
  320. public String getLine2() {
  321. return line2;
  322. }
  323.  
  324. public void setLine2(String line2) {
  325. this.line2 = line2;
  326. }
  327.  
  328. public String getCity() {
  329. return city;
  330. }
  331.  
  332. public void setCity(String city) {
  333. this.city = city;
  334. }
  335.  
  336. public String getState() {
  337. return state;
  338. }
  339.  
  340. public void setState(String state) {
  341. this.state = state;
  342. }
  343.  
  344. public String getZip() {
  345. return zip;
  346. }
  347.  
  348. public void setZip(String zip) {
  349. this.zip = zip;
  350. }
  351.  
  352. public String getCountry() {
  353. return country;
  354. }
  355.  
  356. public void setCountry(String country) {
  357. this.country = country;
  358. }
  359.  
  360. private static final long serialVersionUID = -178898928354655555L;
  361. }
  362.  
  363. @RequestMapping(value="user/{documentId}", method=RequestMethod.GET)
  364. public @ResponseBody User getUserForDocument( Model model, @PathVariable("documentId") Long docId){
  365.  
  366. Document doc = null;
  367. try{
  368. doc = dService.find(docId);
  369. }catch(Exception e){
  370. Logger logger = Logger.getLogger(DocumentController.class);
  371. logger.error(e.getMessage());
  372. }
  373. User user = doc.getUser();
  374.  
  375. user.getPerson();
  376. user.getPerson().getLocation();
  377. return user;
  378. }
  379.  
  380. @Repository()
  381. public class DocumentDaoImpl implements DocumentDao {
  382.  
  383. @PersistenceContext
  384. private EntityManager entityManager;
  385.  
  386. @Transactional
  387. public Document find(Long id) {
  388.  
  389. Document doc = entityManager.find(Document.class, id);
  390.  
  391. Hibernate.initialize(doc.getUser());
  392. Hibernate.initialize(doc.getUser().getPerson());
  393. Hibernate.initialize(doc.getUser().getPerson().getLocation());
  394.  
  395. return doc;
  396. }
  397.  
  398. @SuppressWarnings("unchecked")
  399. @Transactional
  400. public List<Document> getUnassignedDocumentsForUser(User user) {
  401.  
  402. Query query = entityManager.createQuery(new StringBuffer()
  403. .append("select d from Document d WHERE d.user = :user ")
  404. .append("AND NOT d IN( SELECT d from Book b, IN(b.docs) bd WHERE bd.id = d.id )")
  405. .append("").toString());
  406. query.setParameter("user", user);
  407. List<Document> tmp = (ArrayList<Document>) query.getResultList();
  408. for(Document doc : tmp){
  409. Hibernate.initialize(doc);
  410. Hibernate.initialize(doc.getUser());
  411. Hibernate.initialize(doc.getUser().getPerson());
  412. Hibernate.initialize(doc.getUser().getPerson().getLocation());
  413. entityManager.detach(doc);
  414. entityManager.detach(doc.getUser());
  415. entityManager.detach(doc.getUser().getPerson());
  416. entityManager.detach(doc.getUser().getPerson().getLocation());
  417. }
  418. return tmp;
  419. }
  420.  
  421. @Transactional
  422. public Document save(Document doc) {
  423.  
  424. if (doc.getId() == null) {
  425. entityManager.persist(doc);
  426. return doc;
  427. } else {
  428. return entityManager.merge(doc);
  429. }
  430. }
  431.  
  432. public EntityManager getEntityManager() {
  433. return entityManager;
  434. }
  435.  
  436. public void setEntityManager(EntityManager entityManager) {
  437. this.entityManager = entityManager;
  438. }
  439.  
  440. }
  441.  
  442. @Component
  443. public class BWHandlerExceptionResolver extends SimpleMappingExceptionResolver implements InitializingBean{
  444.  
  445. public void afterPropertiesSet() throws Exception {
  446. Properties props = new Properties();
  447. props.put(Exception.class.getName(),"error");
  448. this.setExceptionMappings(props);
  449.  
  450. }
Add Comment
Please, Sign In to add comment