[Java Face question] Framework Chapter II

Source: Internet
Author: User

How 1.Hibernate Works and why Hibernate is used? working principle:

1. Read and parse the configuration file

2. Read and parse mapping information, create Sessionfactory

3. Open session

4. Create Transaction Transation

5. Persistent operation

6. Commit a transaction

7. Close session

8. Close Sesstionfactory

why Hibernate is used (that is, its advantages ):

1. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.

2. Hibernate is a JDBC-based, mainstream persistence framework that is an excellent ORM implementation. He simplifies the coding of the DAO layer to a great extent

3. Hibernate uses the Java reflection mechanism rather than the bytecode enhancer to achieve transparency.

4. Hibernate mapping has excellent flexibility. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

the difference between get and load methods in 2.Hibernate

Hibernate for the Load method that the data in the database must exist, can be assured that the use of agents to delay loading, if the use of the process found problems, can only throw exceptions;

Hibernate for the Get method, hibernate must obtain the actual data, otherwise null is returned.

Specific Description:

For the Get method, hibernate confirms that the data exists for that ID, first in the session cache, then in the level two cache, without querying the database and returning NULL in the database.

The Load method loads the entity object, based on the configuration of the lazy attribute at the class level on the mapping file (True by default).

Sub-situation discussion:

If true, first look in the session cache to see if the ID corresponds to the existence of the object, does not exist using lazy loading, returns the entity's proxy class object (the proxy class is a subclass of the entity class, dynamically generated by Cglib). Wait until the specific use of the object (in addition to the OID) to query the level two cache and database, if you still do not find a qualifying record, will throw a objectnotfoundexception.

If False, it is the same as the Get method lookup order, except that a objectnotfoundexception will be thrown if the qualifying record is not found.

How is 3.Hibernate delayed loading?

Hibernate3 provides a lazy load feature for properties. When hibernate is querying the data, the data does not exist in memory, but when the program actually operates on the data, the object exists in memory, the delay is loaded, and he saves the server's memory overhead, thereby improving the performance of the server.

How does the relationship between classes be implemented in 4.Hibernate?

The relationship between classes and classes is mainly manifested in the operation of the relationship between tables and tables, all of which operate on objects, and we map all tables and classes together in the program, and they operate through Many-to-one, One-to-many, many-to-many in the configuration file.

What is the difference between update () and Saveorupdate () in 5.Hibernate?

Saveorupdate ():

If the object is already persisted in this session, do nothing

Throws an exception if another object associated with this session has the same persistent identity (identifier)

If the object does not have a persisted identity (identifier) attribute, call Save () on it

If the persistent identity of the object (identifier) indicates that it is a newly instantiated object, call Save () on it

If the object is accompanied by version information (through <version> or <timestamp>) and the value of the Version property indicates that it is a newly instantiated object, call Save (). otherwise update () this object.

update (): is to update the entity object of a free State directly.

6. Talk about Hibernate's caching mechanism.

First- level cache: The internal cache exists in Hibernate, which belongs to the application thing-level cache.

level Two cache: application-level caching, distributed cache.

Usage Scenarios: data is not modified by third parties, data size is acceptable, data update frequency is low, the same data is frequently used by the system, non-critical data

Introduce third-party caches (such as ehcache, etc.).

7. How to optimize hibernate?

Use bidirectional one-to-many associations without using

Flexible use of unidirectional one-to-many associations

You don't have to be a one-to-many substitution

Configure the object cache without using the collection cache

One-to-many collection using bag, multi-set using Set

Inheriting classes using explicit polymorphism

Table fields are less, tables associated not afraid of more, there is a level two cache backing

8. Talk about lazy loading and opensessioninview of Hibernate

The delay load should be in the session range, when used to load again;

Opensessioninview is writing a filter on the Web layer to open and close the session, which means that the session is open during a request, guaranteeing a delay in loading the session.

9. Brief description of Struts2 workflow

1. The client browser makes an HTTP request.

2, according to the Web. XML configuration, the request was Filterdispatcher received.

3, according to the Struts.xml configuration, find the action class and method that need to be called, and through the IOC way, the value is injected to Aciton.

4. Action invokes the business logic component to process the business logic, which includes form validation.

5, the action is completed, according to the configuration in the Struts.xml to find the corresponding result of the return, and jump to the corresponding page.

6. Returns the HTTP response to the client browser.

10. The design pattern of struts

MVC pattern

The Actionservlet is loaded and initialized when the Web application is started;

When a user submits a form, a configured Actionform object is created and populated with the corresponding data in the form;

Actionservlet determines whether form validation is required based on the settings configured for the Struts-config.xml file, and if necessary, select which action to send the request to after calling Actionform's validate () validation. If the action does not exist, Actionservlet creates the object first and then invokes the Execute () method of the action;

Execute () Gets the data from the Actionform object, completes the business logic, returns a Actionforward object, actionservlet the client request to the JSP component specified by the Actionforward object;

The Actionforward object specifies that the JSP generates a dynamic Web page that is returned to the customer.

Advantages and disadvantages of struts

Advantages:

Implementing the MVC pattern, with a clear structure, allows developers to focus only on the implementation of business logic.

With a wealth of tags to use, Struts's tag library (Taglib), if flexible, can greatly improve the development efficiency. In addition, for the current domestic JSP developers, in addition to the use of JSP comes with common tags, rarely develop their own tags, perhaps struts is a good starting point.

Page navigation. Page navigation will be a future development direction, in fact, to do so, so that the system's context clearer. Through a configuration file, you can grasp the connection between the parts of the whole system, which is of great benefit to the later maintenance. This is especially true when another group of developers took over the project.

Provides a exception processing mechanism.

Database Link Pool Management

Support i18n

Disadvantages:

To go to the presentation layer, you need to configure the forward, each go to the presentation layer, I believe most are directly to the JSP, and involves the steering, need to configure the forward, if there are 10 display layer of the JSP, need to configure 10 struts, but also does not include sometimes directories, file changes, Need to re-modify forward, note that each time the configuration is modified to require redeployment of the entire project, and tomcate such a server, you must restart the server, if the business changes frequently complex systems, such operations are simply unthinkable. Now that is so, tens of hundreds of people at the same time online use of our system, you can imagine how much I worry.

The action of Struts must be Thread-safe, which allows only one instance to handle all requests. So all the resources that the action uses must be synchronized uniformly, which raises the thread-safety issue.

The test is inconvenient. Each of struts's actions is coupled to the web layer so that its tests depend on the Web container and unit tests are difficult to implement. However, there is a junit extension tool that struts testcase can implement for its unit testing.

Type of conversion. The Formbean of struts takes all the data as a string type, which can be converted using the tool commons-beanutils. However, it is converted at the class level, and the type of conversion is not configurable. It is also very difficult to return the error message to the user when the type is converted.

The dependency on the servlet is too strong. Struts must rely on servletrequest and servletresponse when handling action, all of which are not able to get rid of the servlet container.

The front-end expression language aspect. Struts integrates Jstl, so it uses the JSTL expression language primarily to get data. But Jstl's expression language is weak in terms of collection and indexed properties.

Difficulty in controlling the action execution. Struts creates an action, which can be very difficult if you want to control the order in which it is executed. You'll even have to write the servlet again to implement your functional requirements.

The pre-and post-processing of the action. Struts handles action as a class-based hierarchies, which makes it difficult to operate before and after action processing.

insufficient support for events. In struts, it is actually a form form that corresponds to an action class (or Dispatchaction), in other words: in struts, it is actually a form that can only correspond to one event. Struts This event mode is known as a coarse-grained event compared to the Applicationevent,application event and the component event.  

12. Why use Spring (pros )?

Spring effectively organizes your middle-tier objects, whether or not you choose to use EJBS. If you just use struts or other framework,spring specifically designed for the Java EE API, focus on solving the remaining problems.

Spring can eliminate the overuse of singleton that is common in many projects. Excessive use of Singleton reduces the testability of the system and the degree of object-oriented.

By working with a configuration file in a way that is consistent across applications and projects, spring eliminates the need for a variety of custom-formatted properties files. Ever wondered what magical property items or system attributes a class was looking for, and had to read Javadoc or even source code? With spring, you just need to look at the JavaBean property of the class. The use of inversion Ofcontrol (discussed below) helped to achieve this simplification.

By minimizing the cost of programming interfaces rather than classes, spring can promote good programming practices.

Spring is designed so that applications created with it depend as little as possible on his APIs. Most business objects in a spring application are not dependent on spring.

Applications built using spring are easy to unit test.

Spring enables the use of EJBS to be an implementation choice, not an inevitable choice for application architectures. You can choose to use POJOs or local EJBS to implement the business interface without affecting the calling code.

Spring helps you solve many problems without using EJBS. Spring provides an alternative to EJBS that can be used in many Web applications. For example, spring can use AOP to provide declarative transaction management without passing through the EJB container, if you only need to deal with a single database and not even need a JTA implementation.

Spring provides a consistent framework for data access, whether it is using JDBC or O/R mapping products such as hibernate.

13. List some of the ways you know to implement spring transactions

Programmatic transaction management: code needs to be written manually, rarely used in real-world development,

Declarative transaction management based on Transactionproxyfactorybean, which needs to be configured for each class of transaction management

Declarative transaction management of XML based on ASPECTJ, no need to change classes, configure in XML file.

Annotation-based declarative transaction management with simple configuration and the need to add annotations to the business layer class

14. Talk about the isolation level and propagation behavior of spring transactions

Isolation level:

-Default isolation level using database defaults

-read_uncommitted will appear dirty read, non-repeatable read and Phantom read problem

-read_committed will appear repeat read and Phantom read

-Repeatable_read will appear phantom read

-serializable is the safest, but the most expensive, the performance impact is extremely serious

propagation Behavior:

-Required the transaction is present, and the transaction is created without being present

-Supports a transaction is present, does not exist and does not create a transaction

-Mandatory presence transaction is incorporated into the transaction, does not exist, throws an exception

-Requires_new Always Create new transactions

-Not_supported The presence of a transaction is suspended, and a non-transactional operation has been performed

-Never always executes non-transactional and throws an exception if a transaction is currently present

-Nested embedded transaction

15. What is a di mechanism?

Dependency Injection (Dependecy injection) and control inversion (inversion of controls) are the same concept, specifically: when a role requires another role to assist, in the traditional program design process, the caller is usually created to create an instance of the callee.

but the work of creating the callee in spring is no longer done by the caller, so called control inversion . The work of creating the callee is done by spring, and then injected by the caller is also referred to as dependency injection.

Spring manages objects in a dynamic and flexible manner, injecting two ways, setting injection and constructing injection.

  Advantages of setting injection : Intuitive, natural

  Advantages of construct injection : You can determine the order of dependencies in the constructor.

16. What is AOP?

Aspect-oriented programming (AOP) to refine spring's dependency injection (DI),

Aspect-oriented programming in spring is mainly represented in two areas:

1. To provide declarative transaction management for tangent-oriented programming

2.spring supports user-defined facets

[Java Face question] Framework Chapter II

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.