Hibernate base------>hibernate Collection Mapping--->set mapping

Source: Internet
Author: User
Tags set set
Set Set mapping
Set set mapping is simple, I feel a bit like a one-to-many relationship (only a single PO's a-to-many relationship), because it can be a table and two hibernate necessary configuration, a run class on it.


The code is as follows (for example, people address)

First write the JavaBean model class (person)
Package one;

Import Java.util.Set;

public class Person {
private int id;
private String name;
Private set<string> address; Here are the collection properties to be mapped by hibernnate, and there are about seven types of Hibernate collection properties: 1,list 2,set 3,map 4,array (for Mapping Array collection properties) 5, Primitive-array (used to map an array of base data types) 6,bag (used to map unordered collections) 7,idbag (used to map unordered collections, but to enhance logical order for collections)

Get,set method omitted
}
Write the configuration file again
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >

<class name= "person" table= "person" >

<id name= "id" column= "id" >
<generator class= "native"/>
</id>
<property name= "name" column= "name" ></property>
<set name= "Address" >
<key column= "address_id" ></key> key corresponding to the primary key in the database
<element column= "Addressname" type= "string" ></element> element here corresponds to a field in the database
</set>
</class>



Finally finish running the class can be
Package one;

Import Java.util.HashSet;
Import Java.util.Set;

Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import org.hibernate.classic.Session;
Import Org.junit.Test;

public class App {
Static sessionfactory SF;
static{
Sf=new Configuration (). Configure (). Buildsessionfactory ();
}
@Test
public void Test_add_onetomany () {
Session session = Sf.opensession ();
Transaction tx = Session.begintransaction ();
Person p = new person ();
P.setname ("Mr. Lin");
set<string> address = new hashset<string> ();
Address.add ("Dongguan");
Address.add ("Humen");
P.setaddress (address);
Session.save (P);
Tx.commit ();
Session.close ();
}
}

Show effect

Hibernate collection Mapping--->set mapping ">


Show foreign key relationships

Hibernate collection Mapping--->set mapping ">



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.