In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Download the full set of files: hibernate-release-4.1.7.Final.zip
Directory structure
Under the Documentation folder are the development help documents
The Lib folder is the library used by the developer, and the required directory under this folder is the JAR file that must be imported at the time of development.
The Project folder is the project source file
Comparison of JDBC and Hibernate:
Initial configuration deployment of Hibernate:
1. Create a web project
two。 Add the jar package of Hibernate required for the response
3. Create a Java Bean business entity class
4. Create a configuration file for entity class mapping: * * .hbm.xml
5. Create a configuration file hibernate.cfg.xml for Hibernate
6. Create a test class to test whether the hibernate is configured successfully
Entity class: UserInfo.java
Public class UserInfo
{
Private int userid
Private String userName
Private String userPwd
Private Date birthday
/ / various get and set methods for attributes have been omitted
/ / create a constructor with id
Public UserInfo (int id)
{
This.userid=id
}
Public UserInfo (int userid, String userName, String userPwd, Date birthday)
{
Super ()
This.userid = userid
This.userName = userName
This.userPwd = userPwd
This.birthday = birthday
}
/ / A constructor without parameters is required.
Public UserInfo ()
{
}
}
Configuration file UserInfo.hbm.xml for entity class
(try to keep the configuration file in the same package as the entity class name for ease of management and entity class name.)
/ / the package package specifies the package of the entity class
/ / name specifies the entity class name table as the table name corresponding to the entity class in the database. If not specified, it is consistent with the class name.
/ / id identifies the field class=native identification of the primary key since the growth
/ / property specifies the field of the attribute. Column can specify the name of the corresponding field in the database. If not, it is consistent with the attribute name of the entity class.
Configuration file hibernate.cfg.xml for Hibernate
True
True
Org.hibernate.dialect.Oracle10gDialect
Oracle.jdbc.driver.OracleDriver
Jdbc:oracle:thin:@192.168.3.252:1521:orcl
Bam1
Bam1
Update
Test class Test.java
Add a piece of data:
Public static void add ()
{
/ / step 1: read the configuration file hibernamte.cfg.xml file of Hibernate
Configurationcon=new Configuration () .configure ()
/ / step 2: create a service registration builder object and load all configuration information through the configuration object
ServiceRegistryBuilderregbulider=newServiceRegistryBuilder () .applySettings (con.getProperties ()
/ / create a registration service
ServiceRegistryreg=regbulider.buildServiceRegistry ()
/ / step 3: create a session factory
SessionFactorysessionFactory=con.buildSessionFactory (reg)
/ / step 4: create a data manipulation session
Sessionsession=sessionFactory.openSession ()
/ / create an object
, /
UserInfohanhan=new UserInfo ()
Hanhan.setUserName ("hanhan313")
Hanhan.setUserPwd ("123")
Hanhan.setBirthday (new Date ())
/ *
/ / create things
Transactiontran=session.beginTransaction ()
/ / Save data
Session.save (hanhan)
/ / submit things
Tran.commit ()
/ / close the conversation
Session.close ()
}
Query a piece of data:
After the session reply is created
/ / query the user information of userinfo with id 10
UserInfouser=session.get (UserInfo.class,10)
Update a piece of data:
First, query the data.
UserInfo user=session.get (UserInfo.class,10)
After modifying the data of user
And then get the session reply.
Session.update (user)
Delete a piece of data:
When deleting data, wrap the id data that needs to be deleted into objects.
For example:
UserInfouser=new UserInfo ()
User.setUserId (10)
Session.delete (user); / / when deleted, the id in user will be automatically obtained for deletion.
The process of hibernate:
Principles that must be followed in persisting JAVA classes
1. Declare the access method (get/set) for the persistent field of the class, and Hibernate persists the JavaBeans-style properties.
2. Implement a default constructor so that Hibernate can instantiate the object using the Constructor.newInstance () method.
3. If it is a property of a collection type, its type must be defined as the interface of the collection, for example: List, Set
4. Provide an identification attribute (Identifier property), without which some functions will not work, such as cascading updates, session.saveOrUpdate ().
Status of the object:
Instantaneous (transient): there is no data corresponding to it in the database. If it exceeds the scope, it will be reclaimed by the JVM garbage collector. It is usually an object that comes out of new and is not associated with session.
Persistent: there is data in the database corresponding to it, which is currently associated with session, and the associated session is not closed and the transaction is not committed; the state of the persistent object changes, which will affect the database when the transaction commits (hibernate can detect it).
Detached: there is data in the database that corresponds to it, but there is currently no session associated with it; the state of the managed object changes and cannot be detected by hibernate.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.