Interview Java Persistence layer (10)

Source: Internet
Author: User
Tags relational database table sql injection attack

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

To put it simply, ORM is to automatically persist objects in a program to a relational database or to convert rows in a relational database table into Java objects by using metadata that is mapped between the object and the database (in Java, XML or annotations). It is essentially converting data from one form to another.

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

The session is a lightweight, non-thread-safe object (a 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 is completed. The session is the primary interface that the persistence layer service provides externally.

The session will delay getting the database connection (that is, it is only available when needed). To avoid creating too many sessions, you can use Threadlocal to bind the session to the current thread so that the same thread will always get the same session. The Sessionfactory getcurrentsession () method in Hibernate 3 can be done.

What do the 93,session Save (), update (), merge (), lock (), Saveorupdate (), and persist () methods do? What's the difference?
Hibernate objects have three states: transient (transient), persistent (persistent), and free (detached).

Instances of transient states can be persisted by invoking the Save (), persist (), or saveorupdate () method;

An instance of a Free State can be persisted by calling update (), Saveorupdate (), lock (), or replicate (). Save () and persist () will throw an INSERT statement for SQL, and update () or merge () will throw an 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 can complete the functionality of the Save () and update () methods, with the intent of merging new states onto existing persisted objects or creating new persisted objects.

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 populated into the persisted instance, and that the identifier filling may be deferred to the flush time;

2, Persist () method ensures that when it is called outside of a transaction does not trigger an INSERT statement, when it is necessary to encapsulate a long session flow, the persist () method is necessary;

3. The save () method does not guarantee that 2nd, it returns an identifier, so it executes the INSERT statement immediately, whether inside or outside the transaction. As for the difference between the lock () method and the update () method, the update () method is to turn an object that has changed out of the tube state into a persistent state, and the lock () method turns an object that has not changed out of the tube state into a persistent state.

94, describes the process of loading entity objects session.
1, the session before invoking the database query function, the first level of the cache through the entity type and the primary key to find, if the first-level cache lookup hit and the data state is legitimate, then directly returned;
2, if the first-level cache is not hit, the next session will be in the current nonexists record (equivalent to a query blacklist, if duplicate invalid query can quickly make a judgment, thereby improving performance) in the search, if there is the same query conditions in nonexists, Then NULL is returned;
3, if the first level cache query failed to query level two cache, if the level two cache hit directly back;
4, if the previous query is not hit, the issue of the SQL statement, if the query did not find the corresponding record will be added to the session nonexists the query to record, and return null;
5, according to the mapping configuration and SQL statements get resultset, and create the corresponding entity object;
6, the object into the session (first-level cache) management;
7, if there is a corresponding interceptor, then execute the OnLoad method of the Interceptor;
8, if you open and set up to use a level two cache, the data objects into the two-level cache;
9. Returns the data object.

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

$ generates the incoming data directly in SQL.

Note: Using the $ placeholder may result in a SQL injection attack, where you can use # without using $, and write the ORDER BY clause with $ instead of #.

96, explain the role of the MyBatis namespace (namespace).
In large projects, there may be a large number of SQL statements, which makes it difficult to have a unique identity (ID) for each SQL statement. 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 that namespace. As long as we can guarantee that this ID is unique in each namespace, there will be no more conflicts even if the statement IDs in the different mapping files are the same.

97. What does dynamic SQL mean in MyBatis?
For some complex queries, we may specify multiple query conditions, but these conditions may or may not exist, and we may need to assemble the SQL statements ourselves if we do not use the persistence layer framework, but MyBatis provides the functionality of dynamic SQL to solve this problem. The elements used to implement dynamic SQL in MyBatis are:
-If-choose/when/otherwise-trim-where-set-foreach

Examples of usage:

   <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>
what are the shortcomings of 98,JDBC programming, and how does mybatis solve these problems?

1, JDBC: Database link creation, release frequently cause system resource waste to affect system performance, if use database link pool can solve this problem.

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

2, Jdbc:sql statements written in the code cause 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 configuration is separated from the Java code in the Xxxxmapper.xml file.
3. JDBC: It is troublesome to pass arguments to the SQL statement because the WHERE condition of the SQL statement is not necessarily, possibly more or less, and the placeholder needs to correspond to parameter one by one.

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

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

, What are the differences between MyBatis and hibernate?

1, MyBatis and hibernate, it's 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 with XML or annotations. The Java object and the SQL statement map are generated to the final executed SQL, and the result of SQL execution is then mapped to the Java object.
2, MyBatis learning threshold is low, simple and easy to learn, the programmer directly write the original ecological SQL, can strictly control the performance of SQL execution, flexibility, is very suitable for the relational data model requirements of software development, such as Internet software, enterprise operations software, etc., because such software requirements change frequently, But demand changes require a rapid output.                However, the premise of flexibility is that MyBatis can not do database independence, if you need to implement software that supports a variety of databases, you need to customize multiple sets of SQL mapping files, a lot of work. 3. Hibernate object/Relational mapping ability is strong, database independence is good, for the relational model of high-demand software (such as the need for fixed custom software) if Hibernate development can save a lot of code, improve efficiency. But Hibernate's disadvantage is that the learning threshold is high, mastering the threshold is higher, and how to design the O/R mapping, how to trade off between performance and object models, and how to use hibernate with great experience and ability.
In short, in accordance with the needs of users in a limited resource environment as long as the maintenance and extensibility of a good software architecture is a good architecture, so the framework only suitable is the best.

(here can also be combined with their own understanding said, don't say that can't hold)

100, simply say MyBatis's first-level cache and level two cache?

MyBatis first go to the cache to query the result set, if not query the database, if there is a cache out of the returned result set will not go to the database. The MyBatis internal storage cache uses a hashmap,key as a hashcode+sqlid+sql statement. Value is the Java object generated from the query-out mapping
MyBatis's Level Two cache is the query cache, which is scoped to a mapper namespace, where querying SQL in the same namespace can fetch data from the cache. A secondary cache can be cross-sqlsession.

Transfer from http://www.cnblogs.com/peke/p/7887626.html

Interview Java Persistence layer (10)

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.