// 通知持久化引擎该类是一个实体 @Entity @Table(name = "employee", schema="springdao") public class Employee implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = -581308461934077076L; // 注解了持久化标识,即唯一的标识键 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private Long salary; private String phoneNum; @Basic(fetch = FetchType.LAZY) @Lob @Column(name = "picture") // 大型对象的延迟加载 private Byte[] picture; @Enumerated(EnumType.STRING) // 默认的是插入序号 用插入值可以避免增加新的枚举类型序号的变化 private EmployeeType type; @Temporal(TemporalType.DATE) //java.util 日期类型需要指明隐射时JDBC处理的方式 private Calendar dob; @Temporal(TemporalType.TIMESTAMP) private Date startDate; @Transient private String combineArrt; // 忽略,不将其映射到数据库 // 当定义双向一对多(或多对一)关系时: // 多对一一方是所有方,在该方定义连接列 // 一对多是反方,所以必须使用 mappedBy 元素,否则会生成一个中间联结表 @ManyToOne @JoinColumn(name = "dept_id") // JoinColumn 来实现外键关联,默认的话为 private Department department; // 源实体中的关系特性的名称_目标实体的主键名 // 按照惯例逻辑映射先出现,因为物理映射有默认值 @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "pspace_id") private ParkingSpace parkingSpace; // 多对多关系的实现是中间表(join table),但是必须要有一方是mappedBy @ManyToMany @JoinTable(name = "emp_proj", joinColumns = @JoinColumn(name = "emp_id"), inverseJoinColumns = @JoinColumn(name = "proj_id") ) private Collection<Project> projects; // 单项一对多关系,需要中间表 @OneToMany @JoinTable(name = "emp_phone", joinColumns = @JoinColumn(name = "emp_id"), inverseJoinColumns = @JoinColumn(name = "phone_id") ) private Collection<Phone> phones; public Employee() {} public Employee(Integer id) { this.id = id; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getSalary() { return salary; } public void setSalary(Long salary) { this.salary = salary; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public Byte[] getPicture() { return picture; } public void setPicture(Byte[] picture) { this.picture = picture; } public EmployeeType getType() { return type; } public void setType(EmployeeType type) { this.type = type; } public String getCombineArrt() { return this.id + "; " + this.name; } public void setCombineArrt(String combineArrt) { this.combineArrt = combineArrt; } public Calendar getDob() { return dob; } public void setDob(Calendar dob) { this.dob = dob; } public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public ParkingSpace getParkingSpace() { return parkingSpace; } public void setParkingSpace(ParkingSpace parkingSpace) { this.parkingSpace = parkingSpace; } public Collection<Project> getProjects() { return projects; } public void setProjects(Collection<Project> projects) { this.projects = projects; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", phoneNum=" + phoneNum + ", picture=" + Arrays.toString(picture) + ", type=" + type + ", dob=" + dob + ", startDate=" + startDate + ", combineArrt=" + combineArrt + "]"; } }
@Entity @Table(name = "addresstwo") public class AddressTwo implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 4003827955642961159L; @TableGenerator(name = "Address_Gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen") private int id; private String name; public AddressTwo() {} public AddressTwo(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
@Entity @Table(name = "department") public class Department implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy = "department") private Collection<Employee> employees; public Department() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Collection<Employee> getEmployees() { return employees; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", employees=" + employees + "]"; } }
@Entity @Table(name = "park_space") public class ParkingSpace implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 7356683100796513602L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private int lot; private String location; // 双向onetoone mappedBy 还是表明 Employee 是源实体,放在没有定义联结列的实体上 @OneToOne(mappedBy = "parkingSpace") private Employee employee; public ParkingSpace() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public int getLot() { return lot; } public void setLot(int lot) { this.lot = lot; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
@Entity @Table(name = "project") public class Project implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 7315080326584390902L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String proName; @ManyToMany(mappedBy = "projects") private Collection<Employee> employees; public Project() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProName() { return proName; } public void setProName(String proName) { this.proName = proName; } public Collection<Employee> getEmployees() { return employees; } public void setEmployees(Collection<Employee> employees) { this.employees = employees; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
@Entity @Table(name = "phone") public class Phone implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 682045004499830046L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private int type; private String num; public Phone() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } @Override public String toString() { return "Phone [id=" + id + ", type=" + type + ", num=" + num + "]"; } }
@Entity @Table(name = "head") public class Head implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 1856809533670124595L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToOne(optional = false, cascade = CascadeType.ALL) @JoinColumn(name = "nose_id") private Nose nose; public Head() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Nose getNose() { return nose; } public void setNose(Nose nose) { this.nose = nose; } @Override public String toString() { return "Head [id=" + id + ", name=" + name + "]"; } }
@Entity @Table(name = "nose") public class Nose implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 8615516251260117582L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @OneToOne(mappedBy = "nose") private Head head; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Head getHead() { return head; } public void setHead(Head head) { this.head = head; } @Override public String toString() { return "Nose [id=" + id + ", name=" + name + "]"; } }
/** * ClassName:Animal <br/> * Function: 嵌入对象 embedded object. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2014年7月4日 下午1:58:45 <br/> * @author zhangzhaoyu * @version * @since JDK 1.7 * @see */ @Entity @Table(name = "animal") public class Animal implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = 3295314624812247524L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; private String age; @Embedded private LiveAddress liveAddress; public Animal() {} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Animal [id=" + id + ", name=" + name + ", age=" + age + ", liveAddress=" + liveAddress + "]"; } }
/** * ClassName:Bird <br/> * Function: 重命名嵌入字段的值. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2014年7月4日 下午2:12:46 <br/> * @author zhangzhaoyu * @version * @since JDK 1.7 * @see */ @Entity @Table(name = "exercise_bird") public class Bird implements Serializable { /** * serialVersionUID:TODO * @since JDK 1.7 */ private static final long serialVersionUID = -317303554096481889L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; @Embedded @AttributeOverrides({ @AttributeOverride(name = "state", column = @Column(name = "bird_state")), @AttributeOverride(name = "zip", column = @Column(name = "bird_zip")) }) private LiveAddress liveAddress; public Bird() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public LiveAddress getLiveAddress() { return liveAddress; } public void setLiveAddress(LiveAddress liveAddress) { this.liveAddress = liveAddress; } @Override public String toString() { return "Bird [id=" + id + ", name=" + name + ", liveAddress=" + liveAddress + "]"; } }
/** * ClassName:LiveAddress <br/> * Function: 嵌入对象 embedded object. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2014年7月4日 下午2:01:44 <br/> * @author zhangzhaoyu * @version * @since JDK 1.7 * @see */ @Embeddable @Access(AccessType.FIELD) public class LiveAddress implements Serializable { private String street; private String city; private String state; @Column(name = "zip_code") private String zip; }