Myeclipse反向工程
单向一对多案例:
hibernate配置文件,
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> <![CDATA[ jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=utf8 ]]> </property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="com/hpu/entity/Grade.hbm.xml"/> <mapping resource="com/hpu/entity/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.hpu.entity; import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; import java.util.Set; public class Grade implements Serializable{ private int gid; private String gname; private String gdesc; private Set<Student> students = new HashSet<Student>(); public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public String getGdesc() { return gdesc; } public void setGdesc(String gdesc) { this.gdesc = gdesc; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public Grade(int gid, String gname, String gdesc) { super(); this.gid = gid; this.gname = gname; this.gdesc = gdesc; } public Grade(String gname, String gdesc) { super(); this.gname = gname; this.gdesc = gdesc; } @Override public String toString() { return "Grade [gid=" + gid + ", gname=" + gname + ", gdesc=" + gdesc + ", students=" + students + "]"; } }实体类2:
package com.hpu.entity; import java.io.Serializable; public class Student implements Serializable{ private int gid; private int sid; private String sname; private String sex; private Grade grade; public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public Student(){ super(); } public Student(String sname, String sex) { super(); this.sname = sname; this.sex = sex; } }
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hpu.entity.Grade" table="grade"> <id name="gid" column="gid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="gname" type="java.lang.String"> <column name="gname" length="20" not-null="true"></column> </property> <property name="gdesc"> <column name="gdesc"></column> </property> <set name="students" table="student"> <key column="gid"></key> <one-to-many class="com.hpu.entity.Student" /> </set> </class> </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hpu.entity.Student" table="student"> <id name="sid" column="sid" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="sname" type="java.lang.String"> <column name="sname" length="20" not-null="true"></column> </property> <property name="sex"><column name="sex"></column> </property> <many-to-one name="grade" class="com.hpu.entity.Grade" column="gid" cascade="save-update,persist"></many-to-one> </class> </hibernate-mapping>
package com.hpu.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; private static Session session; static { Configuration config = new Configuration().configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()); StandardServiceRegistry ssr = ssrb.build(); sessionFactory = config.buildSessionFactory(ssr); } // 获取SessionFactory public static SessionFactory getSessionFactory() { return sessionFactory; } // 获取Session public static Session getSession() { session = sessionFactory.openSession(); return session; } // 关闭Session public static void closeSession(Session session) { if (session != null) { session.close(); } } }
package com.hpu.test; import org.hibernate.Session; import org.hibernate.Transaction; import com.hpu.entity.Grade; import com.hpu.entity.Student; import com.hpu.util.HibernateUtil; public class Test { public static void main(String[] args) { add(); // findStudentsByGrade(); // update(); // delete(); } public static void add(){ Grade g = new Grade("软件1302","软件1301"); Student stu1 = new Student("黄大于","男"); Student stu2 = new Student("小明","女"); Student stu3 = new Student("小红", "女"); g.getStudents().add(stu1); g.getStudents().add(stu2); g.getStudents().add(stu3); Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); session.save(g); session.save(stu1); session.save(stu2); session.save(stu3); tx.commit(); HibernateUtil.closeSession(session); } public static void findStudentsByGrade(){ Session session = HibernateUtil.getSession(); Grade grade = (Grade) session.get(Grade.class, 1); System.out.println(grade.toString()); System.out.println(grade.getGname() + "," + grade.getGdesc()); } public static void update(){ Grade g = new Grade("网络1301","网络1304"); Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Student stu = (Student) session.get(Student.class, 1); session.save(g); tx.commit(); HibernateUtil.closeSession(session); } public static void delete(){ Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); Student stu = (Student) session.get(Student.class, 2); session.delete(stu); tx.commit(); HibernateUtil.closeSession(session); } }