SSH关联映射注解

86 阅读1分钟

关联映射
多对一many-to-one
建立User和Note的单表映射
在Note类中加user属性,删除原有的userId属性
在user属性前追加下面标记

//追加关联属性 Note(n) User(1)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name=”USER_ID”)
private User user;

public User getUser() {
return user;
}
一对多one-to-many
建立User和Note的单表映射
在User中加notes集合属性
在notes属性前追加下面标记

//追加属性
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name=”USER_ID”)
private List notes;

public List getNotes() {
return notes;
}

public void setNotes(List notes) {
this.notes = notes;
}
from User u join fetch u.notes where u.id=?//返回User

from User u join u.notes n where u.id=?//返回Object[][user,note]