Hibernate Core configuration file
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hi Bernate.org/dtd/hibernate-configuration-3.0.dtd "> <hibernate-configuration> <session-factory> <!--DataSource Configuration - < propertyname= "connection.driver_class">Com.mysql.jdbc.Driver</ property> < propertyname= "connection.url">Jdbc:mysql://localhost:3306/test</ property> < propertyname= "connection.username">Root</ property> < propertyname= "connection.password"></ property> <!--specify the SQL dialect used to connect to the database - < propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ property> <!--whether the console outputs SQL statements - < propertyname= "show_sql">True</ property> <!--SQL output statements for Hibernate - < propertyname= "hibernate.format_sql">True</ property> <!--Specifies whether the program automatically creates tables in the database - < propertyname= "hbm2ddl.auto">Update</ property> <!--specify the path to the mapping file - <MappingResource= "zr/com/pojo/teamclass.hbm.xml"/> <MappingResource= "zr/com/pojo/student.hbm.xml"/> </session-factory></hibernate-configuration>
Mapping relationships for entity classes
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping package= "zr.com.pojo"> <!--Configure mapping Relationships - <classname= "teamclass"Table= "team_class"> <!--Configure the properties of the primary key and the corresponding primary key - <IDname= "classID"column= "class_id"> <Generatorclass= "native"/> </ID> <!--columns for configuration properties and relative data - < propertyname= "lfname"column= "class_name"/> <!--set cascade= "all" cascade relationship inverse= "true" Discard management foreign key - <Setname= "students"Table= "student"Cascade= "all"Inverse= "true"> <!--FOREIGN Key - <Keycolumn= "class_id"/> <!--one -to-many - <One-to-manyclass= "zr.com.pojo.Student"/> </Set> </class></hibernate-mapping>
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org /dtd/hibernate-mapping-3.0.dtd "> <hibernate-mapping> <!--Mapping Relationships - <classname= "zr.com.pojo.Student"Table= "student"> <IDname= "id"column= "stu_id"> <!--how to generate a primary key ID - <Generatorclass= "native"/> </ID> < propertyname= "name"column= "stu_name"/> <!--<property name= "classId" column= "class_id"/> - <Many-to-onename= "teamclass"class= "zr.com.pojo.TeamClass"column= "class_id"Cascade= "all"/> </class></hibernate-mapping>
package zr.com.pojo;public class Student {private int id; Private String name; Private Teamclass teamclass; public int getId () {return id; } public void SetId (int id) {this.id = id; } public String getName () {return name; } public void SetName (String Name) {this.name = name; } public teamclass Getteamclass () {return teamclass; } public void Setteamclass (teamclass Teamclass) {this.teamclass = teamclass; } public Student () {super (); } public Student (int id, String name, teamclass teamclass) {super (); This.id = id; THIS.name = name; This.teamclass = teamclass; } @Override public String toString () {return "Student [id=" + ID + ", name=" + name + ", teamclass=" + Teamclass + "]"; } }
package zr.com.pojo;import Java.util.hashset;import java.util.set;public class teamclass {private int classID; Private String lfname; Private Set<Student>Students = new HashSet<Student>(); public int GetClassID () {return classID; } public void Setclassid (int classID) {this.classid = classID; } public String getlfname () {return lfname; } public void Setlfname (String Lfname) {this.lfname = lfname; } public Set<Student>getstudents () {return students; } public void Setstudents (Set<Student>Students) {this.students = students; } public teamclass () {super (); } public teamclass (int classID, String lfname, Set<Student>Students) {super (); This.classid = classID; This.lfname = lfname; This.students = students; } @Override public String toString () {return "teamclass [classid=" + ClassID + ", lfname=" + lfname + ", students=" + students + "]"; }}
packagezr.com.test;Importjava.util.List;Importjava.util.UUID;Importorg.hibernate.Criteria;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration;Importorg.hibernate.service.ServiceRegistry;Importorg.hibernate.service.ServiceRegistryBuilder;Importzr.com.pojo.Student;Importzr.com.pojo.TeamClass; public classTestClass { public Static voidmain (string[] Args) {//creating a Configuration objectConfiguration Configuration =NewConfiguration (). Configure ("hibernate.cfg.xml"); //Create a Serviceregistry objectServiceregistry Serviceregistry =Newserviceregistrybuilder (). applysettings (configuration.getproperties ()). buildserviceregistry (); //Create a Sessionfactory objectSessionfactory sessionfactory =configuration.buildsessionfactory (serviceregistry); //Create Session ObjectSession session =sessionfactory.opensession (); //Open Transactionsession.begintransaction (); //Create an entity classStudent student1=NewStudent (); Student1.setname ("lf"); Student Student2=NewStudent (); Student2.setname ("gl"); Teamclass Teamclass=NewTeamclass (); Teamclass.setlfname ("java"); Teamclass.getstudents (). Add (student1); Teamclass.getstudents (). Add (student2); //Inserting DataSession.save (teamclass); //Commit a transactionsession.gettransaction (). Commit (); }}
Hibernate One-to-many (cascade Relationship)