Discover spring hibernate session, include the articles, news, trends, analysis and practical advice about spring hibernate session on alibabacloud.com
1, SSH three framework integration principle
The integration of spring and Struts2 is to give the Action object to the Spring container for creation.
The integration of spring and Hibernate is to give the sessionfactory to the spring container for maintenance, a
The Get () and load () methods provided by the session interface in Hibernate are used to obtain an entity object, with some differences in usage and query performance. Beta version: Hibernate 4.2.0.GetThe session interface provides 4 overloaded get methods that obtain entity objects through persistent class + primary
Sometimes the heart knows, but may not be able to organize a good language to express, accumulate how to answer questions.1.Hibernate working principle and why use?How it works: 1. Read and parse the configuration file 2. Read and parse the mapping information, create Sessionfactory 3. Open Session 4. Create transaction transation 5. Persist Operation 6. COMMIT TRANSACTION 7. Close
This paper mainly refers to the use of saveorupdate, clear, flush and other methods of the session, as well as the output of the control statement.The Saveorupdate method of 1.SessionFirst look at the description of the Hibernate API documentation:
void
saveOrUpdate(Objectobject)Either Save () or update () the given instance, depending upon the value of its identifier property.
Listen to our teacher today for a lesson on hibernate, for the basic operation of Hibernate has a general understanding, about many-to-many, one-to-one, one-to-many, Many to a very good understanding, used to MyEclipse automatically generated hibernate mapping file, this time to see his own set up an environment, himself from the beginning to write a configuratio
Before we talk about spring transaction management, let's think about how we do data manipulation in hibernate when we're not using spring. In Hibernate we do one operation at a time we have to open the transaction, then the data operation, and then commit the transaction, close the transaction, we do this because
Sesison interface is the most important persistence manager in hibernate. It can perform basic CRUD operations, execution condition queries,Transaction ControlAnd the lifecycle of the Persistent Object.
Single thread, non-shared object. Threads are not secure. When designing the software architecture, avoid multiple threads sharing the same session instance.
Session
be permanently saved (such as a disk). Can be in the form of files, databases, and XML.
Off-State (Detached): When the session associated with a persistent object is closed, the persisted object is turned into a de-tube object. When the off-tube object is re-associated to the session, it is again transformed into a persistent object.
Here I would like to summarize a bit about the instantaneo
The session interface is the most important interface that hibernate provides to the program to manipulate the database, and is a single-threaded object that provides basic methods for saving, updating, deleting, and querying. It has a cache that holds persisted objects and updates the database as they are synchronized when the cache is cleaned up.Note: Some methods of the
Spring and Hibernate integrationSteps1) Introduction of JAR PackageConnection pool/Database driver packageHibernate-related JAR packagesSpring core packages and AOP packagesSpring-ormSpring-tx2) configurationHibernate.cfg.xmlBean.xml3) BuildPackage com.cx.entity;/** * Created by Cxspace on 16-8-11. */public class Dept { private int id; private String name; public int getId () { return ID;
@ Manytoone (fetch = fetchtype. Lazy)
Exception in lazy loading: org. hibernate. lazyinitializationexception: cocould not initialize proxy-No session
I thought it was my configuration problem. I tested it for a long time and found the problem:
When spring security is used, the server is started to inject basedao queries. Because the request query is not passed
The Web project will be session-held through sessions, which are stored in server memory;
Now in order to improve the performance and stability of the site, the Web project published to multiple servers, through proxy such as Nginx or F5 do load balancing;
Due to the normal configuration of load balancing, the client requests are randomly forwarded to a server, which causes the session to be lost;
the database to the cache!Note: Because the transaction mechanism of the database, in the same transaction, will ensure the atomicity of the operation, so in addition to the data, the Refresh () method is not refreshed, not this method has a problem, but the database isolation mechanism, Oracle has two isolation mechanisms, MySQL has four isolation mechanisms, As long as MySQL is set to repeatable read, you can look up the latest data!Three: Clear ()This method will erase all the current caches
Previously, SessionFactory sf = conf. buildSessionFactory () was recommended by the IDE not to continue using it. Now, the best way for hibernate to get the session is to use: The code is as follows:Copy code Package org. Rudiment. hibernate;Import org. Rudiment. hibernate. News;Import org.
Spring provides great support for Hibernate integration. Spring provides initialization for sessionfactory, So you no longer need to worry about session open and close. Spring also provides flexible transaction declarations.
This article describes how to integrate
Struts: Responsible for the display layer, is a perfect MVC implementation, it calls the business layer to complete the operational logic, the business layer calls the persistence layer to complete the database read and write.Hibernate: Creates a series of persisted classes that correspond to the properties of each class to the database table's property one by one.Spring: relies on injection and control inversion. As soon as you get a class that implements this interface, you can easily inject t
exception is thrown.-REQUIRES_NEW: always creates new transactions.-NOT_SUPPORTED: if a transaction exists, the transaction is suspended and non-transaction operations are continuously performed.-NEVER always executes non-transactions. If the current transaction exists, an exception is thrown.-NESTED embedded transactions
9. Differences between update () and saveOrUpdate () in Hibernate.From hibernate instructions:SaveOrUpdate () Does the following:I
The main function of the session is to provide the creation, read, and delete operations of the mapped entity class instance. The instance may exist in the following three states:Temporary state (transient): Never persisted, not associated with any sessionPersistent State (persistent): associated with only one sessionOff-State (detached): persisted, but not currently associated with any session1, the instance obtained by the Get () or load () method i
instantiate the private date date1;// Using the Date object directly, you need to instantiate the @Test public void Testico () { SYSTEM.OUT.PRINTLN (date); The output System.out.println (date1) of the Bean injection object; Date1=new Date (); System.out.println (date1); System.out.println (date1); }}The output results are as follows:Sat Jul 16:38:26 CST 2016NullSat Jul 16:38:26 CST 2016Sat Jul 16:38:26 CST 2016You see the results, we must under
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.