Getting Started 10 - Component Mapping
Consider such an object category:
User.java
Package online.
PUBLIC CLASS User {
Private string id;
PRIVATE STRING NAME;
Private char sex;
PRIVATE INT AGE;
PRIVATE STRING ADDRESS
........
}
We create a database table corresponding to this category:
Create Table User
ID Char (32) Not Null,
Name varchar (16) Not null,
Sex char (1),
Age INTEGER,
Address Varchar (255) Not Null,
Primary Key (ID)
);
Correspondingly, you can write image files to reach in accordance with the topics described previously, however we want to indicate the address attribute in a custom object email so that we use this custom object as an auxiliary object, in appropriate Time, we can directly call the sendmail () method of the email object, that is, our User category becomes:
User.java
Package online.
PUBLIC CLASS User {
Private string id;
PRIVATE STRING NAME;
Private char sex;
PRIVATE INT AGE;
PRIVATE EMAIL Email;
Public int getage () {
Return Age;
}
Public string getId () {
Return ID;
}
Public string getname () {
Return Name;
}
Public char getsex () {
Return SEX;
}
Public email getemail () {
Return email;
}
Public void setage (INT i) {
AGE = I;
}
Public void setid (String String) {
ID = String;
}
Public void setname (String string) {
Name = string;
}
Public void setsex (char c) {
SEX = C;
}
Public void setemail (email email) {
THIS.EMAIL = Email;
}
}
And our new Email category is designed as follows:
Email.java
Package online.
PUBLIC CLASS Email {
PRIVATE STRING ADDRESS
Public void setaddress (String address) {
THIS.ADDRESS = Address;
}
Public string getaddress () {
Return Address;
}
Public void sendmail () {
System.out.println ("Send Mail TO:" Address;
}
}
In fact, we just extract the user's Email information and independently for an auxiliary object, which is designed to operate in the application, and in fact, the information in the database table does not increase, we don't have to change The content of the data sheet. Obviously, if this is designed, the object and data table is not a list, the number of objects will be more than the data sheet, the object's design particle size is more than the data sheet, in order to complete the object and data table, Use the
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
id>
Property>
component>
clas>
hibernate-maping>
This mapping file is mainly compared to the previous mapping file, which is mainly red
User User = new user ();
User.setname ("Caterpillar"); user.setsex ('m');
User.Setage (28);
Email email = new email ();
Email.SetAddress ("justin@caterpillar.onlyfun.net");
User.semail (email);
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
Session.save (user);
TX.comMit ();
session.close ();
The following program fragment demonstrates how to query the data:
Session session = sessionFactory.openSession ();
List users = session.find ("from user");
session.close ();
For (listiterator itrator = users.listiterator (); item.hasnext ();) {
User User = (user) iterator.next ();
System.out.println (user.getname ()
"/ N / TAGE:" user.getage ()
"/ n / tsex:" user.getSex ());
User.getemail (). Sendmail ();
}
About Component further settings, you can take a look at 5.1.12 content of the reference manual.