一对多、多对一、一对一的注解实现
casecade={CasecadeType.REFRESH}的含义是只是查询级联,它还有其他3个类型:
CascadeType.MERGE级联更新
CascadeType.PERSIST级联保存
CascadeType.REMOVE级联删除
optional=false定义关联类不一定要存在
@JoinColumn 关联的对象的字段
@ManyToOne多对一 @OneToMany一对多 @OneToOne一对一
需要注意的是一对一关联关系,hibernate会建立唯一约束,用错的话,后果很严重奥。
event对象
@Entity
@Table(name = "event")
public class EventEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "eventId", nullable = true)
private long eventId; //事件id
private String eventNote; //事件内容
private String eventTitle; //事件标题
private String createTime; //创建时间
@ManyToOne(cascade = {CascadeType.REFRESH}, optional = false)
@JoinColumn(name="userId")
private UserEntity user;
@OneToOne(optional = false, cascade = CascadeType.REFRESH)
@JoinColumn(name = "eventTitleImage")
private EventImageEntity eventTitleImage; //事件封面照片
private String eventAddress; //事件地址
private String eventType; //事件类型
private String lon; //经度
private String lat; //纬度
private String eventstate; //事件状态
@OneToMany(cascade = {CascadeType.REFRESH})
@JoinColumn(name = "eventId")
private List eventImageList;
public long getEventId() {
return eventId;
}
public void setEventId(long eventId) {
this.eventId = eventId;
}
public String getEventNote() {
return eventNote;
}
public void setEventNote(String eventNote) {
this.eventNote = eventNote;
}
public String getEventTitle() {
return eventTitle;
}
public void setEventTitle(String eventTitle) {
this.eventTitle = eventTitle;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
public EventImageEntity getEventTitleImage() {
return eventTitleImage;
}
public void setEventTitleImage(EventImageEntity eventTitleImage) {
this.eventTitleImage = eventTitleImage;
}
public String getEventAddress() {
return eventAddress;
}
public void setEventAddress(String eventAddress) {
this.eventAddress = eventAddress;
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getEventstate() {
return eventstate;
}
public void setEventstate(String eventstate) {
this.eventstate = eventstate;
}
public List getEventImageList() {
return eventImageList;
}
public void setEventImageList(List eventImageList) {
this.eventImageList = eventImageList;
}
}
user对象
@Entity
@Table(name = "user")
public class UserEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userId")
private long userId;
private String username; //用户名称
private String phone; //用户电话
private String password; //密码
@OneToOne(optional = false, cascade = CascadeType.REFRESH)
@JoinColumn(name = "userimage")
private ImageEntity userImage; //用户头像
private String isRole; //是否有权限
private String createTime; //创建时间
private String sign; //签名
private String sex; //性别
private String age; //年纪
private String birthday; //生日
private String numberId; //用户ID标示
private String emotional; //情感状态
private String updateTime; //修改时间
private String userstate; //用户状态(1正常 2禁用 3删除)
@ManyToOne(cascade={CascadeType.REFRESH},optional = false)
@JoinColumn(name="userLevel")
private Exp exp; //等级
private long exping; //当前经验值
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ImageEntity getUserImage() {
return userImage;
}
public void setUserImage(ImageEntity userImage) {
this.userImage = userImage;
}
public String getIsRole() {
return isRole;
}
public void setIsRole(String isRole) {
this.isRole = isRole;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getNumberId() {
return numberId;
}
public void setNumberId(String numberId) {
this.numberId = numberId;
}
public String getEmotional() {
return emotional;
}
public void setEmotional(String emotional) {
this.emotional = emotional;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public String getUserstate() {
return userstate;
}
public void setUserstate(String userstate) {
this.userstate = userstate;
}
public Exp getExp() {
return exp;
}
public void setExp(Exp exp) {
this.exp = exp;
}
public long getExping() {
return exping;
}
public void setExping(long exping) {
this.exping = exping;
}
}
image对象
@Entity
@Table(name = "image")
public class ImageEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "imageId")
private long imageId;
private String url;
public long getImageId() {
return imageId;
}
public void setImageId(long imageId) {
this.imageId = imageId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
exp对象
@Entity
@Table(name = "exp")
public class Exp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "expId")
private long expId;
private long grade; //等级
private long expnumber; //经验值
private String expname; //显示名称
public long getExtId() {
return expId;
}
public void setExtId(long expId) {
this.expId = expId;
}
public long getGrade() {
return grade;
}
public void setGrade(long grade) {
this.grade = grade;
}
public long getExpnumber() {
return expnumber;
}
public void setExpnumber(long expnumber) {
this.expnumber = expnumber;
}
public String getExpname() {
return expname;
}
public void setExpname(String expname) {
this.expname = expname;
}
}
eventImage对象
@Entity
@Table(name = "eventimage")
public class EventImageEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "eventImageId")
private long eventImageId;
private long eventId;
@OneToOne(optional = false, cascade = CascadeType.REFRESH)
@JoinColumn(name = "imageId")
private ImageEntity image;
public long getEventImageId() {
return eventImageId;
}
public void setEventImageId(long eventImageId) {
this.eventImageId = eventImageId;
}
public long getEventId() {
return eventId;
}
public void setEventId(long eventId) {
this.eventId = eventId;
}
public ImageEntity getImage() {
return image;
}
public void setImage(ImageEntity image) {
this.image = image;
}
}