Common face questions in Java framework

Source: Internet
Author: User
Tags joins to domain

How does hibernate work and why should I use it?
Principle:
1. Read and parse the configuration file 2. Read and parse the mapping information and create the SessionFactory3. Open Sesssion4. Creates a transaction Transation5. Persistence Operation 6. Commit TRANSACTION 7. Close the Session8. Turn off Sesstionfactory Why use: Hibernate performance is very good because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer. Hibernate is a major JDBC-based persistence framework and is an excellent ORM implementation that greatly simplifies the coding of DAO layers hibernate uses the Java reflection mechanism rather than bytecode enhancer to achieve transparency

How does hibernate delay loading?
1. Hibernate2 Deferred load implementation: a) Entity object B) Collection (Collection) 2. Hibernate3 provides lazy loading of properties when hibernate is querying the data, the data does not exist in memory, and when the program actually operates on the data, the object exists in memory, and the delay is loaded, which saves the server's memory overhead and improves the server's performance.


How do the relationships between classes be implemented in hibernate? (e.g. one-to-many, many-to-many relationships)
The relationship between classes and classes is mainly manifested in the relationship between tables and tables, they operate on objects, and in our program we map all the tables and classes together, and they pass the Many-to-one, One-to-many, Many-to-many said hibernate cache mechanism internal cache exists hibernate is also called a first-level cache, belongs to the application of the thing-level cache two cache: a) application and cache B) distributed cache conditions: data will not be modified by third parties, data size in an acceptable range, Low data update frequency, the same data is frequently used by the system, non-critical data c) third-party cache implementation Hibernate query method SQL, CRITERIA,OBJECTCOMPTOSITIONHQL: Attribute query parameter query, named parameter Query association query paging query Statistical functions
How to optimize hibernate?
Use bidirectional one-to-many associations, do not use one-way pair more flexible use unidirectional one-to-many associations do not use a single-sided, with many to replace the configuration object cache, not using the collection cache a pair of multi-collection use bag, multi-to-many sets using Set inheritance class using explicit polymorphic table fields less, the tables associated not afraid of


Spring working mechanism and why to use
1.spring MVC requests that all requests be submitted to Dispatcherservlet, which will delegate the other modules of the application system responsible for the actual processing of the request. 2.DispatcherServlet queries one or more handlermapping, When a request is found to be processed Controller.3.dispatcherservlet requests submitted to the target Controller4.controller for business logic processing, a modelandview5.dispathcher query is returned for one or more viewres Olver View parser, locate the view object specified by the Modelandview object 6. The View object is responsible for rendering back to the client. Why use: AOP allows developers to create non-behavioral concerns, called crosscutting concerns, and insert them into application code. With AOP, public services (such as logs, persistence, transactions, and so on) can be decomposed into facets and applied to domain objects without increasing the complexity of the object model of the domain object. IOC allows you to create an application environment where objects can be constructed, and then pass their collaboration objects to those objects. As the word inversion shows, the IOC is like the reverse JNDI. Not using a bunch of abstract factories, service locators, single elements (singleton), and direct constructs (straight construction), each object is constructed with its collaboration objects. Therefore, the collaboration object (collaborator) is managed by the container. Spring even an AOP framework, is also an IOC container. The best part of Spring is that it helps you replace objects. With Spring, you simply add dependencies (collaboration objects) with the JavaBean property and configuration file. You can then easily replace collaboration objects with similar interfaces when you need them


how to optimize when conditional queries are slow in the database
1. Build an index 2. Reduce the association between Tables 3. Optimize SQL, try to get SQL to locate data quickly, do not let SQL do full table query, should go index, A table with large data is ranked in front 4. Simplify query fields, useless fields do not, already control the return results, try to return a small amount of data in Hibernate multi-table query, each table takes several fields, that is, the query out of the result set does not have an entity class corresponding to it, How to solve this problem solution one, according to object[] data out of the data, and then their own group Bean solution Two, the bean write constructor for each table, such as table one to find out field1,field2 two fields, then there is a constructor is the bean (type1 Filed1,type2 field2), and you can generate this bean directly in HQL.
What are the core classes of hibernate and what are their interrelationships? What are the important ways?

Configurationsessionfactorysession the following methods Saveloadupdatedelete

What are the main types of data table mapping relationships in Hibernate
One-to-manyinverse: The main control side, the foreign key relationship has who control inverse=false is the main control side, the foreign key is controlled by it Inverse=true is the accused side, The foreign key has nothing to do with it. To achieve the control of the main control party must be the property of the Cascade: Cascade Main Table increment from table to fix Main Table delete from table Delete lazy: Delay Lazy=false: Remove all content, no delay (common) lazy= True: Remove part of the content, the rest of the content is dynamically fetched through get can be removed from each other's content


the difference between JDBC, Hibernate, and Ibatis
JDBC: manual, Manual write Sqldelete, insert, update to pass the value of an object to a SQL, not directly to an object select: Returns a ResultSet, one line from the resultset, A field is taken out of a field and then encapsulated in an object, not directly returning an object. Ibatis Features: Semi-automated SQL to manually write delete, insert, UPDATE: Pass directly to an object select: Returns an object directly hibernate: Automatic non-write SQL, automatically encapsulates delete, insert, UPDATE: Passing directly to an object select: Returns an object directly

three states of Hibernate
Transient state (Transient), persistent State (persistent), off-pipe (Detached). Objects that are in persistent state are also known as PO (Persistence object), and instantaneous and de-tube objects are also known as VO (Value object). Transient state a Java object that opens up memory space by the new command, eg. person person = new Person ("amigo", "female"), or if no variable is referenced to the object, it will be reclaimed by the Java virtual machine. Instantaneous objects exist in memory, it is the carrier that carries the information, does not have any relation with the database data, in Hibernate, the instantaneous object can be associated with the database through the session's save () or Saveorupdate () method, and the data corresponding to the inserted database , the instantaneous object is transformed into a persisted object. An object in this state with persistent states has a corresponding record in the database and has a persistent identity. In the case of Hibernate's Delete () method, the corresponding persistent object becomes instantaneous, because the corresponding data in the database has been deleted, and the object is no longer associated with the database's record. When a session executes close () or clear (), evict (), the persisted object becomes a de-tube object, at which point the persisted object becomes a de-tube object, while the object has a database recognition value, but it is not under the management of Hibernate persistence layer. Persistent objects have the following characteristics: 1. Associated with the session instance; 2. There is a record associated with it in the database. Off-state when the session associated with a persistent object is closed, the persisted object is transformed into a de-tube object. When the off-tube object is re-associated to the session, it is again transformed into a persistent object. The de-tube object has the recognition value of the database and can be transformed into a persistent object by means of update (), Saveorupdate () and so on. The off-pipe object has the following characteristics: 1. Essentially the same as an instantaneous object, when no variable references it, the JVM reclaims it at the appropriate time; 2. A database record identification value is more than an instantaneous object.
talking about the function of inverse in hibernate
The inverse property defaults to false, which means that both sides of the relationship maintain the relationship. For example, student and teacher are many-to-many relationships, with an intermediate table teacherstudent maintenance. GP) I if student this side inverse= "true", then the relationship is maintained by the other end teacher, meaning teacherstudent tables (intermediate tables) are not manipulated when student is inserted. Actions on the intermediate table are triggered only when teacher is inserted or deleted. So it is wrong to inverse= "true" on both sides, causing no action to trigger the effect on the intermediate table, and when both sides inverse= "false" or default, two relationships are inserted in the intermediate table. 3 kinds: hql, qbc--query by Criteria API, native SQL (created through createsqlquery) say the difference between update () and Saveorupdate () in Hibernate, Session load () and get () the difference. The Saveorupdate () method can implement the function of update (), but it will take more steps, as follows: If the object has been persisted in the session, it is not manipulated; The identifier property of the object (identifier) Does not exist in the database or is a temporary value, call the Save () method to save it, if another object in the session has the same identifier throws an exception, none of the above conforms to the call Update () updates. The Session.load/get method can read records from the database based on the specified entity class and ID, and returns the corresponding entity object. The difference is that if a qualifying record is not found, the Get method returns NULL, and the load method throws a Objectnotfoundexception;load method to return the entity's proxy class instance, and the Get method returns the entity class forever The Load method can take full advantage of the existing data in the internal cache and the level two cache, while the Get method only makes data lookups in the internal cache, and if no corresponding data is found, it will pass through the level two cache and directly call SQL to complete the data read

What cache policies are supported by Hibernate and compare their pros and cons
Read-only: This strategy applies to data that is frequently read but not updated, which is by far the simplest and most efficient caching strategy Read/write: This strategy applies to data that needs to be updated, is more resource-intensive than read-only, and in a non-JTA environment, Each transaction needs to be called in Session.close and Session.disconnect () nonstrict Read/write: This policy does not guarantee that two simultaneous transactions will modify the same piece of data, This strategy applies to data that is often read but rarely updated transactional: This strategy is a fully transactional cache policy that can be used in a JTA environment

How to view the SQL that hibernate generates and executes
In the file applicationconfig.xml that defines the database and database properties, set Hibernate.show_sql to true so that the generated SQL will appear on the console note: Doing so will add to the burden on the system and is not conducive to performance tuning

the advantages and disadvantages of three different retrieval strategies of hibernate
1 immediate retrieval; Pros: Fully transparent to the application, regardless of whether the object is persisted or in a free State, the application can easily navigate from one object to the object associated with it; disadvantage: too many 1.select statements, 2. It is possible to load objects that the application does not need to access, wasting a lot of memory space; 2 Deferred retrieval: Advantages: The application determines which objects need to be loaded, avoids the ability to execute redundant SELECT statements, and avoids loading objects that the application does not need to access. Therefore, it can improve the retrieval performance and save the memory space; disadvantage: If the application wants to access the Free State proxy class instance, it must ensure that he has been initialized in the persistent state; 3 urgent left outer connection search advantages: 1 completely transparent to the application, regardless of whether the object is in a persistent state or a free State, An application can easily navigate an object to the object it is associated with. 2 The use of outer joins, the number of SELECT statements is small; disadvantage: 1 May load objects that the application does not need to access, waste a lot of memory space, and 2 complex database table connections can also affect the retrieval performance;
What is the hibernate concurrency mechanism how to handle concurrency problems
A, Hibernate session object is non-thread-safe, for a single request, a single session, a single unit of work (that is, a single transaction, a single thread), it is usually used only once, and then discarded. If a session instance allows sharing, those that support concurrent runs, such as HTTP request,session beans, will cause resource contention. If you have hibernate session in the HTTP session, you may have synchronous access to the HTTP session. As long as the user clicks on the browser's "refresh" quickly enough, it causes two concurrently running threads to use the same session. b, multiple transactions concurrently access the same piece of resources, may throw the first kind of lost updates, dirty read, Phantom Read, non-repeatable read, the second class lost update a series of problems. Solution: Set the transaction isolation level. Serializable: Serialization. Isolation level up to repeatable READ: Repeatable READ Committed: Committed data reads READ UNCOMMITTED: UNCOMMITTED data read. Lowest isolation level C, set Lock: Optimistic lock and pessimistic lock. Optimistic Lock: Use version number or timestamp to detect update loss, set optimistic-lock= "All" in the map to implement version checking without a version or timestamp attribute mapping, hibernate compares the status of each field of a row of records Row-Level pessimistic locks: hibernate always uses the locking mechanism of the database, never locking objects in memory! Just specify the isolation level for the JDBC connection, and then let the database take care of everything.
class Lockmode defines the different locking levels required for hibernate

Lockmode.upgrade,lockmode.upgrade_nowait,lockmode.read;

hibernate refused connection, server crashes cause
1. DB does not open 2. There may be a problem with the network connection 3. The connection configuration is 4 wrong. Whether the driven Driver,url are all written to the 5. Lib to add the appropriate driver, the data connection code is wrong 6. The database configuration may have issue 7. There are too many current joins, and the server has a limit of 8 visitors. The corresponding port of the server is not open, that is, it does not provide the corresponding service


Common face questions in Java framework

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.