Java learning notes 01, java learning notes

Source: Internet
Author: User
Tags xml example

Java learning notes 01, java learning notes

1. Prototype Design:

The module, element, and human-computer interaction form of the page are separated from the skin by using the method described in wireframes.

2. The following is the hierarchical structure of the persistence layer designed using PowerDesigner:

The dotted triangle represents"Implementation Interface"The real-line triangle"Inheritance"

 

The dependency inversion principle is used: the high-level module should not be dependent on the underlying layer.

Specifically, the AbstractBaseDao abstract class adopts the template mode in the design mode to selectively implement specific functions.

The facade mode is used.

3. Object-Relational Mapping (ORM:

The role is to map between a relational database and an object. Maps an Object to a Relation, and maps a link to an Object. In this way, we do not need to deal with complex SQL when operating the database, you only need to operate on it like an operation object (map the fields of the relational database to the attributes of the object in the memory ).

From this, we can see the six core interfaces of Hibernate, two main configuration files, and their direct relationship. All Hibernate content is here. Let's take a simple look from top to bottom. Each interface is summarized in one sentence.

1. Configuration Interface: configures and starts Hibernate

2. SessionFactory interface: initializes Hibernate

3. Session interface: Responsible for CRUD operations on persistent objects

4. Transaction interface: Responsible for transactions

5. Query interface and Criteria interface: responsible for executing various database queries

 

Note: The Configuration instance is an object during startup. Once SessionFactory is created, it is discarded.

The hibernate. cfg. xml example code is as follows:

1 <! DOCTYPE hibernate-configuration PUBLIC 2 "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 4 5 

 
// The Order. hbm. xml Code is as follows:

1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC 3 "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5

The object Order code is as follows:

package com.djdj.entity;import java.io.Serializable;import java.util.Set;public class Order implements Serializable{           private int id;           private int orderNo;           private Set<OrderItem> item;                    public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public int getOrderNo() {            return orderNo;        }        public void setOrderNo(int orderNo) {            this.orderNo = orderNo;        }        public Set<OrderItem> getItem() {            return item;        }        public void setItem(Set<OrderItem> item) {            this.item = item;        }           }

The code for Hibernate to call the configuration file to create a database table is as follows:

Public class CreateTable {public static void main (String [] args) {// read Configuration cfg = new Configuration (). configure (); SchemaExport export = new SchemaExport (cfg); export. create (true, true );}}

The test code is as follows:

Public class Test {public static void main (String [] args) {// read Configuration cfg = new Configuration (). configure (); // obtain SessionFactory factory = cfg. buildSessionFactory (); // get session Session session = null; try {session = factory. openSession (); // start the transaction session. beginTransaction ();

//// Write the data operation code in the middle
//
Session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace ();} finally {session. close (); // do not change offline // u. setName ("1 ");}}}

Take User as an example. The code for adding, deleting, querying, and modifying a User is as follows:

// Set user // User user = new User (); // user. setName ("st2"); // user. setPwd ("1112"); // save is a transient object, followed by a persistent object: Related to database data // session. save (user); // query object // User u = (User) session. get (User. class, 2); // load get proxy object, high efficiency, only get real objects in use, get real objects directly. // User u = (User) session. load (User. class, 2); // System. out. println (u. getName (); // delete an object // User u = (User) session. get (User. class, 2); // System. out. println (u. getName (); // session. delete (u); // modify the Object User u = (User) session. get (User. class, 2); System. out. println (u. getName (); u. setName ("sys"); session. update (u );

Cascade operations:

Casade is used to describe whether to perform similar operations on the associated slave object when performing some operation on the master object. cascade is commonly used:

None, all, save-update, delete, lock, refresh, evict, replicate, persist,

Merge, delete-orphan (one-to-multiple ). In general, cascade-to-one and cascade-to-sequence are not set. cascade is set in <one-to-one> and <one-to-sequence>.

Sagacity_shen

It was originally set at 00:58:34

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.