Hibernate ManyToOne - OneToMany循环?

时间:2014-04-22 16:06:01

标签: java hibernate many-to-one hibernate-annotations hibernate-onetomany

我是hibernate的新手,我使用注释

这是我的两张桌子:

enter image description here

这是我的表“Persona”

 @Entity
@Table(name = "persona")
public class Persona implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "codigo", unique = true, nullable = false)
    private int codigo;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "codTipoIdent", nullable = false)
    private TipoIdentificacion tipoIdentificacion;

    ....

这是我的第二张表“TipoIdentificacion”

@Entity
@Table(name = "tipoIdentificacion")
public class TipoIdentificacion implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "codigo", unique = true, nullable = false)
    private int codigo;

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

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tipoIdentificacion")    
    private Set<Persona> persona = new HashSet<Persona>(0);

“Persona”的主键是“codigo”,而“TipoIdentificacion”是“codigo”,它是来自persona的FK(codTipoIdent)

一切看起来都很好,我在我的网络应用程序中做得很好,但是当我调试请求时,“TipoIdentificacion”列表看起来像

enter image description here

它会循环,为什么?

1 个答案:

答案 0 :(得分:0)

这是预期的行为,因为您已在此处定义了双向关系。 您可以创建单向关系以避免这种情况 -

http://webdev.apl.jhu.edu/~jcs/ejava-javaee/coursedocs/605-784-site/docs/content/html/jpa-relationex-many2one-uni.html