hibernate中一对多的方式二

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);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值