Scene: A student, there are contacts (contact name and contact details), a student can have more than one contact, storage is divided into two tables, a student table student, a Contact table, but only one configuration file, That is, a configuration file generates two tables, which is a one-to-many relationship and can be configured using a One-to-many mapping, with two configuration files, but here's an introduction to using the collection-mode component mappings or recommending the use of the former, because the former is intuitive and easy to understand
Contact.java
Package com.fgh.hibernate;
/**
* Contact Human
* @author FGH
*
*
/public class Contacts {
//Contact Way
private String method;
Contact Content
private String name;
Public String GetMethod () {return method
;
}
public void Setmethod (String method) {
This.method = method;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
}
Student.java
Package com.fgh.hibernate;
Import Java.util.HashSet;
Import Java.util.Set;
/** *
Student class
* @author fgh
* */public
class Student {
//Because only one. hbm.xml configuration file is used, so here's the definition of Student_ Component The foreign key of the table
private String student_id;
Private String ID;
private String name;
Define a contact set for storing multiple contacts
private Set contacts = new Hashset<contact> ();
Public String GetId () {return
ID;
}
public void SetId (String id) {
this.id = ID;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String getstudent_id () {return
student_id;
}
public void setstudent_id (String student_id) {
this.student_id = student_id;
}
Public Set getcontacts () {return
contacts;
}
public void Setcontacts (Set contacts) {
this.contacts = contacts;
}
}
Student.hbm.xml
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping public
"-//hibernate/hibernate mapping DTD 3.0//en"
"http:// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
To build a table class:
Createtable.java
Package com.fgh.hibernate;
Import org.hibernate.cfg.Configuration;
Import Org.hibernate.tool.hbm2ddl.SchemaExport;
public class CreateTable {public
static void Main (string[] args) {
Schemaexport export = new Schemaexport (New Con Figuration (). Configure ());
Export.create (True, true);
}
Test class: Hibernatetest.java
Package com.fgh.hibernate;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration; /** * Assembly mapping is somewhat similar to a single table mapping just to get a list of the fields in a table. * * @author FGH * */public class Hibernatetest {private St
Atic sessionfactory sessionfactory;
static {try {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();
catch (Exception e) {e.printstacktrace ();
} public static void Main (string[] args) {Session session = Sessionfactory.opensession ();
Transaction tx = NULL;
try {tx = Session.begintransaction ();
Student Student = new Student ();
Student.setname ("Zhangsan");
Contact contacts = new Contact ();
Contact.setmethod ("Telephone");
Contact.setname ("1231241234");
Contact Contact2 = new Contact ();
Contact2.setmethod ("Address");
Contact2.setname ("Beijing");
Student.getcontacts (). Add (Contact); Student.getcontacts (). Add (contACT2);
Session.save (student);
Tx.commit ();
catch (Exception e) {e.printstacktrace ();
if (null!= tx) {tx.rollback ();
finally {session.close ();
}
}
}Over