D是关系表 A , B, C他们之间的关系 都是靠D来维护的
我用HIBERNATE 可以写出 A B C的实体BEAN和配置文件 但不知道如何来写关系表D,有没有必要去写这张表的实体BEAN和配置文件呢!
请教!
Teacher实体类代码:
package manytomany;
import java.util.HashSet;
import java.util.Set;
/**
* Teachers generated by MyEclipse - Hibernate Tools
*/
public class Teachers implements java.io.Serializable {
// Fields
private Integer teacherid;
private String teachername;
private Set students = new HashSet(0);
// Constructors
/** default constructor */
public Teachers() {
}
/** minimal constructor */
public Teachers(Integer teacherid, String teachername) {
this.teacherid = teacherid;
this.teachername = teachername;
}
/** full constructor */
public Teachers(Integer teacherid, String teachername, Set students) {
this.teacherid = teacherid;
this.teachername = teachername;
this.students = students;
}
// Property accessors
public Integer getTeacherid() {
return this.teacherid;
}
public void setTeacherid(Integer teacherid) {
this.teacherid = teacherid;
}
public String getTeachername() {
return this.teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public Set getStudents() {
return this.students;
}
public void setStudents(Set students) {
this.students = students;
}
}
student实体类代码:
package manytomany;
import java.util.HashSet;
import java.util.Set;
/**
* Students generated by MyEclipse - Hibernate Tools
*/
public class Students implements java.io.Serializable {
// Fields
private Integer studentid;
private String studentname;
private Set teachers = new HashSet(0);
// Constructors
/** default constructor */
public Students() {
}
/** minimal constructor */
public Students(Integer studentid, String studentname) {
this.studentid = studentid;
this.studentname = studentname;
}
/** full constructor */
public Students(Integer studentid, String studentname, Set teachers) {
this.studentid = studentid;
this.studentname = studentname;
this.teachers = teachers;
}
// Property accessors
public Integer getStudentid() {
return this.studentid;
}
public void setStudentid(Integer studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return this.studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public Set getTeachers() {
return this.teachers;
}
public void setTeachers(Set teachers) {
this.teachers = teachers;
}
}
DAO类代码:
package manytomany;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DAO {
public void save(Students students)
{
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(students);
tx.commit();
session.close();
}
}
session工厂类代码:
package manytomany;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
操作类代码:
package manytomany;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InfoOperate extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Students student1 = new Students();
student1.setStudentid(1);
student1.setStudentname("张三");
Students student2 = new Students();
student2.setStudentid(2);
student2.setStudentname("李四");
Teachers teacher1 = new Teachers();
teacher1.setTeacherid(1);
teacher1.setTeachername("中岛");
Teachers teacher2 = new Teachers();
teacher2.setTeacherid(2);
teacher2.setTeachername("田中");
student1.getTeachers().add(teacher1);
student1.getTeachers().add(teacher2);
student2.getTeachers().add(teacher1);
student2.getTeachers().add(teacher2);
DAO dao = new DAO();
dao.save(student1);
dao.save(student2);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}