hibernate configuration file and mapping file

Hibernate configuration file

Introduction to Hibernate Framework

The Hibernate framework is an open-source object-relational mapping framework, which encapsulates JDBC very lightweight objects, allowing Java programmers to manipulate the database with object programming thinking as they wish.

1. Hibernate configuration file

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 数据库URL -->
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:orcl
		</property>
		<!-- 数据库用户 -->
		<property name="connection.username">scott</property>
		<!-- 数据库用户密码 -->
		<property name="connection.password">orcl</property>
		<!-- 数据库JDBC驱动 -->
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
		</property>
		<!-- 指定当前session范围和上下文 -->
		<property name="current_session_context_class">thread</property>
		<!-- oracle版本方言 -->
		<property name="dialect">
			org.hibernate.dialect.Oracle10gDialect
		</property>
		<!-- 是否将运行期生成的SQL输出到日志已供调试 -->
		<property name="show_sql">true</property>
		<!-- 是否格式化 -->
		<property name="format_sql">true</property>
		<!-- 映射文件 -->
		<mapping resource="cn/bdqn/entity/Dept.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

2. Hibernate configuration file parameter function

(1) connection.url: represents the database URL. Jdbc:oracle:thin:@10.0.0.176:1521:orcl. is the URL of the Oracle database, where jdbc.oracl.thin:@ is a fixed wording, 10.0.0.176 is the IP address, 1521 is the port number, and orcl is the database instance name .
(2) Connection.username: Represents the database user name.
(3) Connection.password: indicates the database user password.
(4) Connection.driver_class: indicates the database driver. Oracle: jdbc.driver.OracleDriver is the driver class of Oracle database.
(5) Dialect: used to configure the data type used by Hibernate. Hibernate supports almost all subject databases, including Oracle, DB2, MS SQL Server and MySQL. Org.hibernate.dialect.Oracle10gDialect specifies that the current database type is Oracle 10g and above.
(6) Curr_sql: If set to true, the SQL statement will be output on the console when the program is running.
(7) Format_sql: If set to true, the formatted SQL statement will be output on the console when the program is running.

3. Hibernate mapping file

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Hibernate映射文件 -->
<hibernate-mapping>
	<class name="cn.bdqn.entity.Dept" table="`DEPT`">
		<id name="deptNo" column="`DEPTNO`" type="java.lang.Byte">
			<generator class="assigned"></generator>
		</id>
		<property name="deptName" column="`DNAME`" type="java.lang.String"></property>
		<property name="location" column="`LOC`" type="java.lang.String"></property>
	</class>
</hibernate-mapping>

4. Hibernate mapping file parameters

Dept.hb.xml defines the mapping of Dept class to database table DEPT. The meaning of each element is as follows.
class: Represents the database table name of the persistent class.
table: Represents the fully qualified name of the persistent class.
id: Represents the mapping between the OID of the persistent class and the primary key of the table. Common attributes are as follows:
type: indicates the name of the persistent class attribute, which matches the attribute's accessor.
column: indicates the name of the database table field corresponding to the persistent class attribute, and can also be specified in the sub-element column.
The child element of the generator:id element is used to specify the generation strategy of the primary key. Common attributes and elements are as follows:
class: attributes are used to specify a specific primary key generation strategy.
param: The element is used to pass parameters.

The commonly used primary key generation strategies are as follows:
(1). assigned: The primary key is generated by the application without Hibernate participation. This is the default generation strategy when no element is specified.
(2). increment: For the primary key of type long, short, or int, the value of the primary key has been automatically increased. The primary key increases in numerical order, and the increment is 1.
(3).identity: For databases that support identity columns such as SQL Server, DB2, MySQL, etc., this primary key generation strategy can be used to generate an automatic growth primary key, but the corresponding The primary key field is set to the identity column.
(4).sequence: For databases such as Oracle and DB2 that support sequences, this primary key generation strategy can be used to generate an automatic growth primary key, and the name of the sequence in the database can be passed into the sub-element param.
(5). Native: Hibernate judges which primary key generation strategy to use according to the underlying database, that is, the primary key value is generated by the database used.
propert: defines the correspondence between the attributes in the persistent class and the fields in the database table. Common attributes are as follows:
name: indicates the name of the persistent class attribute, which matches the attribute accessor.
type: Represents the type of persistent class attribute.
column: indicates the name of the database table field corresponding to the persistent class attribute, and can also be specified in the sub-element column.
column element: used to specify the field in the database corresponding to the persistent class attribute represented by its parent element. Its common attributes are as follows:
name: indicates the name of the field;
length: indicates the length of the field;
not-null: sets whether it cannot be null, set to true means it cannot be null;

5. Advantages of Hibernate

(1) Object/Relational Database Mapping (ORM) It only needs to manipulate objects when it is used, making development more object-oriented, abandoning database-centric thinking, and completely object-oriented thinking
(2) transparent persistence (persistent) with persistence Stateful, single-threaded object with business functions, this object has a short lifetime. These objects may be ordinary JavaBeans/POJOs. This object does not implement a third-party framework or interface. The only special thing is that they are associated with (only one) Session. Once the Session is closed, these objects will leave the persistent state, so that they can be used freely by any layer of the application.
(3) Transaction (org.hibernate.Transaction) The application is used to specify objects within the scope of the atomic operation unit. It is single-threaded and has a short life cycle. It abstracts the application from the underlying specific JDBC, JTA and CORBA transactions. In some cases, a Session may contain multiple Transaction objects. Although whether to use this object is optional, whether it is using the underlying API or the Transaction object, the opening and closing of the transaction boundary is essential. (4) It is not intrusive, that is, the so-called lightweight framework (5) Portability will be very good (6) Cache mechanism, providing first-level cache and second-level cache (7) Concise HQL programming

6. Disadvantages of Hibernate

(1) Not suitable for applications that use a large number of stored procedures in the center
(2) Large-scale batch insertion, modification and deletion of inappropriate Hibernate.

Guess you like

Origin blog.csdn.net/qq_44829957/article/details/109531038