我正在研究hibernate。我有两个实体:项目和联系人。这两个实体具有一对多的关系。但是,当我保存一个Project类时,hibernate不会在Contact表(类)中插入任何记录,只是更新Project表。
业务是;
有很多项目。用户通过双击选择项目,打开新的jsp(获得项目ID)并输入联系信息并按下“添加”按钮。我想要做的是用户选择的项目已经在db上。没问题,但我想插入联系人信息,包括project_id。我期待从hibernate更新项目表还插入联系表。但现在现实只是更新项目表。
这是我的类的结构:主控制器是Contact Controller。
package Model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="xxer_Projects_t")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int project_id;
@Column(name = "Project_number")
private String Project_number;
@Column(name = "Project_name")
private String Project_name;
@ManyToOne(fetch=FetchType.EAGER,cascade= {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH})
@JoinColumn(name = "constructor_id")
private Constructor constructor;
@OneToMany(fetch=FetchType.EAGER,mappedBy = "project",
cascade= {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH})
private List<Contact> contacts;
@OneToMany(fetch=FetchType.EAGER,mappedBy = "project",
cascade= {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH})
public List<Contact> getContacts() {
return contacts;
}
@OneToMany(fetch=FetchType.EAGER,mappedBy = "project",
cascade= {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH})
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public Project() {
// TODO Auto-generated constructor stub
}
public Project(String project_number, String project_name) {
Project_number = project_number;
Project_name = project_name;
}
public int getProject_id() {
return project_id;
}
public void setProject_id(int project_id) {
this.project_id = project_id;
}
public String getProject_number() {
return Project_number;
}
public void setProject_number(String project_number) {
Project_number = project_number;
}
public String getProject_name() {
return Project_name;
}
public void setProject_name(String project_name) {
Project_name = project_name;
}
public Constructor getConstructor() {
return constructor;
}
public void setConstructor(Constructor constructor) {
this.constructor = constructor;
}
}
package Model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="xxer_contacts_t")
public class Contact {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name_surname;
private String email;
private String phone;
@ManyToOne(fetch=FetchType.EAGER,cascade= {CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH})
@JoinColumn(name = "project_id")
private Project project;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName_surname() {
return name_surname;
}
public void setName_surname(String name_surname) {
this.name_surname = name_surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Project getProject_id() {
return project;
}
public void setProject_id(Project project_id) {
this.project = project_id;
}
public Contact() {
}
}
package Controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Dao.ConstructorDao;
import Dao.ContactDao;
import Dao.ProjectDao;
import Model.Constructor;
import Model.Contact;
import Model.Project;
/**
* Servlet implementation class ContactController
*/
@WebServlet("/ContactController")
public class ContactController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ContactController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int projectid = Integer.parseInt(request.getParameter("id"));
if (request.getParameter("Operation").equals("add")) {
String name = request.getParameter("NameSurname");
String email = request.getParameter("Email");
String phone = request.getParameter("Phone");
Project p = ProjectDao.getProjectById (projectid);
List <Contact> loc = new ArrayList<>();
Contact cont = new Contact();
cont.setName_surname(name);
cont.setEmail(email);
cont.setPhone(phone);
cont.setProject_id(p);
loc.add(cont);
p.setContacts(loc);
ProjectDao.insert(p);
/*
cont.setName_surname(name);
cont.setEmail(email);
cont.setPhone(phone);
cont.setProject_id(p);
ContactDao.Insert(cont);
*/
}
// List
List<Contact> ListOfContacts =ContactDao.getListofContacts(projectid);
request.setAttribute("ListOfContacts", ListOfContacts);
request.setAttribute("id", projectid);
RequestDispatcher dispacher = request.getRequestDispatcher("/ProjectContacts.jsp");
dispacher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package Dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import Model.Constructor;
import Model.OfferType;
import Model.Project;
import _JSP.HibernateUtil;
public class ProjectDao {
public ProjectDao() {
// TODO Auto-generated constructor stub
}
/*
public static void main(String[] args) {
int id = 1;
Project p = new Project ("Proje3","TRIA Projesi");
insert(p);
}
*/
public static void insert(Project p) {
SessionFactory sf =HibernateUtil.getSessionFactory();
//SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
try {
session.beginTransaction();
//Constructor con = session.get(Constructor.class, consid);
//p.setConstructor(con);
//session.save(p);
//session.persist(p);
session.saveOrUpdate(p);
session.getTransaction().commit();
}
finally {
session.close();
System.out.println("Project Session Closed!!!!");
}
}
public static List<Project> getListofProjects() {
//SessionFactory sf = new Configuration().configure().buildSessionFactory();
SessionFactory sf =HibernateUtil.getSessionFactory();
Session session = sf.openSession();
//List <OfferType> ol = session.createCriteria(OfferType.class).list();
List <Project> ol =session.createQuery("from Project order by 1 desc").list();
session.close();
return ol;
}
public static Project getProjectById(int id) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
//List <OfferType> ol = session.createCriteria(OfferType.class).list();
Project p =session.get(Project.class,id);
session.close();
return p;
}
public static List<Constructor> LoadConsDropDown() {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
List <Constructor> listofcons = new ArrayList<Constructor>();
listofcons = session.createQuery("from Constructor order by name asc").list();
session.close();
return listofcons;
}
}
package Dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import Model.Contact;
import Model.Project;
import _JSP.HibernateUtil;
public class ContactDao {
public ContactDao() {
// TODO Auto-generated constructor stub
}
public static void Insert(Contact c) throws SQLException {
//SessionFactory sf = new Configuration().configure().buildSessionFactory();
SessionFactory sf =HibernateUtil.getSessionFactory();
Session session = sf.openSession();
try {
session.beginTransaction();
//Constructor con = session.get(Constructor.class, consid);
//p.setConstructor(con);
session.save(c);
session.getTransaction().commit();
}
finally {
session.close();
System.out.println("Contact Session Closed!!!!");
}
}
public static List<Contact> getListofContacts(int id) {
//SessionFactory sf = new Configuration().configure().buildSessionFactory();
SessionFactory sf =HibernateUtil.getSessionFactory();
Session session = sf.openSession();
//List <OfferType> ol = session.createCriteria(OfferType.class).list();
//List <Contact> ol =session.createCriteria(Contact.class).list();
List <Contact> ol = session.createCriteria(Contact.class).createAlias("project", "p").add(Restrictions.eq("p.project_id", id)).list();
session.close();
return ol;
}
}