package model;
import java.util.HashSet;
import java.util.Set;
public class Student {
private String id;
private String name;
private Set contacts = new HashSet();
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 Set getContacts() {
return contacts;
}
public void setContacts(Set contacts) {
this.contacts = contacts;
}
}
package model;
public class Contact {
private String student_id;
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String studentId) {
student_id = studentId;
}
private String method;
private String address;
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "model.Student" table = "student" lazy="true">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>
<property name="name" column = "name" type = "string"></property>
<set name="contacts" table = "contact">
<key column = "student_id"></key>
<composite-element class="model.Contact">
<property name="method" type = "string"></property>
<property name="address" type = "string"></property>
</composite-element>
</set>
</class>
</hibernate-mapping>
package model;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
//create table contact (student_id varchar(255) not null, method varchar(255), address varchar(255))
//create table student (id varchar(255) not null, name varchar(255), primary key (id))
//alter table contactddd add index FK8566BC45B226DF5 (student_id), add constraint FK8566BC45B226DF5 foreign key (student_id) references student (id)
public class Create {
public static void main(String[] args) {
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true, true);
}
}
package model;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
public static void main(String[] args) {
save();
}
/**
* Hibernate: insert into student (name, id) values (?, ?)
Hibernate: insert into contact (student_id, method, address) values (?, ?, ?)
Hibernate: insert into contact (student_id, method, address) values (?, ?, ?)
*
*/
static void save () {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = new Student ();
student.setName("zhangsan");
Contact contact1 = new Contact ();
contact1.setMethod("telephone");
contact1.setAddress("123456");
Contact contact2 = new Contact ();
contact2.setMethod("address");
contact2.setAddress("shanghai");
student.getContacts().add(contact1);
student.getContacts().add(contact2);
session.save(student);
tx.commit();
}catch (Exception ex) {
ex.printStackTrace();
if (null != tx) {
tx.rollback();
}
}finally {
HibernateUtil.close(session);
}
}
}