Getting started with Hibernate BLOG [5. hibernate components]

Source: Internet
Author: User

[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

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.