2. One-to-one
3. One-to-many
4. Many-to-many
Many-to-One Mappings
@Entity
public class Employee {
// ...
@ManyToOne
private Department department;
// ...
}
@Entity
public class Employee {
@Id private int id;
@ManyToOne
@JoinColumn(name="DEPT_ID")
private Department department;
// ...
}
One-to-One Mappings
@Entity
public class Employee {
@Id private int id;
private String name;
@OneToOne
@JoinColumn(name="PSPACE_ID")
private ParkingSpace parkingSpace;
// ...
}
@Entity
public class ParkingSpace {
@Id private int id;
private int lot;
private String location;
@OneToOne(mappedBy="parkingSpace")
private Employee employee;
// ...
}
One-to-Many Mappings
@Entity
public class Department {
@Id private int id;
private String name;
@OneToMany(mappedBy="department")
private Collection
// ...
}
@Entity
public class Department {
@Id private int id;
private String name;
@OneToMany(targetEntity=Employee.class, mappedBy="department")
private Collection employees;
// ...
}
Many-to-Many Mappings
@Entity
public class Employee {
@Id private int id;
private String name;
@ManyToMany
private Collection
// ...
}
@Entity
public class Project {
@Id private int id;
private String name;
@ManyToMany(mappedBy="projects")
private Collection
// ...
}
No comments:
Post a Comment