Hibernate一对多关系:错误的列数。应该是2

时间:2017-04-26 07:27:56

标签: java hibernate

类UserMSTR:

package com.java.spring.data.domain;

    import java.io.Serializable;
    import java.util.Set;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;

    import org.springframework.data.jpa.domain.AbstractPersistable;

    @Entity
    public class UserMSTR extends AbstractPersistable<Long> implements Serializable{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue()
        private long userID;

        private String userName;

        private String password;

        @OneToMany(mappedBy = "UserMSTR")
        private Set<UserAttribute> userAttribute;

        public long getUserID() {
            return userID;
        }
        public void setUserID(long userID) {
            this.userID = userID;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }



        public Set<UserAttribute> getUserAttribute() {
            return userAttribute;
        }

        public void setUserAttribute(Set<UserAttribute> userAttribute) {
            this.userAttribute = userAttribute;
        }

    }

Class UserAttribute:

package com.java.spring.data.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
public class UserAttribute  extends AbstractPersistable<Long> implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String attrName;

    private String attrValue;

/*  @GenericGenerator(name = "generator", strategy = "foreign",
    parameters = @Parameter(name = "property", value = "USERMSTR"))
    @Id
    @GeneratedValue(generator = "generator")
    private String userID;*/

    @ManyToOne()
    @JoinColumn(name="userID")
    private UserMSTR userMstr;

    public String getAttrName() {
        return attrName;
    }

    public void setAttrName(String attrName) {
        this.attrName = attrName;
    }

    public String getAttrValue() {
        return attrValue;
    }

    public void setAttrValue(String attrValue) {
        this.attrValue = attrValue;
    }

    /*public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }*/

    public UserMSTR getUserMstr() {
        return userMstr;
    }

    public void setUserMstr(UserMSTR userMstr) {
        this.userMstr = userMstr;
    }

    /*public UserAttribute(String attrName, String attrValue, String userID, UserMSTR userMstr) {
        super();
        this.attrName = attrName;
        this.attrValue = attrValue;
        this.userID = userID;
        this.userMstr = userMstr;
    }*/
    public UserAttribute(){

    }


}

错误: 由以下原因引起:org.hibernate.AnnotationException:从com.java.spring.data.domain.UserAttribute引用com.java.spring.data.domain.UserMSTR的外键具有错误的列数。应该是2

1 个答案:

答案 0 :(得分:0)

@OneToMany(mappedBy = "UserMSTR") 

确保UserMSTR UserMSTR;类中有一个这样的Reference变量:UserAttribute(因为你在mappedBy = "UserMSTR"类中提到了UserMSTR)。

可能是您的参考变量名称与您在mappedBy中使用的名称不同。

希望这会起作用!!!!