In Java, elements of the Hibernate Persistence class (map) set attributes are component ing.

Source: Internet
Author: User

This article will introduce the elements of the Hibernate Persistence class (map) set attribute in Java as component ing. I hope this tutorial will help you.

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 <String, Address> addresses = new HashMap <String, Address> ();


Public Map <String, Address> getAddresses (){
Return addresses;
}
Public void setAddresses (Map <String, Address> 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. component class of persistence class

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. Persistence class ing File

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"/>
<Map-key column = "component_key" type = "string"/>
<Composite-element class = "Address">
<Parent name = "owner"/>
<Property name = "street" column = "home_street"> </property>
<Property name = "zipCode" column = "home_zip_code"> </property>
<Property name = "city" column = "home_city"> </property>
</Composite-element>
</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 <String, Address> am = new HashMap <String, Address> ();
Am. put ("address 1", ad1 );
Am. put ("address 2", ad2 );

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> show tables;
+ --------------------- +
| Tables_in_hibernate |
+ --------------------- +
| Test_component |
| Test_member |
+ --------------------- +
2 rows in set (0.00 sec)

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 | home_street | home_zip_code | home_city | component_key |
+ -------------- + ------------- + --------------- + ----------- + --------------- +
| 1 | 1st Street | 123 | Guangzhou | Address 1 |
| 1 | St. 2nd | 456 | Shenzhen | Address 2 |
+ -------------- + ------------- + --------------- + ----------- + --------------- +
2 rows in set (0.00 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.