java - Hibernate一对多关系: the wrong number of column.应该是2

标签 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;
        }

    }

类用户属性:

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

最佳答案

@OneToMany(mappedBy = "UserMSTR") 

确保在 UserAttribute 类中存在如下引用变量:UserMSTR UserMSTR;(因为您在 UserMSTR 类中提到了 mappedBy = "UserMSTR")。

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

希望这能成功!!!

关于java - Hibernate一对多关系: the wrong number of column.应该是2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627641/

相关文章:

java - 使用 JSoup 抓取 XML

Java 读取 Maven 项目中的 XML 文件 - Openshift JBoss

java - 运行时如何知道EJB是什么样的?

mysql - 在 MYSQL 的单个列中仅存储月份和日期

java - 我应该在哪里声明不同布局的按钮?

java - 在子类中抛出错误、异常和运行时异常

java - 如何连接两个条件查询

java - Spring + Hibernate加载大量记录

java - DdlTransactionIsolatorJtaImpl 无法找到 TransactionManager

mysql - 当我更新多对多关系中的实体时,如何阻止 Hibernate/JPA 删除连接表记录?