The Key of the Hibernate persistence class (Map) set attribute is the ing of components.

Source: Internet
Author: User
Tags generator


1. hibernate. cfg. xml

The code is as follows: Copy code

<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd>

<Hibernate-configuration>
<Session-factory>
<! -- Configure database dialect -->
<Property name = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </property>
<! -- Configure the database driver -->
<Property name = "hibernate. connection. driver_class"> com. mysql. jdbc. Driver </property>
<! -- Configure the database username -->
<Property name = "hibernate. connection. username"> root </property>
<! -- Configure the database password -->
<Property name = "hibernate. connection. password"> root </property>
<! -- Configure the database url -->
<Property name = "hibernate. connection. url"> jdbc: mysql: // localhost: 3306/hibernate </property>
<! -- Configure the maximum capacity of the data pool -->
<Property name = "hibernate. c3p0. max_size"> 20 </property>
<! -- Configure the minimum capacity of the data pool -->
<Property name = "hibernate. c3p0. min_size"> 1 </property>
<! -- Configure the timeout threshold for data links -->
<Property name = "hibernate. c3p0. timeout"> 5000 </property>
<! -- Display whether the executed SQL is printed in the background on the console -->
<Property name = "hibernate. show_ SQL"> true </property>
<! -- Whether to display printed SQL statements in a friendly format -->
<Property name = "hibernate. format_ SQL"> true </property>
<! -- Print some auxiliary comments -->
<Property name = "hibernate. use_ SQL _comments"> true </property>

<Property name = "hibernate. c3p0. max_statements"> 100 </property>
<Property name = "hibernate. c3p0. idle_test_period"> 3000 </property>
<Property name = "hibernate. c3p0. acquire_increment"> 2 </property>
<Property name = "hibernate. c3p0. validate"> true </property>
<! -- Configure the data operation method -->
<Property name = "hbm2ddl. auto"> create </property>
<! -- Add the above Member ing file -->
<Mapping resource = "org/Rudiment/hibernate/Member. hbm. xml"/>
</Session-factory>
</Hibernate-configuration>


2. Persistence class Member. java

The code is as follows: Copy code

Package org. Rudiment. hibernate;

Import java. util. HashMap;
Import java. util. Map;

Public class Member {
Private Integer id;
Private String name;
Private Integer age;
Private Map <Address, String> addresses = new HashMap <Address, String> ();
 
Public Map <Address, String> getAddresses (){
Return addresses;
    }
Public void setAddresses (Map <Address, String> addresses ){
This. addresses = addresses;
    }
Public Integer getId (){
Return id;
    }
Public void setId (Integer id ){
This. id = id;
    }
Public String getName (){
Return name;
    }
Public void setName (String name ){
This. name = name;
    }
Public Integer getAge (){
Return age;
    }
Public void setAge (Integer age ){
This. age = age;
    }
   
}

3. Persistence class component class Address. java

The code is as follows: Copy code

Package org. Rudiment. hibernate;

Import java. util. HashMap;
Import java. util. Map;

Public class Address {

Private String street;
Private String zipCode;
Private String city;

Private Member owner;
   
Public Member getOwner (){
Return owner;
    }
Public void setOwner (Member owner ){
This. owner = owner;
    }
Public String getStreet (){
Return street;
    }
Public void setStreet (String street ){
This. street = street;
    }
Public String getZipCode (){
Return zipCode;
    }
Public void setZipCode (String zipCode ){
This.zip Code = zipCode;
    }
Public String getCity (){
Return city;
    }
Public void setCity (String city ){
This. city = city;
    }
}

4. The ing file Member. cfg. xml of the persistence class

The code is as follows: Copy code

<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! -- Generated 2013-9-9 19:50:34 by Hibernate Tools 3.4.0.CR1 -->
<Hibernate-mapping package = "org. Rudiment. hibernate">
<Class name = "Member" table = "TEST_MEMBER">
<Id name = "id" column = "ID">
<Generator class = "native"> </generator>
</Id>
<Property name = "age" column = "AGE"> </property>
<Property name = "name" column = "NAME"> </property>
<Map name = "addresses" table = "test_component">
<Key column = "component_id" not-null = "true"/>
<Composite-map-key class = "Address">
<Key-property name = "street" column = "home_street" type = "string"/>
<Key-property name = "zipCode" column = "home_zip_code" type = "string"/>
<Key-property name = "city" column = "home_city" type = "string"/>
</Composite-map-key>
<Element column = "address_name" type = "string"/>
</Map>
</Class>
</Hibernate-mapping>

5. MemberHandler. java

The code is as follows: Copy code

Package org. Rudiment. hibernate;

Import java. util. HashMap;
Import java. util. Map;

Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. cfg. Configuration;
Import org. hibernate. service. ServiceRegistry;
Import org. hibernate. service. ServiceRegistryBuilder;

Public class MemberHandler {

Public static void insert (){
Configuration conf = new Configuration ();
Conf. configure ();
ServiceRegistry sr = new ServiceRegistryBuilder (). applySettings (conf. getProperties (). buildServiceRegistry ();
SessionFactory sf = conf. buildSessionFactory (sr );
Session session = sf. openSession ();
Session. beginTransaction ();
Member m = new Member ();
M. setAge (24 );
M. setName ("Louis ");
           
Address ad1 = new Address ();
Ad1.setCity ("Guangzhou ");
Ad1.setOwner (m );
Ad1.setStreet ("No. 1 Street ");
Ad1.setZipCode ("123 ");

Address ad2 = new Address ();
Ad2.setCity ("Shenzhen ");
Ad2.setOwner (m );
Ad2.setStreet ("Street 2 ");
Ad2.setZipCode ("456 ");

Map <Address, String> am = new HashMap <Address, String> ();
Am. put (ad1, "address 1 ");
Am. put (ad2, "address 2 ");

M. setAddresses (am );
           
Session. save (m );
Session. getTransaction (). commit ();
Session. close ();
        }
   
Public static void main (String [] args ){
Insert ();
    }
}

 

Note:

When running MemberHandler, you will find that the backend database has an additional data table test_member and test_component. The table content is as follows:

The code is as follows: Copy code

Mysql> select * from test_member;
+ ---- + ------ + ------- +
| ID | AGE | NAME |
+ ---- + ------ + ------- +
| 1 | 24 | Louis |
+ ---- + ------ + ------- +
1 row in set (0.00 sec)

Mysql> select * from test_component;
+ -------------- + ------------- + --------------- + ----------- +
| Component_id | address_name | home_street | home_zip_code | home_city |
+ -------------- + ------------- + --------------- + ----------- +
| 1 | address 1 | 1st Street | 123 | Guangzhou |
| 1 | address 2 | Street 2 | 456 | Shenzhen |
+ -------------- + ------------- + --------------- + ----------- +
2 rows in set (0.00 sec)

Mysql> desc test_member;
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
| ID | int (11) | NO | PRI | NULL | auto_increment |
| AGE | int (11) | YES | NULL |
| NAME | varchar (255) | YES | NULL |
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
3 rows in set (0.02 sec)

Mysql> desc test_component;
+ --------------- + -------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ --------------- + -------------- + ------ + ----- + --------- + ------- +
| Component_id | int (11) | NO | PRI | NULL |
| Address_name | varchar (255) | YES | NULL |
| Home_street | varchar (255) | NO | PRI | NULL |
| Home_zip_code | varchar (255) | NO | PRI | NULL |
| Home_city | varchar (255) | NO | PRI | NULL |
+ --------------- + -------------- + ------ + ----- + --------- + ------- +
5 rows in set (0.02 sec)

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.