当采用Annotation来管理实体类之后得到的如下的公式:
PO=POJO+@Annotation
定义一个实体类可以这样的用:
@Entity
@Table(name="person_table")
public class Person
{
/* 指定使用复合主键类是Name */
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="first"
, column=@Column(name="person_first")),
@AttributeOverride(name="last"
, column=@Column(name="person_last" , length=20))
})
private Name name;
//普通属性
@Column(name="person_email")
private String email;
@Embedded
@AttributeOverrides({
@AttributeOverride(name="name"
, column=@Column(name="cat_name" , length=35)),
@AttributeOverride(name="color"
, column=@Column(name="cat_color"))
})
//组件属性,代表此人拥有的宠物
private Cat pet;
//name属性的setter和getter方法
public void setName(Name name)
{
this.name = name;
}
public Name getName()
{
return this.name;
}
//email属性的setter和getter方法
public void setEmail(String email)
{
this.email = email;
}
public String getEmail()
{
return this.email;
}
//pet属性的setter和getter方法
public void setPet(Cat pet)
{
this.pet = pet;
}
public Cat getPet()
{
return this.pet;
}
}
//修饰组件属性类
@Embeddable
public class Name
implements java.io.Serializable
{
private String first;
private String last;
//无参数的构造器
public Name()
{
}
//初始化全部属性的构造器
public Name(String first , String last)
{
this.first = first;
this.last = last;
}
//first属性的setter和getter方法
public void setFirst(String first)
{
this.first = first;
}
public String getFirst()
{
return this.first;
}
//last属性的setter和getter方法
public void setLast(String last)
{
this.last = last;
}
public String getLast()
{
return this.last;
}
//提供重写的equals方法
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj.getClass() == Name.class)
{
Name target = (Name)obj;
if (target.getFirst().equals(first)
&& target.getLast().equals(last))
{
return true;
}
}
return false;
}
//提供重写的hashCode方法
public int hashCode()
{
return first.hashCode() + last.hashCode() * 17;
}
}
//修饰组件属性类
@Embeddable
Hibernate的映射文件