Detailed explanation of the first example of Hibernate

Source: Internet
Author: User

Create a Java project and import the shelving package User. java: [java] package com. pojo; public class User implements java. io. serializable {private Integer id; private String name; private Integer age; public Integer getId () {return this. id;} private void setId (Integer id) {this. id = id;} public String getName () {return this. name;} public void setName (String name) {this. name = name;} public Integer getAge () {return this. age;} Public void setAge (Integer age) {this. age = age ;}}resolution: You can see that the getter and setter method of this class uses the standard JavaBean naming convention, and the field) set the access level to private ). This is a recommended design, but it is not necessary. Hibernate can also directly access these fields. The advantage of using the access method is that it provides robustness during refactoring ). For a specific user, the id property holds the Unique identifier value. If we want to use all the features provided by Hibernate, all persistence entity classes (including secondary dependent classes) need such an identifier attribute. In fact, most applications (especially web applications) need to use identifiers to differentiate objects. Therefore, you should consider using the identifiers instead of using them as a limitation. However, we usually do not operate on the identity of an object, so the access level of its setter method should be declared private. In this way, only Hibernate can assign an Identifier value to an object when it is saved. You can see that Hibernate can directly access the public, private, and protected access methods and fields. So which method you choose depends on you, and you can make your choice consistent with your application design. All persistence classes require constructors with or without parameters, because Hibernate must use the Java reflection mechanism to create objects for you. Constructor can have a private access level. However, when a runtime proxy is generated, access control at least at the package level is required, in this way, it is more efficient to obtain data from the persistence class without bytecode instrumentation. Create Table "user" under Database "pla": Resolution: This table name has the same name as the User class, and the first letter is case-insensitive. User. hbm. xml: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <! -- Mapping file autogenerated by MyEclipse Persistence Tools -->

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.