Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用springboot和hibernate从mysql数据库检索的日志数据出现问题_Java_Spring_Hibernate_Spring Boot_Jpa - Fatal编程技术网

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

Java 使用springboot和hibernate从mysql数据库检索的日志数据出现问题,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我试图使用Springboot REST api从mysql数据库检索日志,但是日志是多次重复的,而不是一行。我的数据库中只有一行数据,但当使用GET调用它时,它会作为重复日志出现。检查图片以查看它: 下面是生成这些日志的实体类的代码。第一个是用户日志: package com.dafe.spring.applogger.entity; import java.util.ArrayList; import java.util.List; import javax.persistence.C

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

下面是生成这些日志的实体类的代码。第一个是用户日志:

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 + "]";
    }

}

下面是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.dao;
导入java.util.List;
导入javax.persistence.EntityManager;
导入org.hibernate.Session;
导入org.hibernate.query.query;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
导入com.dafe.spring.applogger.entity.UserLog;
@存储库
公共类UserLogDAO HibernateImplementation实现UserLogDAO{
//为实体管理器定义字段
私人实体管理者实体管理者;
//设置构造函数注入
@自动连线
公共用户LogDaoHibernateImplementation(实体管理器实体管理器){
实体管理器=实体管理器;
}
@凌驾
公共列表findAll(){
//从实体管理器获取当前休眠会话
会话currentSession=entityManager.unwrap(会话.class);
//创建查询
查询查询=
currentSession.createQuery(“来自UserLog”,UserLog.class);
//执行查询并获取结果列表
List userLog=theQuery.getResultList();
//返回结果
返回用户日志;
}
@凌驾
公共用户日志findById(int-theId){
//获取当前会话
会话currentSession=entityManager.unwrap(会话.class);
//获取用户日志
用户日志用户日志=
获取(UserLog.class,theId);
//返回用户日志
返回null;
}
@凌驾
公共作废保存(用户日志用户日志){
//获取当前会话
会话currentSession=entityManager.unwrap(会话.class);
//拯救
currentSession.save或update(用户日志);
}
@凌驾
公共void deleteById(intheid){
//获取当前休眠会话
会话currentSession=entityManager.unwrap(会话.class);
//使用主键删除对象
查询查询=
createQuery(“从日志中删除,其中id=:theuserId”);
setParameter(“theuserId”,theId);
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();

    }

}

包com.dafe.spring.applogger.rest;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入com.dafe.spring.applogger.dao.UserLogDAO;
导入com.dafe.spring.applogger.entity.UserLog;
//此api选择所有
@RestController
@请求映射(“/api”)
公共类UserLogRestController{
@自动连线
私有UserLogDAO UserLogDAO;
//使用构造函数注入注入logDao
公共UserLogRestController(UserLogDAO theUserLogDao){
}
//公开日志并返回日志列表
@GetMapping(“/userLog”)
公共列表findAll(){
返回userLogDao.findAll();
}
}

请帮我弄清楚。提前感谢

我最近遇到了这个问题,我已经修复了将这个附件放在@JsonIgnore上的问题 在人际关系方面,如果它对你也适用,你可以试试

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


 @JsonIgnore
 @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
 private List<Action>action;
@JsonIgnore
@许多酮
@JoinColumn(name=“log\u id”)
私有用户日志;
@杰索尼奥雷
@OneToMany(mappedBy=“userLog”,cascade=CascadeType.ALL)
私人诉讼;
 @JsonIgnore
 @ManyToOne
 @JoinColumn(name="log_id")
 private UserLog userLog;


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