带有compossed Id的表中的递归引用给出了映射问题

时间:2011-09-05 23:19:54

标签: java hibernate recursion hibernate-mapping

现在我真的迷失了......或者很困惑......我得到了这个课程

            package co.com.adv.Salarix2.core.nominaprocessor.model;
            // Generated 23/08/2011 03:13:44 PM by Hibernate Tools 3.2.4.GA

            import javax.persistence.Column;
            import javax.persistence.Embeddable;

            /**
             * NodoId generated by hbm2java
             */
            @Embeddable
            public class NodoId implements java.io.Serializable {

                private int idArbol;
                private int idNodo;
                private int idHoja;

                public NodoId() {
                }

                public NodoId(int idArbol, int idNodo, int idHoja) {
                    this.idArbol = idArbol;
                    this.idNodo = idNodo;
                    this.idHoja = idHoja;
                }

                @Column(name = "Id_Arbol", nullable = false)
                public int getIdArbol() {
                    return this.idArbol;
                }

                public void setIdArbol(int idArbol) {
                    this.idArbol = idArbol;
                }

                @Column(name = "Id_Nodo", nullable = false)
                public int getIdNodo() {
                    return this.idNodo;
                }

                public void setIdNodo(int idNodo) {
                    this.idNodo = idNodo;
                }

                @Column(name = "Id_Hoja", nullable = false)
                public int getIdHoja() {
                    return this.idHoja;
                }

                public void setIdHoja(int idHoja) {
                    this.idHoja = idHoja;
                }

                public boolean equals(Object other) {
                    if ((this == other))
                        return true;
                    if ((other == null))
                        return false;
                    if (!(other instanceof NodoId))
                        return false;
                    NodoId castOther = (NodoId) other;

                    return (this.getIdArbol() == castOther.getIdArbol())
                            && (this.getIdNodo() == castOther.getIdNodo())
                            && (this.getIdHoja() == castOther.getIdHoja());
                }

                public int hashCode() {
                    int result = 17;

                    result = 37 * result + this.getIdArbol();
                    result = 37 * result + this.getIdNodo();
                    result = 37 * result + this.getIdHoja();
                    return result;
                }

                public String getStringRep() {
                    return this.idArbol + "-" + this.idHoja + "-" + this.idNodo;
                }

            }

如果我保留最后一种方法......

                public String getStringRep() {
                    return this.idArbol + "-" + this.idHoja + "-" + this.idNodo;
                }

它引发了maping异常:

部署错误:   部署“persistence.unit:unitName = Salarix2.ear / Salarix2.jar#Salarix2”由于以下原因而出错:org.hibernate.AnnotationException:referencedColumnNames(Id_Arbol,Id_ N.com,Id_Hoja)的co.com.adv.Salarix2.core.nominaprocessor.model.Nodo.nodo引用co.com.adv.Salarix2.core.nominaprocessor.model.Nodo未映射到单个属性

但如果我删除该方法,它部署好......为什么会发生这种情况???

1 个答案:

答案 0 :(得分:0)

这是因为Hibernate使用Reflection并认为Classes是“bean”。 Beans有一个标准的getter和setter命名法,如果你有一个像getStringRep这样的getter,hibernate将尝试找到一个名为StringRep的属性 - 你没有 - 为了避免这种情况,只使用非标准名称作为非持久性属性,例如stringRep()而不是getStringRep()(或使用@Transient注释而不是getter)。这应该可以解决你的问题。