Java face test-persistence layer 10 __java

Source: Internet
Author: User
Tags connection pooling object model sql injection
java face test-persistence layer 10

91, what is ORM.
Object-Relational mapping (object-relational Mapping, or ORM) is a technique to solve the problem of mismatch between the object-oriented model of program and database;

To put it simply, ORM is to automatically persist objects in a program to relational databases or to convert rows from relational database tables into Java objects by using metadata that describes the mapping between the object and the database (in Java, XML or annotations). Essentially, it transforms data from one form to another.


is Sessionfactory thread safe in 92,hibernate? is the session thread safe (two threads can share the same session).
Sessionfactory corresponds to the concept of a data store for hibernate, which is thread-safe and can be accessed concurrently by multiple threads. Sessionfactory are typically built only when they are started. For applications, it is best to encapsulate sessionfactory through a single example mode for easy access.

Session is a lightweight, non thread-safe object (the session cannot be shared between threads), which represents a unit of work that interacts with the database. The session is created by Sessionfactory and is closed after the task completes. The session is the main interface provided by the persistence layer service externally.

The session delays the acquisition of the database connection (i.e., it is acquired when it is needed). To avoid creating too many sessions, you can use Threadlocal to bind the session to the current thread, so that the same thread always gets the same session. The Getcurrentsession () method of Sessionfactory in Hibernate 3 can be done.


93,session's Save (), update (), merge (), lock (), Saveorupdate (), and persist () methods are respectively. What's the difference.
The objects of Hibernate have three states: instantaneous state (transient), persistent state (persistent) and Free State (detached).

An instance of a transient state can be changed into a persistent state by invoking the Save (), persist (), or saveorupdate () method;

An instance of a free State can become persistent by calling update (), Saveorupdate (), lock (), or replicate (). Save () and persist () will raise the SQL INSERT statement, and update () or merge () will raise the UPDATE statement.

The difference between save () and update () is that a transient state object is changed to a persistent state, and a Free State object is changed to a persistent state. The merge () method completes the functionality of the Save () and update () methods, with the intention of merging the new state into an existing persisted object or creating a new persisted object.

For the Persist () method, follow the instructions in the official documentation:

1. The persist () method persists an instance of a transient state, but does not guarantee that the identifier is immediately filled into the persisted instance, and the filling of the identifier may be deferred to the flush time;

2, persist () method to ensure that when it is called outside a transaction does not trigger an INSERT statement, when the need to encapsulate a long session process, the persist () method is necessary;

3. The save () method does not guarantee 2nd, which returns an identifier, so it immediately executes the INSERT statement, whether inside or outside the transaction. As for the difference between the lock () method and the update () method, the update () method changes the object that has been changed from the state of the removal to a persistent state, and the lock () method makes a persistent state of an object that has not changed the state of the removal.


94, which describes the process of session loading entity objects. &NBSP
        1, session, before calling the database query function, is first searched through the entity type and primary key in the level one cache. If the first-level cache lookup hits and the data state is valid, it returns directly; &NBSP
        2, if the first-level cache does not hit, The next session will be in the current nonexists record (equivalent to a query blacklist, if a duplicate invalid query can be quickly judged, thereby enhancing performance) to find, if the same query conditions in nonexists, then return null; 
        3, if the first level of cache query failed to query level two cache, if two cache hit directly back; &NBSP
         4, if none of the previous queries were hit, a SQL statement is issued, and if the query does not find the corresponding record, the query is added to the nonexists of the session to be logged and returned null; 
        5, resultset based on mapping configuration and SQL statements, and creation of corresponding entity objects; &NBSP
         6, the management of the object into the session (first-level cache); &NBSP
         7, if there is a corresponding interceptor, execute the OnLoad method of the Interceptor; &NBSP
        8, If you turn on and set up to use level two caching, the data object is included in level two cache;  
        9, returns the data object.


What is the difference between writing placeholders using # and $ in 95,mybatis.
#将传入的数据都当成一个字符串, quotes are automatically added to the incoming data;

$ The incoming data is displayed directly in SQL.

Note: Using the $ placeholder can result in SQL injection attacks, where you can use the # instead of $, and instead of # when writing an ORDER BY clause.


96, explain the role of the MyBatis namespace (namespace).
In large projects, there may be a large number of SQL statements, at which point a unique identification (ID) for each SQL statement becomes difficult. To solve this problem, in MyBatis, you can have a unique namespace for each mapping file, so that each SQL statement defined in the mapping file becomes an ID defined in this namespace. As long as we can guarantee that this ID in each namespace is unique, no conflict will occur even if the statement IDs in the different mapping files are the same.


97, what is the meaning of dynamic SQL in MyBatis.
For some complex queries, we may specify multiple query criteria, but these conditions may or may not exist, and if you do not use the persistence layer framework we may need to assemble the SQL statements ourselves, but MyBatis provides dynamic SQL functionality to solve this problem. The main elements in MyBatis for implementing dynamic SQL are:
-If-choose/when/otherwise-trim-where-set-foreach

Use examples:

<select id= "foo" parametertype= "blog" resulttype= "blog" >
SELECT * from T_blog where 1 = 1 <if test= "title!= null" >
and title = #{title} </if> <if test= "content!= null" >
and content = #{content} </if> <if test= "owner!= null" >
and owner = #{owner} </if> </select>

98,JDBC Programming has some deficiencies, mybatis is how to solve these problems.

1, JDBC: Database link creation, release frequently cause system resources waste to affect system performance, if the use of database link pool to resolve this issue.

MyBatis: Configure the data link pool in Sqlmapconfig.xml and use connection pooling to manage database links.

2, Jdbc:sql statement written in the code to cause the code is not easy to maintain, the actual application of SQL changes may be large, SQL changes need to change the Java code.

MyBatis: The SQL statement is configured in the Xxxxmapper.xml file and separated from Java code.
3. JDBC: Trouble passing parameters to SQL statements, because the WHERE condition of the SQL statement is not necessarily, may be more or less, placeholder needs and parameter one by one corresponds.

Mybatis:mybatis automatically maps Java objects to SQL statements.
4,JDBC: The result set parsing trouble, SQL changes lead to parsing code changes, and the need to traverse before parsing, if the database records encapsulated into Pojo object resolution is more convenient.

Mybatis:mybatis automatically maps the results of SQL execution to Java objects.


What is the difference between 99,mybatis and hibernate?

1. Unlike MyBatis and hibernate, it is not exactly an ORM framework because MyBatis requires programmers to write their own SQL statements, but MyBatis can flexibly configure the SQL statements to run through XML or annotations. The Java object and the SQL statement map are generated to generate the final execution of the SQL, and finally the results of SQL execution are mapped to generate Java objects.
2, MyBatis learning threshold is low, simple and easy to learn, programmers directly compile the original eco-SQL, can strictly control the performance of SQL, flexibility, is very suitable for relational data model requirements are not high software development, such as Internet software, enterprise operation software, etc., because such software requirements change frequently, A change in demand requires a rapid output of results.                However, the flexible premise is that mybatis can not do database independence, if you need to implement software that supports multiple databases, you need to customize multiple sets of SQL mapping files, the workload is large. 3, Hibernate object/Relationship mapping ability, good database independence, for relational model requirements of software (such as the need for fixed customized software) if the use of hibernate development can save a lot of code, improve efficiency. But the disadvantage of hibernate is the high threshold of learning, to master the threshold is higher, and how to design O/R mapping, how to balance performance and object model, and how to use good hibernate need to have strong experience and ability to do.
In short, in accordance with the needs of users in a limited resource environment, as long as the maintenance, scalability, good software architecture is a good structure, so the framework is only suitable for the best.

(This can also be combined with their own understanding to say, don't say can't hold)


100, simply say the first level cache and level two cache of MyBatis.

MyBatis first the query result set in the cache, if there is no query the database, if there are out from the cache to return the result set does not go to the database. The MyBatis internal storage cache uses a hashmap,key as the Hashcode+sqlid+sql statement. Value to map generated Java objects from query
The MyBatis level two cache, which is the query cache, is scoped to a mapper namespace that queries SQL from the same namespace to fetch data from the cache. Second-level caching can span sqlsession.


Java Companion public number of major companies to organize a number of commonly used interview pen questions, for everyone in the spare time to learn some of the topics, accumulated over time, wait for the interview, everything is ripe, the interview will naturally be easier.

Related Article

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.