开发者社区> 问答> 正文

使用springboot和hibernate从mysql db检索的日志数据出现问题

我正在尝试使用Springboot REST API从mysql数据库中检索日志,但是日志以多次重复出现,而不仅仅是一行。我的数据库中只有一行数据,但是当使用GET调用它时,它会作为重复日志出现。检查图片以查看它:

以下是生成这些日志的实体类的代码。第一个是UserLog:

package com.dafe.spring.applogger.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="log")
public class UserLog {

    //define field

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="user_id")
    private String userId;

    @Column(name="session_id")
    private String sessionId;

    @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
        private List<Action>action;
    //define constructors

    public UserLog() {

    }

    public UserLog(String userId, String sessionId) {
        this.userId = userId;
        this.sessionId = sessionId;
    }
    //define getters and setters


    public String getUserId() {
        return userId;
    }


    public void setUserId(String userId) {
        this.userId = userId;
    }


    public String getSessionId() {
        return sessionId;
    }


    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public List<Action> getAction() {
        return action;
    }


    public void setAction(List<Action> action) {
        this.action = action;
    }

    @Override
    public String toString() {
        return "Log [userId=" + userId + ", sessionId=" + sessionId + "]";
    }

}

这是另一个实体类Action:

package com.dafe.spring.applogger.entity;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="action")
public class Action {

    //declare & annotate your fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
    private int id;

@Column(name="time")
    private Timestamp time;

@Column(name="type")
    private String type;


@ManyToOne
@JoinColumn(name="log_id")
private UserLog userLog;

public Action(int id, Timestamp time, String type, UserLog userLog) {
    this.id = id;
    this.time = time;
    this.type = type;
    this.userLog = userLog;
}

    //create and generate constructor
    public Action() {

    }

    public Action(Timestamp time, String type, UserLog userLog) {
        this.time = time;
        this.type = type;
        this.userLog = userLog;
    }

    //generate getters and setters

    public Timestamp getTime() {
        return time;
    }

    public void setTime(Timestamp time) {
        this.time = time;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public UserLog getUserLog() {
        return userLog;
    }

    public void setUserLog(UserLog userLog) {
        this.userLog = userLog;
    }
    //generate toString 

    @Override
    public String toString() {
        return "Action [time=" + time + ", type=" + type + ", userLog=" + userLog + "]";
    }


}

这是dao实现类

package com.dafe.spring.applogger.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.dafe.spring.applogger.entity.UserLog;

@Repository
public class UserLogDaoHibernateImplementation implements UserLogDAO {


    //define field for entity manager
    private EntityManager entityManager;

    //set up constructor injection
    @Autowired
    public UserLogDaoHibernateImplementation(EntityManager theEntityManager) {

    entityManager= theEntityManager;
    }

    @Override
    public List<UserLog> findAll() {

        //get the current hibernate session from entity manager
        Session currentSession = entityManager.unwrap(Session.class);


        //create a query
        Query <UserLog> theQuery = 
                currentSession.createQuery("from UserLog", UserLog.class);

        //execute query and get result list

        List<UserLog> userLog = theQuery.getResultList();

        //return the results

        return userLog;
    }

    @Override
    public UserLog findById(int theId) {

        //get the current session
        Session currentSession = entityManager.unwrap(Session.class);

        //get the userLog
        UserLog userLog = 
                currentSession.get(UserLog.class, theId);


        //return the userLog

        return null;
    }

    @Override
    public void save(UserLog theUserLog) {
        //get the current session
        Session currentSession = entityManager.unwrap(Session.class);

        //save
    currentSession.saveOrUpdate(theUserLog);

    }

    @Override
    public void deleteById(int theId) {
        //get the current hibernate session
                Session currentSession = entityManager.unwrap(Session.class);

    //delete object with primary key
                Query theQuery = 
                        currentSession.createQuery("delete from log where id=:theuserId");

                theQuery.setParameter("theuserId", theId);

                theQuery.executeUpdate();

    }

} 

和其余的控制器

package com.dafe.spring.applogger.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dafe.spring.applogger.dao.UserLogDAO;
import com.dafe.spring.applogger.entity.UserLog;
// this api selects all the 
@RestController
@RequestMapping("/api")
public class UserLogRestController {

    @Autowired
    private UserLogDAO userLogDao;

    //inject logDao using constructor injection
        public UserLogRestController(UserLogDAO theUserLogDao) {

    }

    //expose logs and return list of logs
    @GetMapping("/userLog")
    public List<UserLog> findAll(){

        return userLogDao.findAll();

    }

}

请帮我解决这个问题。提前致谢

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 20:23:04 606 0
1 条回答
写回答
取消 提交回答
  • 我最近遇到了这个问题,并且已经修复了将这个@@ sonsongnore放在关系中的问题,您可以尝试一下是否也适合您

     @JsonIgnore
     @ManyToOne
     @JoinColumn(name="log_id")
     private UserLog userLog;
    
     @JsonIgnore
     @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
     private List<Action>action;
    
    

    回答来源:Stack Overflow

    2020-03-25 20:23:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像