A simple example of Hibernate Component ing

Source: Internet
Author: User

Create a related database table: [SQL] create table c_user (id int (11) not null auto_increment, age int (11) default null, firstname varchar (50) default null, lastname varchar (50) default null, address varchar (200) default null, zipcode varchar (10) default null, tel varchar (20) default null, primary key (id )) engine = innodb default charset = gbk; Note: This table is a user information table, which can be divided into two parts: one part is name (name), including firstname (surname) and lastname (name); the other is co Ntact (contact information), including address, zipcode, and tel. When creating an object class, you can encapsulate name and contact into two independent classes, which improves the reusability and flexibility of the system. That is to say, you need to use Component ing to map other entity classes together. Create a Name. java class: [java] package collect. component; import java. io. serializable; public class Name implements Serializable {private String firstname; private String lastname; public String getFirstname () {return firstname;} public void setFirstname (String firstname) {this. firstname = firstname;} public String getLastname () {return lastname;} public void setLastname (String lastname) {this. las Tname = lastname ;}} Contact type related to the Contact method. java: [java] package collect. component; import java. io. serializable; public class Contact implements Serializable {private String address; private String zipcodes; private String tel; public Contact () {} public String getAddress () {return address ;} public void setAddress (String address) {this. address = address;} public String getZipcodes () {return zipco Des;} public void setZipcodes (String zipcodes) {this.zip codes = zipcodes;} public String getTel () {return tel;} public void setTel (String tel) {this. tel = tel ;}} finally created the object class Cuser corresponding to the database table. java: [java] package collect. component; public class Cuser {private Integer id; private Integer age; private Name name; private Contact contact; public Integer getId () {return id;} public void setId (Integer Id) {this. id = id;} public Integer getAge () {return age;} public void setAge (Integer age) {this. age = age;} public Name getName () {return name;} public void setName (Name name) {this. name = name;} public Contact getContact () {return contact;} public void setContact (Contact contact) {this. contact = contact ;}} creates a cing file Cuser. hbm. xml: [html] <? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Related Article

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.