Collection-Mode component mappings

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.