Hibernate face Test

Source: Internet
Author: User
Tags connection pooling naming convention



1. What does hibernate say?

Chinese translation is the meaning of hibernation, hibernate is an open source object-relational mapping framework that encapsulates JDBC and implements a mapping between Java entity classes and database tables. Achieve the purpose of data persistence.

2. What is ORM? What are ORM components?
ORM is an object-relational mapping, which is a program technique that enables you to manipulate a data table by manipulating Java objects by mapping Java objects to database tables. Common ORM Components are JDBC, Hibernate, MyBatis, springdate, etc.

What are the advantages of 3.Hibernate versus JDBC? What are the drawbacks?
Pros: Simplifies the repetitive code of the DAO Layer Access database
Hibernate is a fully automated ORM framework that automatically generates SQL statements that are automatically executed.
Hibernate's mapping is flexible and (database dialect) supports many relational databases, from one-to-one to many-to-many complex relationships
Hibernate provides caching mechanisms (session cache, level two cache, query cache) for improved performance.
Cons: Hibernate is a heavyweight framework that is too powerful for JDBC, loses control of SQL, and cannot be optimized for SQL. Hibernate performance is much lower than JDBC in the face of bulk operations.


4. How to build a hibernate environment
1. First import the jar package and configuration file, Hibernate start Session tool class.
2. Configure the database's basic information and database dialect in the configuration file
3. To test, first create the entity class and the tables in the database. Create a mapping file,
A naming convention is an entity class name. Hbm.xml. The position should be similar to the entity under a package.
Configure the mapping relationship between the entity class and the database table in the mapping file.
Add the path to the mapping file in the Hibernate.cfg.xml configuration file.
4. Create a sessionfactory through the Hibernate tool class, create a Session object through the factory, open the transaction through the session, and after the data operation, the transaction commits.


5. How do I convert the three states of Hibernate?
Hibernate's three states are transient, persistent, managed
Transient state: There are no objects in the session cache and no corresponding records in the database
Persistent state: There are objects in the session cache with corresponding records in the database
Managed state: There are no objects in the session cache, and there are corresponding records in the database
For example, there is a user entity class and a user table. When new has a user object, but does not open the transaction. At this time, the user is in a transient state, and the data of the database is not any connection, when the transaction is opened, after the execution of the Session.save () method, the session cache is stored in the user object, and the database has the corresponding data, this time it is converted to a persistent state. When the transaction is committed, the session is destroyed. There is no user object in the session cache, and there is a corresponding record in the database table, which is the managed state.

How many ways to create a session in 6.Hibernate? What's the difference?
There are two ways to create them:
The first is: Create a session through Sessionfactory.getcurrentsession (), it is from the current thread to look for, see if there is a session, if any, then return to the session, if not, create a session. belongs to Singleton mode
The second is to create a session through Sessionfactory.opensession (), each time creating a new session.

What are the caches in 7.Hibernate? How are they configured? (three caches to say)
Hibernate with one level cache (session cache), level two cache (sessionfactory cache), query cache (level three cache)
Level 1 Cache: Create session, each time you get data, Hibernate will take precedence from the session, if not, then query. After the query is placed in the cache, when the session is closed, the cache is destroyed. The data in the session cache cannot be shared by another session
Level 2 cache: Sessionfactory level cache, session in Sessionfactory share a level two cache.
The sessionfactory cache is divided into two parts: a built-in cache and an external cache, and the built-in cache is Hibernate's own use and can only be read. The external cache is available to the user, hibernate only provides an interface for the outside cache and requires a third-party provider to implement it. Commonly used have ehcache and so on.
Secondary cache Usage: 1. Import Ehcache jar packages and Ehcache configuration files (default properties for level two cache)
2. Turn on level two cache in Hibernate.cfg.xml (default is OFF)
3. Determine the level two cache provider
4. Configure the Cache object (there are two modes, one read-only, one read-write)
Level 3 Cache (query cache): three-level cache is available because session.creatquery () does not work in the level two cache
Configuration of Level Three cache: 1. Turn on level Two cache, identify the provider, determine the cache object
2. After the query, call Setcacheable (true) to put in the cache
3. When read, call Setcacheable (true) to go to cache to fetch

8. Talk about the difference between get&load
Get: Send SQL to query immediately after invocation, if query a nonexistent value returns NULL.
Load: Lazy loading mechanism, after the call returns a proxy object, only the ID has a value. SQL statements are sent to query when a property other than a non-ID is used. An exception is thrown if a value that does not exist is queried

9. How do I see the SQL that hibernate generates and executes in the console?
Configure the Show_sql property to True in Hibernate.cfg.xml

How many search methods are available in 10.Hibernate?
Hibernate has two ways of retrieving it:
1. Search Now: Query immediately, when executing the query statement, immediately query all the data. Get
2. Deferred retrieval: Deferred query, after executing the query statement, and then querying when needed.

11. Say Save (), flush (), what has been done since the commit () method call?
Save (): Write good SQL statements to the buffer
Flush (): Flash the SQL in the cache to the database and update the data.
Commit (): commits the transaction and implicitly calls Flush () to update the data

How does the relationship between classes be implemented in 12.Hibernate? (e.g. one-to-many, many-to-many relationships)
1-to-many:
1. Entity class: 1 of the party with set set to save many of the party, more than one party with objects to save 1 of the party
2. Configure the set tag in the mapping file for the 1 party, the name attribute = "Property of the party with multiple" in the set label, and the column property in the key tag as the foreign key field. The class attribute of the Onetomany tag is the full class name of the many party.
3. Configure the Manytoone tag in the multiple-party mapping file, and the name attribute in the label is the property name of the one party that holds the. Configure the properties in the column label label name as foreign key
Many-to-many:
1. Save each other with a set collection in the entity class
2. Configure the Set label label in the mapping file name to hold the property name of the other party, the table property in the label is the middle table name
Configure the key Label key label in the column property for the current table
Then configure the Manytomany label. The column attribute in the label is the foreign key class property of the other party, and the full class name of the entity class for the other party
How is 13.Hibernate delayed loading?
Lazy loading mechanism is controlled by the lazy property, and the Lazy property is configured in the set tag in the mapping file, which is usually true by default.
There are three values in the Lazy property, and true is lazy loading false to not load. Extra and lazy (send aggregate function to query when the user only needs to order number)

14. Talk about the role of inverse in hibernate?
Inverse commonly used in a one-to-many, many-to-many mapping files in the set label, the inverse property is set to True, is the maintenance of foreign key right reversal to the other side, in a one-to-many, the default is 1 of the party, in many-to-many, both sides are maintained, do not set the right to reverse transfer
15.MySQL What is the default transaction isolation level? How does jdbc change? How can hibernate be modified?
The default transaction isolation level for MySQL is 4, which is set in JDBC by a constant value in the connection class.
Hibernate is typically 4 by configuring the transaction isolation level in profile Hibernate.cfg.xml
16. What are the characteristics of a transaction?
Transactional characteristics are atomic, isolated, persistent, consistent
17. What is dirty read, non-repeatable read, Phantom read
Dirty read: Is read to another transaction did not commit the data
Non-repeatable reads: A transaction is modified several times while another transaction reads data, and the data is read differently.
18. Talking about the principle of connection pooling
Connection pooling: The connection pool is created in advance connection placed in a pool, the state is idle, when the outside world needs to use the connection, the allocation connection to use, the connection state is used state. The connection pool has its own management mechanism, but when conncetion is not enough, it will be created, but more than the specified number, the destruction. Connetion that work is completed will be recycled and the status changed to idle state
19. What is the project all about HQL and QBC?
HQL and QBC are object-oriented query statements, QBC are more object-oriented than HQL, and the query statements are encapsulated as methods in QBC.
QBC: Manipulate data by invoking different methods for manipulating objects
HQL: You can write SQL statements manually to query.
20. Talk about pessimistic locks and optimistic locks?
Pessimistic lock: By default when using data, someone else modifies the data I use, so I lock the data, the database provides the implementation (locked by the for update)
Optimistic Lock: Default when using data, no one else changes the data I use. Implemented by version number. Add the Version property to the entity class, the table adds the Versions field, and each time the transaction commits, the revision number is checked, and if the version number does not match, the transaction commits failure, the data is retrieved, and the version number is increased after each transaction commit

21. Talk about the origins of Struts2
Struts2 is made up of Struts1 fame plus webwork, due to the early struts1 not meet the demand, webwork low visibility, the combination of the two, forming a Struts2
22. Talk about the Struts2 workflow
Struts2 Workflow: The browser sends the request through a series of filters to reach the STRUTS2 core control Filterdispatcher, Filterdispatcher calls Actionmapper to determine whether the request needs to invoke an action and, if necessary, filterdispatcher the request to the action proxy object. The agent object of the action queries the struts configuration file through Configuration Manager, finds the action class that needs to be called, and the action proxy object creates an Actioninvcation instance to invoke the action. During the call to action, the 18 interceptors that come with struts will produce a result after the action call, and if the result is a JSP, the view template is invoked to produce a JSP. Then in response to the browser.

23. How to build a STRUTS2 project
Build struts2 Project Flow: 1. Import struts2 jar packages and configuration files first
2. On the web. Adding STRUTS2 core controls to XML
3. Create a helloaction and configure the helloaction in the struts configuration file.
4. Access Action for testing

24. What is a value stack? How is it used in the project?
Value stack: A memory area where data is stored and can be used as a request. In the struts2 middle finger is valuestack,valuestack is a Actioncontext object, is the stack structure. Stores the action object.
In the project, declare familiarity with the same name as the parameter in action, and provide the Get/set method. When struts invokes action, it assigns a value to the corresponding property of the action. And this action object will be stored in the
Value stack, we just need to call.
25. Say Struts2 there are several calibration methods
STRUTS2 has 3 Lieutenant colonel's Test method.
The first: Action inherits Actionsupport, overriding the Validate method of the parent class (which verifies all methods in the action)
Second: Override the Validate Method name () method, which verifies only the method name followed by validate (note the first letter of the method name to capitalize)
Third: Framework validation, writing configuration files, naming rules is the action class name-validation.xml, the location and the action to be verified is placed under the same package.
What is the definition of 26.struts2 interceptor? How is it used in the project?
The Struts2 interceptor is similar to the filter, but only works on the action.
In the project, first create an interceptor class that inherits the Abstractintercepted class, overriding the Intercept rescheduling set () method.
In struts. XML configuration file, define an interceptor, with the interceptor tag, the name attribute in the tag is the interceptor name, and the class attribute is the full class name of the Interceptor class. In defining an interceptor stack, the interceptor-stack tag is used to introduce a custom interceptor and the default interceptor stack defalutestack. Finally, the custom interceptor stack is referenced.
27. What is the underlying implementation principle of token?
The token in struts is used to solve the problem of repeated forms of the form, the underlying principle is that when the browser requests access to a page with a token attribute, the server generates a random number, this random number will save a copy in the session, will also send a copy to the page, When the user submits the form, the server will go to the session to find out if there is an equal random, and if so, release. The server clears the random number. When the form repeats a commit, the server cannot find a matching random number in the session and then intercepts it.


28. What do you mean by OGNL expression? struts2?
OGNL: The object navigation diagram language, which can be used instead of Java script in the page to simplify the operation of data access. OGNL expression: Similar to an EL expression
Struts2: Ognl With this Struts tab, the data in the value stack can be taken directly, and the presence of data in the non-value stack will
Using # to get the OGNL expression browser wrapped with%{} is parsed with an OGNL expression.

How can I access Servletapi in 29.action in several ways?
There are three ways of doing this:
First: Access via Actioncontent
The second type: access via Servletactioncontent
The third type: access by implementing the Servlet*aware interface
30. Talk about the STRUTS2 and hibernate integration process
Struts2 and Hibernate integration process:
1. Build the STRUT2 Environment first:
1. Importing Struts2jar packages and configuration files
2. Configuring the Struts2 core control in Web. xml
3. Write a Hello action and configure the action in the STRUTS2 configuration file
4. Whether the test environment is built successfully
2. Build Hibernate environment
1. Importing Hibernatejar packages and configuration files
2. Import Hibernate Tool Class
3. Create entity classes and database-building tables
4. Write the mapping file, naming the rule: entity class name. Hbm.xml, Location: Under the same package as the entity class. To configure the mapping of entity classes to database tables in a mapping file
5. Writing test classes for environmental testing


Hibernate face Test

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.