从mysql数据库中使用springboot和hibernate检索的日志数据存在问题。

huangapple go评论197阅读模式
英文:

Issue with the log data retrieved from mysql db using springboot and hibernate

问题

以下是翻译好的部分:

UserLog.java:

  1. package com.dafe.spring.applogger.entity;
  2. import java.util.List;
  3. import javax.persistence.CascadeType;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.GenerationType;
  8. import javax.persistence.Id;
  9. import javax.persistence.OneToMany;
  10. import javax.persistence.Table;
  11. @Entity
  12. @Table(name="log")
  13. public class UserLog {
  14. //定义字段
  15. @Id
  16. @GeneratedValue(strategy=GenerationType.IDENTITY)
  17. @Column(name="id")
  18. private int id;
  19. @Column(name="user_id")
  20. private String userId;
  21. @Column(name="session_id")
  22. private String sessionId;
  23. @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
  24. private List<Action> action;
  25. //定义构造函数
  26. public UserLog() {
  27. }
  28. public UserLog(String userId, String sessionId) {
  29. this.userId = userId;
  30. this.sessionId = sessionId;
  31. }
  32. //定义getter和setter方法
  33. public String getUserId() {
  34. return userId;
  35. }
  36. public void setUserId(String userId) {
  37. this.userId = userId;
  38. }
  39. public String getSessionId() {
  40. return sessionId;
  41. }
  42. public void setSessionId(String sessionId) {
  43. this.sessionId = sessionId;
  44. }
  45. public List<Action> getAction() {
  46. return action;
  47. }
  48. public void setAction(List<Action> action) {
  49. this.action = action;
  50. }
  51. @Override
  52. public String toString() {
  53. return "Log [userId=" + userId + ", sessionId=" + sessionId + "]";
  54. }
  55. }

Action.java:

  1. package com.dafe.spring.applogger.entity;
  2. import java.sql.Timestamp;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.GenerationType;
  7. import javax.persistence.Id;
  8. import javax.persistence.JoinColumn;
  9. import javax.persistence.ManyToOne;
  10. import javax.persistence.Table;
  11. @Entity
  12. @Table(name="action")
  13. public class Action {
  14. //声明和注释字段
  15. @Id
  16. @GeneratedValue(strategy=GenerationType.IDENTITY)
  17. @Column(name="id")
  18. private int id;
  19. @Column(name="time")
  20. private Timestamp time;
  21. @Column(name="type")
  22. private String type;
  23. @ManyToOne
  24. @JoinColumn(name="log_id")
  25. private UserLog userLog;
  26. public Action(int id, Timestamp time, String type, UserLog userLog) {
  27. this.id = id;
  28. this.time = time;
  29. this.type = type;
  30. this.userLog = userLog;
  31. }
  32. //创建和生成构造函数
  33. public Action() {
  34. }
  35. public Action(Timestamp time, String type, UserLog userLog) {
  36. this.time = time;
  37. this.type = type;
  38. this.userLog = userLog;
  39. }
  40. //生成getter和setter方法
  41. public Timestamp getTime() {
  42. return time;
  43. }
  44. public void setTime(Timestamp time) {
  45. this.time = time;
  46. }
  47. public String getType() {
  48. return type;
  49. }
  50. public void setType(String type) {
  51. this.type = type;
  52. }
  53. public UserLog getUserLog() {
  54. return userLog;
  55. }
  56. public void setUserLog(UserLog userLog) {
  57. this.userLog = userLog;
  58. }
  59. //生成toString方法
  60. @Override
  61. public String toString() {
  62. return "Action [time=" + time + ", type=" + type + ", userLog=" + userLog + "]";
  63. }
  64. }

UserLogDaoHibernateImplementation.java:

  1. package com.dafe.spring.applogger.dao;
  2. import java.util.List;
  3. import javax.persistence.EntityManager;
  4. import org.hibernate.Session;
  5. import org.hibernate.query.Query;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Repository;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import com.dafe.spring.applogger.entity.UserLog;
  10. @Repository
  11. public class UserLogDaoHibernateImplementation implements UserLogDAO {
  12. //定义EntityManager字段
  13. private EntityManager entityManager;
  14. //通过构造函数注入
  15. @Autowired
  16. public UserLogDaoHibernateImplementation(EntityManager theEntityManager) {
  17. entityManager = theEntityManager;
  18. }
  19. @Override
  20. public List<UserLog> findAll() {
  21. //从EntityManager获取当前Hibernate会话
  22. Session currentSession = entityManager.unwrap(Session.class);
  23. //创建查询
  24. Query<UserLog> theQuery = currentSession.createQuery("from UserLog", UserLog.class);
  25. //执行查询并获取结果列表
  26. List<UserLog> userLog = theQuery.getResultList();
  27. //返回结果
  28. return userLog;
  29. }
  30. @Override
  31. public UserLog findById(int theId) {
  32. //获取当前会话
  33. Session currentSession = entityManager.unwrap(Session.class);
  34. //获取UserLog
  35. UserLog userLog = currentSession.get(UserLog.class, theId);
  36. //返回UserLog
  37. return null;
  38. }
  39. @Override
  40. public void save(UserLog theUserLog) {
  41. //获取当前会话
  42. Session currentSession = entityManager.unwrap(Session.class);
  43. //保存或更新
  44. currentSession.saveOrUpdate(theUserLog);
  45. }
  46. @Override
  47. public void deleteById(int theId) {
  48. //获取当前Hibernate会话
  49. Session currentSession = entityManager.unwrap(Session.class);
  50. //使用主键删除对象
  51. Query theQuery = currentSession.createQuery("delete from log where id=:theuserId");
  52. theQuery.setParameter("theuserId", theId);
  53. theQuery.executeUpdate();
  54. }
  55. }

UserLogRestController.java:

  1. package com.dafe.spring.applogger.rest;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.dafe.spring.applogger.dao.UserLogDAO;
  8. import com.dafe.spring.applogger.entity.UserLog;
  9. @RestController
  10. @RequestMapping("/api")
  11. public class UserLogRestController {
  12. @Autowired
  13. private UserLogDAO userLogDao;
  14. //使用构造函数注入userLogDao
  15. public UserLogRestController(UserLogDAO theUserLogDao) {
  16. }
  17. //公开logs并返回logs列表
  18. @GetMapping("/userLog")
  19. public List<UserLog> findAll() {
  20. return userLogDao.findAll();
  21. }
  22. }
英文:

I am trying to retrieve a log from mysql database using a Springboot REST api but the logs are coming in multiple repetitions instead of just one line. I only have data in one row in my database but when it gets called using the GET, it comes in as repetitive logs. Check the picture to see it:

从mysql数据库中使用springboot和hibernate检索的日志数据存在问题。

Below is the code for the entity classes that produced these logs. First one is UserLog:

  1. package com.dafe.spring.applogger.entity;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.persistence.CascadeType;
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.GenerationType;
  9. import javax.persistence.Id;
  10. import javax.persistence.OneToMany;
  11. import javax.persistence.Table;
  12. @Entity
  13. @Table(name=&quot;log&quot;)
  14. public class UserLog {
  15. //define field
  16. @Id
  17. @GeneratedValue(strategy=GenerationType.IDENTITY)
  18. @Column(name=&quot;id&quot;)
  19. private int id;
  20. @Column(name=&quot;user_id&quot;)
  21. private String userId;
  22. @Column(name=&quot;session_id&quot;)
  23. private String sessionId;
  24. @OneToMany(mappedBy=&quot;userLog&quot;,cascade=CascadeType.ALL)
  25. private List&lt;Action&gt;action;
  26. //define constructors
  27. public UserLog() {
  28. }
  29. public UserLog(String userId, String sessionId) {
  30. this.userId = userId;
  31. this.sessionId = sessionId;
  32. }
  33. //define getters and setters
  34. public String getUserId() {
  35. return userId;
  36. }
  37. public void setUserId(String userId) {
  38. this.userId = userId;
  39. }
  40. public String getSessionId() {
  41. return sessionId;
  42. }
  43. public void setSessionId(String sessionId) {
  44. this.sessionId = sessionId;
  45. }
  46. public List&lt;Action&gt; getAction() {
  47. return action;
  48. }
  49. public void setAction(List&lt;Action&gt; action) {
  50. this.action = action;
  51. }
  52. @Override
  53. public String toString() {
  54. return &quot;Log [userId=&quot; + userId + &quot;, sessionId=&quot; + sessionId + &quot;]&quot;;
  55. }
  56. }

Here is the other entity class Action:

  1. package com.dafe.spring.applogger.entity;
  2. import java.sql.Timestamp;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.GenerationType;
  7. import javax.persistence.Id;
  8. import javax.persistence.JoinColumn;
  9. import javax.persistence.ManyToOne;
  10. import javax.persistence.Table;
  11. @Entity
  12. @Table(name=&quot;action&quot;)
  13. public class Action {
  14. //declare &amp; annotate your fields
  15. @Id
  16. @GeneratedValue(strategy=GenerationType.IDENTITY)
  17. @Column(name=&quot;id&quot;)
  18. private int id;
  19. @Column(name=&quot;time&quot;)
  20. private Timestamp time;
  21. @Column(name=&quot;type&quot;)
  22. private String type;
  23. @ManyToOne
  24. @JoinColumn(name=&quot;log_id&quot;)
  25. private UserLog userLog;
  26. public Action(int id, Timestamp time, String type, UserLog userLog) {
  27. this.id = id;
  28. this.time = time;
  29. this.type = type;
  30. this.userLog = userLog;
  31. }
  32. //create and generate constructor
  33. public Action() {
  34. }
  35. public Action(Timestamp time, String type, UserLog userLog) {
  36. this.time = time;
  37. this.type = type;
  38. this.userLog = userLog;
  39. }
  40. //generate getters and setters
  41. public Timestamp getTime() {
  42. return time;
  43. }
  44. public void setTime(Timestamp time) {
  45. this.time = time;
  46. }
  47. public String getType() {
  48. return type;
  49. }
  50. public void setType(String type) {
  51. this.type = type;
  52. }
  53. public UserLog getUserLog() {
  54. return userLog;
  55. }
  56. public void setUserLog(UserLog userLog) {
  57. this.userLog = userLog;
  58. }
  59. //generate toString
  60. @Override
  61. public String toString() {
  62. return &quot;Action [time=&quot; + time + &quot;, type=&quot; + type + &quot;, userLog=&quot; + userLog + &quot;]&quot;;
  63. }
  64. }

here is the dao implementation class

  1. package com.dafe.spring.applogger.dao;
  2. import java.util.List;
  3. import javax.persistence.EntityManager;
  4. import org.hibernate.Session;
  5. import org.hibernate.query.Query;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Repository;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import com.dafe.spring.applogger.entity.UserLog;
  10. @Repository
  11. public class UserLogDaoHibernateImplementation implements UserLogDAO {
  12. //define field for entity manager
  13. private EntityManager entityManager;
  14. //set up constructor injection
  15. @Autowired
  16. public UserLogDaoHibernateImplementation(EntityManager theEntityManager) {
  17. entityManager= theEntityManager;
  18. }
  19. @Override
  20. public List&lt;UserLog&gt; findAll() {
  21. //get the current hibernate session from entity manager
  22. Session currentSession = entityManager.unwrap(Session.class);
  23. //create a query
  24. Query &lt;UserLog&gt; theQuery =
  25. currentSession.createQuery(&quot;from UserLog&quot;, UserLog.class);
  26. //execute query and get result list
  27. List&lt;UserLog&gt; userLog = theQuery.getResultList();
  28. //return the results
  29. return userLog;
  30. }
  31. @Override
  32. public UserLog findById(int theId) {
  33. //get the current session
  34. Session currentSession = entityManager.unwrap(Session.class);
  35. //get the userLog
  36. UserLog userLog =
  37. currentSession.get(UserLog.class, theId);
  38. //return the userLog
  39. return null;
  40. }
  41. @Override
  42. public void save(UserLog theUserLog) {
  43. //get the current session
  44. Session currentSession = entityManager.unwrap(Session.class);
  45. //save
  46. currentSession.saveOrUpdate(theUserLog);
  47. }
  48. @Override
  49. public void deleteById(int theId) {
  50. //get the current hibernate session
  51. Session currentSession = entityManager.unwrap(Session.class);
  52. //delete object with primary key
  53. Query theQuery =
  54. currentSession.createQuery(&quot;delete from log where id=:theuserId&quot;);
  55. theQuery.setParameter(&quot;theuserId&quot;, theId);
  56. theQuery.executeUpdate();
  57. }
  58. }

and the rest controller

  1. package com.dafe.spring.applogger.rest;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.dafe.spring.applogger.dao.UserLogDAO;
  8. import com.dafe.spring.applogger.entity.UserLog;
  9. // this api selects all the
  10. @RestController
  11. @RequestMapping(&quot;/api&quot;)
  12. public class UserLogRestController {
  13. @Autowired
  14. private UserLogDAO userLogDao;
  15. //inject logDao using constructor injection
  16. public UserLogRestController(UserLogDAO theUserLogDao) {
  17. }
  18. //expose logs and return list of logs
  19. @GetMapping(&quot;/userLog&quot;)
  20. public List&lt;UserLog&gt; findAll(){
  21. return userLogDao.findAll();
  22. }
  23. }

please help me figure this out. Thanks in advance

答案1

得分: 0

我最近遇到了这个问题,我已经通过在关联的地方添加这个注释 @JsonIgnore 来进行了修复,你也可以尝试看看是否对你有效。

  1. @JsonIgnore
  2. @ManyToOne
  3. @JoinColumn(name="log_id")
  4. private UserLog userLog;
  5. @JsonIgnore
  6. @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
  7. private List<Action> action;
英文:

I had this issue recently and I have fixed putting this anottation @JsonIgnore
at relationships, you can try if it works for you too

  1. @JsonIgnore
  2. @ManyToOne
  3. @JoinColumn(name=&quot;log_id&quot;)
  4. private UserLog userLog;
  5. @JsonIgnore
  6. @OneToMany(mappedBy=&quot;userLog&quot;,cascade=CascadeType.ALL)
  7. private List&lt;Action&gt;action;

huangapple
  • 本文由 发表于 2020年3月15日 06:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/60687780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定