hibernate基于注解的组件映射
程序员文章站
2022-05-13 20:02:15
...
基于注解的组件映射
在此以person和name为例:
Name实体类
public class Name {
private String firstName;
private String lastName;
//...省略构造方法和getter,setter方法
}
Person实体类
@Entity//声明该类要在表中映射
public class Person {
private int id;
private Name name;
private Date birthday;
//无参构造
@Id // 声明该属性是主键字段
@GeneratedValue //声明主键生成策略是自增长
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/*
@Embedded:声明该属性组件映射,如果不需要更改name属性在表中字段的默认值,只写该注解即可,如果需要修改name属性在表中字段的默认值,则还需以下配置;
column = @Column(name = "first_name", length = 10):将组件相应的属性修改为什么值;
name = "firstName":对组件的那个属性进行修改
*/
@Embedded
@AttributeOverrides(value = {
@AttributeOverride(column = @Column(name = "first_name", length = 10), name = "firstName"),
@AttributeOverride(column = @Column(name = "last_name", length = 10), name = "lastName") })
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
上一篇: 最美的情诗推荐
下一篇: 清平乐四首——与文友共赏