简体   繁体   中英

Hibernate One to many Relationship : the wrong number of column. should be 2

class 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(){

    }


}

Error: Caused by: org.hibernate.AnnotationException: A Foreign key refering com.java.spring.data.domain.UserMSTR from com.java.spring.data.domain.UserAttribute has the wrong number of column. should be 2

@OneToMany(mappedBy = "UserMSTR") 

Make sure there is a Reference variable like this: UserMSTR UserMSTR; in UserAttribute class (since you mention mappedBy = "UserMSTR" in UserMSTR class) .

May be your reference variable name is different from you used inside in mappedBy .

Hope this will work!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM