使用Dozer进行集合映射时的无限循环

时间:2015-07-13 13:47:46

标签: java collections mapping infinite-loop dozer

我正在开发一个在前端使用BackboneJS而在后端使用Java-Spring Core的项目。我有一个关于将实体(域)对象映射到DTO对象的问题。我收到一条错误消息:

  

org.apache.cxf.interceptor.Fault:无限递归(StackOverflowError)(通过引用链:com.countdown.dto.CategoryDTO [“countdownList”] - > java.util.ArrayList [0] - > com .countdown.dto.CountdownDTO [ “类别”] - GT; .......

User.java

@Entity
@Table(name = "Users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "USER_ID", nullable = false)
    private int id;

    @Column(name = "EMAIL", nullable = false, unique = true)
    private String email;

    @Column(name = "NAME_SURNAME", nullable = false)
    private String nameSurname;

    @Column(name = "PASSWORD", nullable = false)
    private String password;

    @Column(name = "USERNAME", nullable = false, unique = true)
    private String username;

    @Column(name = "REGISTER_DATE", nullable = false)
    private Date registerDate;

    @ManyToOne
    @JoinColumn(name = "ROLE_ID")
    private Role role;

    @OneToMany(mappedBy = "createUser")
    private List<Countdown> createCountdownList = new ArrayList<Countdown>();

    @OneToMany(mappedBy = "updateUser")
    private List<Countdown> updateCountdownList = new ArrayList<Countdown>();

    @ManyToMany
    @JoinTable(name = "FOLLOWINGS",
            joinColumns = @JoinColumn(name = "USER_ID"),
            inverseJoinColumns = @JoinColumn(name = "COUNTDOWN_ID"))
    private List<Countdown> followings = new ArrayList<Countdown>();

    //Getters and setters..

}

Role.java

@Entity
public class Role implements Serializable {

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

    @Column(name = "ROLE_NAME", nullable = false)
    private String roleName;

    @OneToMany(mappedBy = "role",fetch = FetchType.LAZY)
    List<User> userList = new ArrayList<User>();
 }

Countdown.java

@Entity
@Table(name = "COUNTDOWN")
public class Countdown implements Serializable {

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

    @Column(name = "COUNTDOWN_NAME", nullable = false)
    private String countdownName;

    @Column(name = "COUNTDOWN_DATE", nullable = false)
    private Date countdownDate;

    @Column(columnDefinition = "varchar(5000)")
    private String countdownDescription;

    @JoinColumn(name = "CATEGORY_ID", nullable = false)
    @ManyToOne
    private Category category;

    @JoinColumn(name = "CREATE_USER", nullable = false)
    @ManyToOne
    private User createUser;

    @Column(name = "CREATE_DATE", nullable = false)
    private Date createDate;

    @JoinColumn(name = "UPDATE_USER", nullable = false)
    @ManyToOne
    private User updateUser;

    @Column(name = "UPDATE_DATE", nullable = false)
    private Date updateDate;

    @Column(name = "CREATE_USER_IP", nullable = false)
    private int createIP;

    @ManyToMany
    private List<User> followers = new ArrayList<User>();

}

Category.java

@Entity
@Table(name="CATEGORY")
public class Category implements Serializable{

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

    @Column(name = "CATEGORY_NAME" , nullable = false)
    private String categoryName;

    @OneToMany(mappedBy = "category")
    private List<Countdown> countdownList = new ArrayList<Countdown>();

}

业务逻辑: CategoryServiceImpl.java 我在forEach循环中遇到错误。

@Transactional(readOnly = true)
public List<CategoryDTO> getAllCategories() {
    List<Category> categoryList;
    List<CategoryDTO> categoryDTOList = new ArrayList<CategoryDTO>();

    logger.debug("getAllCategories called");

    try {
        categoryList = categoryDAO.findAll();
        for(Category category : categoryList){
            categoryDTOList.add(mapper.map(category,CategoryDTO.class));
        }
    }catch (NoResultException e){
        logger.error("getAllCategories method : No Category wasn't found");
        logger.warn(e,e);
    }catch (Exception e){
        logger.error("getAllCategories method : Categories wasn't found");
        logger.warn(e,e);
    }
    return categoryDTOList;
}

我是否必须在表示层中使用DTO对象?我可以在表示层而不是DTO对象中使用实体对象吗?

我该如何解决这个问题?对不起我的英文不好。谢谢!

1 个答案:

答案 0 :(得分:1)

请尝试:

Map<String, String> map = new HashMap<>();
map.add("New York".toLowerCase() ,"New York");
map.containsKey("new york".toLowerCase()) == true;
map.containsKey("NEW YORK".toLowerCase()) == true;

map.values().each(it -> print it) ---> prints "New York"