In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to explain the working principle of Hibernate examples, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
You may have a general understanding of Hibernate, but it is not necessarily clear what the principle of Hibernate is. This article mainly illustrates the principle of Hibernate through an example. I hope it will be helpful to everyone's study.
We know that if we connect to the database with java, the first thing we think of is JDBC, then what is the principle of Hibernate? hibernate can be understood as a middleware that is responsible for receiving and sending the sql statements of the java program to the database, while the information returned from the database hibernate receives and directly generates an object and transmits it to java.
Before we talk about the principle of Hibernate, let's talk about the files of Hibernate.
Suppose a student student table:
Sql statement:
Create table student (id Number (10), name varchar2 (20))
What happens next? We need to have two files specific to hibernate. One is a file that ends with .cfg.xml. One is a file that ends in .hbm.xml. What are these two documents for?
The function of .cfg.xml is to connect to the database, and inside the file is actually the basic information of a connection library made up of user,password,url,driver.
The content of the file is as follows:
PUBLIC "- / Hibernate/Hibernate Configuration DTD 3.0//EN"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 111property > jdbc:oracle:thin:@127.0.0.1:1521:risproperty > org.hibernate.dialect.Oracle9Dialectproperty > 111property > oracle.jdbc.driver.OracleDriverproperty > session-factory > hibernate-configuration >
The full name of this file should be "your application name .cfg.xml". The name of the project created by the current example is one.cfg.xml.
A brief analysis of this file:
Contains information about the instance of configuration in the program. Through the method configure of this example, we can get the corresponding table information and class information from mapping.
This tag is a SessionFactory object we get in the program through the configure method BuildSessionFactory, which can be understood as a statement, and all our operations on the database are implemented through a series of its methods.
The property inside is something you need to connect. Where dialect is the dialect attribute value of hibernate. The dialect value dialect varies from database to database, so here is a list of how to set the dialect value in different databases (see table below):
RDBMS
Dialect
DB2
Org.hibernate.dialect.DB2Dialect
DB2 AS/400
Org.hibernate.dialect.DB2400Dialect
DB2 OS390
Org.hibernate.dialect.DB2390Dialect
PostgreSQL
Org.hibernate.dialect.PostgreSQLDialect
MySQL
Org.hibernate.dialect.MySQLDialect
MySQL with InnoDB
Org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM
Org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version)
Org.hibernate.dialect.OracleDialect
Oracle 9i/10g
Org.hibernate.dialect.Oracle9Dialect
Sybase
Org.hibernate.dialect.SybaseDialect
Sybase Anywhere
Org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server
Org.hibernate.dialect.SQLServerDialect
SAP DB
Org.hibernate.dialect.SAPDBDialect
Informix
Org.hibernate.dialect.InformixDialect
HypersonicSQL
Org.hibernate.dialect.HSQLDialect
Ingres
Org.hibernate.dialect.IngresDialect
Progress
Org.hibernate.dialect.ProgressDialect
Mckoi SQL
Org.hibernate.dialect.MckoiDialect
Interbase
Org.hibernate.dialect.InterbaseDialect
Pointbase
Org.hibernate.dialect.PointbaseDialect
FrontBase
Org.hibernate.dialect.FrontbaseDialect
Firebird
Org.hibernate.dialect.FirebirdDialect
Stay with me and keep talking about the Student.hbm.xml file. This file is a mapping file for the tables of the database, and we use this file to indicate which class corresponds to which table, and also indicates which fields in which table correspond to the properties in which class.
The content of the file is as follows:
PUBLIC "/ / Hibernate/Hibernate Mapping DTD 3.0//EN"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> id > class > hibernate-mapping >
At this point, the file is over. In particular, let's talk about the problem of id. We saw that there was one in the file, what is this? This is to achieve automatic id increase, that is, if we insert a name into the database, then id will automatically add 1.
This file explains that what this contains is the mapping we mentioned in the previous file. The mapping we get from configure in the java class is read from this file. If a class carries a package, it must have a package name (it is recommended that all written classes carry a package). Pay attention to the package name and class name. The package name is lowercase and the first uppercase of the class name. I am afraid of making mistakes. I would like to remind you that this is to specify the corresponding table of the class first. Then those inside specify the correspondence between the fields in the table and the properties in the class.
At this point, these two special documents are over. Let's start with our java class. There are two main ones: one is the Pojo class and the other is our Test class.
The Pojo class is actually a simple javaBean. (Plain Old Java Objects, simple clean Java object). Look at the following code:
Package src; public class Student {private int id; private String name; public void setId (int id) {this.id=id;} public void setName (String name) {this.name=name;} public int getId () {return id;} public String getName () {return name;}}
It's such a simple class. It corresponds to the field of the database and then takes the value.
Here are our key Test classes:
Package src; import org.hibernate.*; import org.hibernate.cfg.*; public class Test {public static void main (String bb []) {try {SessionFactory sf=new Configuration (). Configure (). BuildSessionFactory (); Session s=sf.opension (); Transaction ts=s.beginTransaction (); for (int item0)
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.