Hibernate Learning Series-----(9) Hibernate map Collection of the operation of set properties

Source: Internet
Author: User
Tags relational database table

As usual, create a new Studentmap.java entity class and store the hobby attribute using the Map collection interface:

 Packagecom.joe.entity;ImportJava.util.Map; Public classStudentmap {Private intID; PrivateString name; Private intAge ; PrivateMap<string,string>Hobby; /*** No parameter constructor*/     PublicStudentmap () {} Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicMap<string, string>Gethobby () {returnHobby; }     Public voidSethobby (map<string, string>Hobby) {         This. Hobby =Hobby; }            }

The next step is to configure the object-relational mapping file, which can be configured in hibernate using the <map> tag, and first look at the common properties and sub-elements of the <map> tag bar:<map> Elements are used to map properties of the Java.util.Map type, and commonly used properties and child elements are:

    1. Name property
    2. Table Property
    3. <key> child elements
    4. <map-key> child elements
    5. <element> child elements

In Java, the Map collection interface holds data in the form of a key-value pair, so <map-key> is the key to the map, so it also requires additional columns to hold the key values. The content in StudentMap.hbm.xml is:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-mapping SYSTEM "Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping>    <!--a class tag corresponds to an entity class, the name attribute specifies the entity class name, and the table property specifies the associated database table -    <classname= "Com.joe.entity.StudentMap"Table= "Stu_map_tab">        <!--PRIMARY Key -        <IDname= "id"column= "stu_id">            <!--the policy of providing ID increment native will be judged by the database itself -            <Generatorclass= "Native"></Generator>        </ID>        <!--other properties, name corresponding to the attribute of the entity class, column corresponding to the relational database table -        < Propertyname= "Name"column= "Stu_name"></ Property>        < Propertyname= "Age"column= "Stu_age"></ Property>                <Mapname= "Hobby"Table= "Hobby_map_tab">            <Keycolumn= "student_id"></Key>            <Map-keycolumn= "KeyColumn"type= "string"></Map-key>            <elementtype= "string"column= "Hobby"></element>        </Map>            </class></hibernate-mapping>

Finally paste the code of the Studentmaptest.java class, this test and the previous several articles in the same test, it does not show one by one:

 Packagecom.joe.test;ImportJava.util.HashMap;ImportJava.util.Map;Importorg.hibernate.HibernateException;Importorg.hibernate.Session;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.tool.hbm2ddl.SchemaExport;Importorg.junit.Test;ImportCom.joe.entity.StudentMap;Importcom.joe.util.HibernateUtils; Public classstudentmaptest {@Test Public voidcreatetable () {Configuration cfg=NewConfiguration (). Configure (); Schemaexport SE=NewSchemaexport (CFG); Se.create (true,true); } @Test Public voidSave () {Transaction tx=NULL; Session Session=NULL; Try{Session=hibernateutils.getsession (); TX=session.begintransaction (); Studentmap Stu=NewStudentmap (); Stu.setname ("Wangwu"); Stu.setage (20); @SuppressWarnings ({"Unchecked", "Rawtypes"}) Map<String,String> map=NewHashMap (); Map.put ("A", "Run"); Map.put ("B", "Eat");                        Stu.sethobby (map);                                    Session.save (Stu);        Tx.commit (); }Catch(Hibernateexception He) {if(tx!=NULL) {tx.rollback ();        } he.printstacktrace (); }finally{hibernateutils.closesession (session); }} @Test Public voidFindAll () {Transaction tx=NULL; Session Session=NULL; Try{Session=hibernateutils.getsession (); TX=session.begintransaction (); Studentmap Stu= (Studentmap) session.get (Studentmap.class, 1); System.out.println (Stu.getid ()+stu.getname () +stu.getage ()); Map<string,string > map=Stu.gethobby ();            SYSTEM.OUT.PRINTLN (map);        Tx.commit (); }Catch(Hibernateexception He) {if(tx!=NULL) {tx.rollback ();        } he.printstacktrace (); }finally{hibernateutils.closesession (session); }    }}
Hibernate on the operation of the collection is written here, in fact, there are many did not write, if there is a great God read my article, hope that a lot of guidance, finally, about the Java collection of learning, I saw in the garden has a garden friends wrote a series, feel write very good, suggest you look, Java collection series Directory (Category)

Hibernate Learning Series-----(9) Hibernate map Collection of the operation of set properties

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.