使用Hibernate生成的Java Bean中的Java-ToString()导致StackOverflowError

时间:2015-05-18 10:43:03

标签: java hibernate

我是这个论坛的新手,所以我希望我没有做错任何事。 我一个月前开始学习Java,我有一点问题。 我有2个实体,我想用它的轨道打印CD,但我有这个例外: 线程“main”中的异常java.lang.StackOverflowError

我不知道如何纠正这个问题.. 我发布了代码和实体:

TbTrack

package it.tgi.cd.entities2;

// default package
// Generated 11-mag-2015 16.29.34 by Hibernate Tools 4.0.0

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

/**
 * TbTrack generated by hbm2java
 */
@Entity
@Table(name = "tb_track", catalog = "db_cd_store")
public class TbTrack implements java.io.Serializable {

    private Integer idTrack;
    private TbCd tbCd;
    private String title;

    public TbTrack() {
    }

    public TbTrack(TbCd tbCd, String title) {
        this.tbCd = tbCd;
        this.title = title;
    }

    @Override
    public String toString() {
        return "idTrack=" + idTrack + ", tbCd=" + tbCd + ", title=" + title;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id_track", unique = true, nullable = false)
    public Integer getIdTrack() {
        return this.idTrack;
    }

    public void setIdTrack(Integer idTrack) {
        this.idTrack = idTrack;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_cd", nullable = false)
    public TbCd getTbCd() {
        return this.tbCd;
    }

    public void setTbCd(TbCd tbCd) {
        this.tbCd = tbCd;
    }

    @Column(name = "title", nullable = false, length = 45)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

TBCD

package it.tgi.cd.entities2;

// default package
// Generated 11-mag-2015 16.29.34 by Hibernate Tools 4.0.0

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * TbCd generated by hbm2java
 */
@Entity
@Table(name = "tb_cd", catalog = "db_cd_store")
public class TbCd implements java.io.Serializable {

    private int idCd;
    private String title;
    private Set<TbTrack> tbTracks = new HashSet<TbTrack>(0);

    public TbCd() {
    }

    public TbCd(int idCd, String title) {
        this.idCd = idCd;
        this.title = title;
    }

    @Override
    public String toString() {
        return "idCd=" + idCd + ", title=" + title + ", track= " + tbTracks;
    }

    public TbCd(int idCd, String title, Set<TbTrack> tbTracks) {
        this.idCd = idCd;
        this.title = title;
        this.tbTracks = tbTracks;
    }

    @Id
    @Column(name = "id_cd", unique = true, nullable = false)
    public int getIdCd() {
        return this.idCd;
    }

    public void setIdCd(int idCd) {
        this.idCd = idCd;
    }

    @Column(name = "title", nullable = false, length = 45)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tbCd")
    public Set<TbTrack> getTbTracks() {
        return this.tbTracks;
    }

    public void setTbTracks(Set<TbTrack> tbTracks) {
        this.tbTracks = tbTracks;
    }

}

CDDAO2

package it.tgi.cd.dao2;

import it.tgi.cd.connection2.DBConnection2;
import it.tgi.cd.entities2.TbCd;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class CDDAO2 {
    public static void searchCD(String title) {
        Session session = DBConnection2.openConnection();
        String query = "FROM TbCd where title='" + title + "'";
        Query _query = session.createQuery(query);
        List<TbCd> list = _query.list();
        System.out.println(list);

        DBConnection2.closeConnection(session);

    }

}
你能帮帮我吗? 谢谢!

2 个答案:

答案 0 :(得分:1)

您正在轨道类中添加CD 字符串,并且您正在CD类中添加所有音轨字符串

这是一个无限递归:一个CD字符串包含一个轨道字符串(以及其他字符串),这个轨道字符串包含CD字符串(与它开始的相同!),它又包含轨道字符串,依此类推

解决方案是要么不在方法TbCd.toString()中包含曲目,要么在方法TbTrack.toString()中不包含CD。我建议使用后者,因为在组装CD字符串时可能会包含所有曲目:

public class TbCd implements java.io.Serializable {
    @Override
    public String toString() {
        return "idCd=" + idCd + ", title=" + title + ", track= " + tbTracks;
    }
}

public class TbTrack implements java.io.Serializable {
    @Override
    public String toString() {
        return "idTrack=" + idTrack + ", title=" + title;
    }
}

答案 1 :(得分:0)

当你这么做时非常简单:

@Override
public String toString() {
    return "idCd=" + idCd + ", title=" + title + ", track= " + tbTracks;
}

它试图打印你的TbTrack对象集,它也会覆盖toString方法,所以:

@Override
public String toString() {
    return "idTrack=" + idTrack + ", tbCd=" + tbCd + ", title=" + title;
}

所以发生了什么:

它打印轨道列表,调用每个轨道toString方法再次打印CD&#34; tbCD&#34;试图一次又一次地打印曲目uupps STACKOVERFLOW到达!!!

删除&#34; tbCD = + tbCD&#34;来自你的TbTrack类的toString,一切都很好。而是打印标题,例如;)

简单修复就是:

@Override
public String toString() {
    return "idTrack=" + idTrack + ", tbCd.Title=" + tbCd.title + ", title=" + title;
}