① Ing component properties ①: component properties are of the basic type

Source: Internet
Author: User

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

Hibernate. cfg. xml:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->

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 Name() {}public Name(String firstname, String lastname) {this.firstname = firstname;this.lastname = 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.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 DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Test. Java:

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

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.