2017-04-26 32 views
1

類UserMSTR:休眠一對多關係:列數錯誤。應該是2

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

    } 

類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 。com.java.spring.data.domain.UserAttribute中的UserMSTR列數錯誤。應該是2

回答

0
@OneToMany(mappedBy = "UserMSTR") 

確保有這樣的參考變量:UserMSTR UserMSTR;UserAttribute類(因爲你在UserMSTR類提mappedBy = "UserMSTR")。

可能是您的參考變量名稱與您在mappedBy中使用的內容不同。

希望這會工作!

+0

是的,我已經改變它,但它仍然給「外部鍵引用com.java.spring.data.domain.UserMSTR從com.java.spring.data.domain.UserAttribute有錯誤的列數。應該是2「異常 –

+0

您是否在'UserAttribute'類中使用了@ @ joincolumn和@ ManyToOne'註解?告訴我這個類的代碼 –

+0

把這個'@OneToMany(mappedBy =「UserMSTR」)'改成'@OneToMany(mappedBy =「userMSTR」)' –