[Java]
Link creation:
Configuration method. Reference other JavaBean in Javabean. And maps to the database table. In this way, you can use the component mechanism provided by hibernate. See the following case: (Javabean omitting the set and get methods)
1. Javabean
Name. Java:
Public class Name {
Private String FirstName;
Private String LastName ;}
User. java:
Public class User {
Private int id;
Private Name name; // composite attribute
Private Date birthday;
}
2. configuration file:
<? Xml version = "1.0"?>
<! DOCTYPE hibernate-mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
"Www.2cto.com">
<! -- Specify the package of the mapped class -->
<Hibernate-mapping
Package = "cn. hibernate. model">
<Class name = "User" table = "user">
<Id name = "id">
<! -- Primary key generator -->
<Generator class = "native"/>
</Id>
<! -- Composite build type -->
<Component name = "name">
<Property name = "firstName"/>
<Property name = "lastName"/>
</Component>
<Property name = "birthday"> </property>
</Class> www.2cto.com
</Hibernate-mapping>
3. Execute the insert operation to create a table:
Public static void main (String args []) {
Name n = new Name ();
N. setFirstName ("firstName ");
N. setLastName ("lastname ");
User u = new User ();
U. setName (n );
U. setBirthday (new Date ());
Session s = HibernateUtil. getSession ();
Transaction tr = s. beginTransaction ();
S. save (u );
Tr. commit ();
}
Result:
Author: zhang6622056