5.7 ing component attributes: The child attributes of the component attributes are of the basic type.

Source: Internet
Author: User

Component PropertiesThe persistence class is not a basic data type, nor a string, date, or other scalar type variable, but a composite type object. During the persistence process, it is only treated as a value type, rather than referencing another persistent entity.

To map component attributes, Hibernate provides<Component.../>Each <component.../> element maps a component property. When you use <component.../> to map component attributes, you must specify a name attribute to specify the attribute name of the component.

A custom class usually has its own attributes. If its attributes are of the basic type, string type, or date type, you can set it to <component... /> Add <property... /> child meta maps child attributes of component attributes. If the sub-attribute is not of the basic type, string type, or date type, the following sub-elements can appear in the <component.../> element:

Child element of <component.../>
<Component.../> If the sub-attribute is of another component type, use the <component.../> sub-element again in <component.../> to map
Set ing Element If the sub-attribute is of the array type or set type, you can... /> use <set... />, <list... />, <map... />
And other sub-elements map these set attributes.
Associate a ing Element If the sub-attribute is referenced by another persistent instance, you can... />, use <role-to-one... />, <one-to-one... />.

Delete the table in the test database first:

Create a new web project and write the code:

Person. Java:

public class Person {private int id;private int age;private Name name;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Name getName() {return name;}public void setName(Name name) {this.name = name;}}

Name. Java:

public class Name {private String firstname;private String lastname;private Person owner;public String getFirstname() {return firstname;}public void setFirstname(String firstname) {this.firstname = firstname;}public String getLastname() {return lastname;}public void setLastname(String lastname) {this.lastname = lastname;}public Person getOwner() {return owner;}public void setOwner(Person owner) {this.owner = owner;}}

Person. HBM. xml:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    

Test. Java:

Public class test {public static void main (string [] ARGs) {person p1 = new person (); p1.setage (20); Name name1 = new name (); name1.setfirstname (""); name1.setlastname (""); p1.setname (name1); person P2 = new person (); p2.setage (25); Name name2 = new name (); name2.setfirstname ("Cao"); name2.setlastname ("Mengde"); p2.setname (name2); Session session = hibernatesessionfactory. getsession (); transaction TXT = session. begintransaction (); Session. save (P1); Session. save (P2); TXT. commit (); hibernatesessionfactory. closesession ();}}

Run test. Java to view the database:

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.