【问题标题】:A Foreign key refering has the wrong number of column. should be 2外键引用的列数错误。应该是 2
【发布时间】:2017-09-11 06:11:44
【问题描述】:

这是我的代码

虚拟人物

public class VirsualPerson extends Person{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@PrimaryKeyJoinColumn(name="VIRSUALPERSON_ID")
private long virsualPersonId;

@ManyToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@JoinTable(name="Anime_character",catalog="anime",joinColumns={
        @JoinColumn(name="VIRSUALPERSON_ID",nullable=false)},inverseJoinColumns={@JoinColumn(name="ANIME_ID",nullable=false)})
private Set<Anime>animeCharacters=new HashSet<Anime>();


@OneToMany(fetch=FetchType.LAZY,mappedBy="charecter")
private Set<VirsualPeopleComment>comments=new HashSet<VirsualPeopleComment>();

public long getVirsualPersonId() {
    return virsualPersonId;
}
public void setVirsualPersonId(long virsualPersonId) {
    this.virsualPersonId = virsualPersonId;
}
public Set<Anime> getAnimeCharacters() {
    return animeCharacters;
}
public void setAnimeCharacters(Set<Anime> animeCharacters) {
    this.animeCharacters = animeCharacters;
}
public Set<VirsualPeopleComment> getComments() {
    return comments;
}
public void setComments(Set<VirsualPeopleComment> comments) {
    this.comments = comments;
}

}

VisualPersonComment

@Entity
@Table(name="people_comment")
public class VirsualPeopleComment {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="peopleCommentId")
private long commentId;

@Column(name="content")
private String commentContent;

@Temporal(TemporalType.TIMESTAMP)
@Column(name="POST_TIME")
private Date postTime;


@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="USER_ID")
private User commentUser;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="VIRSUALPERSON_ID")
private VirsualPerson charecter;

public long getCommentId() {
    return commentId;
}
public void setCommentId(long commentId) {
    this.commentId = commentId;
}
public String getCommentContent() {
    return commentContent;
}
public void setCommentContent(String commentContent) {
    this.commentContent = commentContent;
}

public Date getPostTime() {
    return postTime;
}
public void setPostTime(Date postTime) {
    this.postTime = postTime;
}

public User getCommentUser() {
    return commentUser;
}
public void setCommentUser(User commentUser) {
    this.commentUser = commentUser;
}
public VirsualPerson getCharecter() {
    return charecter;
}
public void setCharecter(VirsualPerson charecter) {
    this.charecter = charecter;
}

}

这是错误

从 VirsualPeopleComment 引用 VirsualPerson 的外键列数错误。应该是 2 我想知道我的注释出了什么问题,非常感谢

【问题讨论】:

标签: java hibernate annotations


【解决方案1】:

我猜你也应该在这里指定@JoinColumn:

@OneToMany(fetch=FetchType.LAZY,mappedBy="charecter")
private Set<VirsualPeopleComment>comments=new HashSet<VirsualPeopleComment>();

应该是这样的

@OneToMany(fetch=FetchType.LAZY,mappedBy="charecter")
@JoinColumn(name="peopleCommentId")
private Set<VirsualPeopleComment>comments=new HashSet<VirsualPeopleComment>();

【讨论】:

  • 是的。要么在两个地方都使用连接列,要么从 VirsualPersonComment 类中删除 @JoinColumn(name="VIRSUALPERSON_ID") 并尝试。
  • (mappedBy 和 @JoinColumn)映射不能一起使用,因为“原因:org.hibernate.AnnotationException:标记为 mappedBy 的关联不得定义像 @JoinTable 或 @JoinColumn 这样的数据库映射”
猜你喜欢
  • 2015-01-28
  • 1970-01-01
  • 2014-07-25
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
相关资源
最近更新 更多