Я новичок в hibernate и пытался достичь ниже, я создал три таблицы
1) Пользователь 2) Проект 3) User_project_Mapping
Теперь мы должны быть в состоянии искать все пользователи сопоставлены с проектами. Чтобы достичь этого, я создал три классифицированных объекта
1) UserEntity -> он содержит отношение OnetoMany для UserProjectMapEntity 2) UserProjectMapEnitiy -> Он содержит отношение один ко многим в ProjectEntity 3) ProjectEntity
Теперь все работает хорошо, я могу создать нового пользователя и т. Д. c.
Теперь есть возможность, что человек хочет проверить, сколько пользователей работает над конкретным проектом? Нужно ли создавать нового пользователя, сущность ProjectMap пользователя и ProjectEntities?
@Entity(name = "UserDetails")
@Table(name= "main_userdetails")
public class UserDetails {
@Id
@Column(name = "USER_ID")
int UseriD;
@Column(name = "Name")
String Name;
@Column(name = "EMAIL")
String Email;
@Column(name = "MARATIAL_STATUS")
String Maratial_Status;
@Column(name = "SPOUCE_NAME")
String Spouce_Name;
@Column(name = "DATE_OF_BIRTH")
Date Date_Of_Birth;
@Column(name = "ADDRESS1")
String Address1;
@Column(name = "ADDRESS2")
String Address2;
@Column(name = "CITY")
String City;
@Column(name = "PROVINCE")
String Province;
@Column(name = "COUNTRY")
String Country;
@Column(name = "Last_Login")
Date Last_Login;
@OneToMany(targetEntity= UserProjectMapping.class)
@JoinColumn(name = "USER_ID")
List<UserProjectMapping> userProjectMapping = new ArrayList<UserProjectMapping>();
public UserDetails(int useriD, String name, String email, String maratial_Status, String spouce_Name,
Date date_Of_Birth, String address1, String address2, String city, String province, String country,
Date last_Login, List<UserProjectMapping> userProjectMapping) {
super();
UseriD = useriD;
Name = name;
Email = email;
Maratial_Status = maratial_Status;
Spouce_Name = spouce_Name;
Date_Of_Birth = date_Of_Birth;
Address1 = address1;
Address2 = address2;
City = city;
Province = province;
Country = country;
Last_Login = last_Login;
this.userProjectMapping = userProjectMapping;
}
public List<UserProjectMapping> getUserProjectMapping() {
return userProjectMapping;
}
public void setUserProjectMapping(List<UserProjectMapping> userProjectMapping) {
this.userProjectMapping = userProjectMapping;
}
public UserDetails(int useriD, String name, String email, String maratial_Status, String spouce_Name,
Date date_Of_Birth, String address1, String address2, String city, String province, String country,
Date last_Login) {
super();
UseriD = useriD;
Name = name;
Email = email;
Maratial_Status = maratial_Status;
Spouce_Name = spouce_Name;
Date_Of_Birth = date_Of_Birth;
Address1 = address1;
Address2 = address2;
City = city;
Province = province;
Country = country;
Last_Login = last_Login;
}
public UserDetails(int useriD) {
super();
UseriD = useriD;
}
public UserDetails() {}
public int getUseriD() {
return UseriD;
}
public void setUseriD(int useriD) {
UseriD = useriD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}
public String getMaratial_Status() {
return Maratial_Status;
}
public void setMaratial_Status(String maratial_Status) {
Maratial_Status = maratial_Status;
}
public String getSpouce_Name() {
return Spouce_Name;
}
public void setSpouce_Name(String spouce_Name) {
Spouce_Name = spouce_Name;
}
public Date getDate_Of_Birth() {
return Date_Of_Birth;
}
public void setDate_Of_Birth(Date date_Of_Birth) {
Date_Of_Birth = date_Of_Birth;
}
public String getAddress1() {
return Address1;
}
public void setAddress1(String address1) {
Address1 = address1;
}
public String getAddress2() {
return Address2;
}
public void setAddress2(String address2) {
Address2 = address2;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getProvince() {
return Province;
}
public void setProvince(String province) {
Province = province;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
public Date getLast_Login() {
return Last_Login;
}
public void setLast_Login(Date last_Login) {
Last_Login = last_Login;
}
}
@Entity(name= "UserProjectMapping")
@Table(name = "userproject_mapping")
public class UserProjectMapping {
@Id
@Column(name= "ProjectID")
String ProjectId;
@OneToMany(targetEntity= ProjectDetails.class)
@JoinColumn(name = "ProjectID")
List<ProjectDetails> projectdetails = new ArrayList<ProjectDetails>();
public UserProjectMapping() {}
public String getProjectId() {
return ProjectId;
}
public void setProjectId(String projectId) {
ProjectId = projectId;
}
public List<ProjectDetails> getProjectdetails() {
return projectdetails;
}
public void setProjectdetails(List<ProjectDetails> projectdetails) {
this.projectdetails = projectdetails;
}
}
@Table(name ="project_details")
@Entity(name = "ProjectDetails")
public class ProjectDetails {
@Id
@Column(name = "ProjectID")
String ProjectID;
@Column(name = "Project_Description")
String ProjectDescription;
@Column(name = "Project_Start_Date")
Date ProjectStartDate;
@Column(name = "Project_End_Date")
Date ProjectEndDate;
@Column(name = "OffShore_Project_Manager")
String OffShoreProjectManager;
@Column(name = "Onsite_Project_Manager")
String OnsiteProjectManager;
public ProjectDetails() {}
public ProjectDetails(String projectID, String projectDescription, Date projectStartDate, Date projectEndDate,
String offShoreProjectManager, String onsiteProjectManager) {
super();
ProjectID = projectID;
ProjectDescription = projectDescription;
ProjectStartDate = projectStartDate;
ProjectEndDate = projectEndDate;
OffShoreProjectManager = offShoreProjectManager;
OnsiteProjectManager = onsiteProjectManager;
}
public String getProjectID() {
return ProjectID;
}
public void setProjectID(String projectID) {
ProjectID = projectID;
}
public String getProjectDescription() {
return ProjectDescription;
}
public void setProjectDescription(String projectDescription) {
ProjectDescription = projectDescription;
}
public Date getProjectStartDate() {
return ProjectStartDate;
}
public void setProjectStartDate(Date projectStartDate) {
ProjectStartDate = projectStartDate;
}
public Date getProjectEndDate() {
return ProjectEndDate;
}
public void setProjectEndDate(Date projectEndDate) {
ProjectEndDate = projectEndDate;
}
public String getOffShoreProjectManager() {
return OffShoreProjectManager;
}
public void setOffShoreProjectManager(String offShoreProjectManager) {
OffShoreProjectManager = offShoreProjectManager;
}
public String getOnsiteProjectManager() {
return OnsiteProjectManager;
}
public void setOnsiteProjectManager(String onsiteProjectManager) {
OnsiteProjectManager = onsiteProjectManager;
}
}
public class UserDetailsDAO {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(UserDetails.class)
.addAnnotatedClass(UserProjectMapping.class)
.addAnnotatedClass(ProjectDetails.class)
.buildSessionFactory();
public List<UserDetails> getUserDetails() {
Session session = factory.getCurrentSession();
session.beginTransaction();
List<UserDetails> lst = session.createQuery("from UserDetails").getResultList();
return lst;
}
public void SaveUser(UserDetails userdetails) {
Session session = factory.getCurrentSession();
session.beginTransaction();
session.save(userdetails);
session.getTransaction().commit();
System.out.println("The deatils have been saved");
}
}