Table of Contents
1.持久化对象POJO编写规则:
2.POJO的OID:
3.数据类型:
4.持久化类配置POJO.hbm.xml:
1)主键生成策略increment:顶层递增:
2)主键生成策略identity:底层递增:
3)主键生成策略sequence:序列:
4)主键生成策略native:本地:
5)主键生成策略uuid:标识符:
6)主键生成策略assigned:手动指定:
7)主键生成策略:复合主键:
Home Database Mysql Tutorial IT忍者神龟之Hibernat持久化对象-数据表映射配置回顾

IT忍者神龟之Hibernat持久化对象-数据表映射配置回顾

Jun 07, 2016 pm 04:09 PM
object ninja Endurance data sheet mapping match

1.持久化对象POJO编写规则: 1) 有空参public构造器; 2) 提供标识属性,映射数据表主键; 3) 属性提供setter和getter方法; 4) 属性使用基本数据类型的包装类型。基本类型在数据库中不能区分null和0; 5) 不使用final修饰。如果使用final则无法生成代

1.持久化对象POJO编写规则:

1) 有空参public构造器;

2) 提供标识属性,映射数据表主键;

3) 属性提供setter和getter方法;

4) 属性使用基本数据类型的包装类型。基本类型在数据库中不能区分null和0;

5) 不使用final修饰。如果使用final则无法生成代理对象;当使用了final,load查询将如同get查询。

2.POJO的OID:

OID是持久化类与数据表主键对应的属性,用来唯一区分持久化对象。

自然主键:采用数据库中有意义的列的值作为主键(有意义)

代理主键:采用自动生成的流水号、UUID作为主键(无意义,推荐)

3.数据类型:

基本类型无法区分null和0,开发中POJO属性都使用包装类型。

4.持久化类配置POJO.hbm.xml:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <code class="xml keyword">xml <code class="xml color1"> version<code class="xml plain">=<code class="xml string">"1.0" <code class="xml color1"> encoding<code class="xml plain">=<code class="xml string">"UTF-8"<code class="xml plain">?> <code class="xml plain">DOCTYPE <code class="xml plain"> hibernate-mapping PUBLIC <code class="xml spaces"> <code class="xml plain">"-//Hibernate/Hibernate Mapping DTD 3.0//EN" <code class="xml spaces"> <code class="xml plain">"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <code class="xml comments"><!-- package="POJO所在包" --> <code class="xml plain">hibernate-mapping <code class="xml color1"> package<code class="xml plain">=<code class="xml string">"cn.cvu.domain"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO类" table="数据库中的表单" catalog="数据库,对应项目的hibernate核心配置文件中<property name=&#39;hibernate.connection.url&#39;>jdbc:mysql:///数据库</property> " --> <code class="xml spaces"> <code class="xml plain">class <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"User" <code class="xml color1"> table<code class="xml plain">=<code class="xml string">"table_user" <code class="xml color1"> catalog<code class="xml plain">=<code class="xml string">"db_hibernate"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO对应的属性" column="表单的主键 列" type="POJO属性的数据类型" --> <code class="xml spaces"> <code class="xml plain">id <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"id" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"id" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"int"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- class="主键生成策略" --> <code class="xml spaces"> <code class="xml plain">generator <code class="xml color1"> class<code class="xml plain">=<code class="xml string">"native"<code class="xml plain">><code class="xml keyword">generator<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 常用策略:increment、identity、sequence、native、uuid、assigned --> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- name="POJO中对应属性" colunm="表单中的 列" type="POJO数据类型" --> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"name" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"name" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"age" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"age" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"int" <code class="xml plain"> ><code class="xml keyword">property<code class="xml plain">> <code class="xml plain">   <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"city" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"city" <code class="xml color1"> sql-type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 常用属性:length:列值长度、not-null:非空true/false、unique:唯一true/false --> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">class<code class="xml plain">> <code class="xml plain"><code class="xml keyword">hibernate-mapping<code class="xml plain">>

1)主键生成策略increment:顶层递增:

\

由hibernate自动完成,原理:先查询最大值,再插入此值加一。OID必须为long、int或short类型。

优点:跨数据库。

缺点:多线程并发访问问题。

2)主键生成策略identity:底层递增:

\

由数据库自动完成,要求数据库必须支持自增主键。mysql支持,oracle不支持。OID必须为long、int或short类型。

优点:无并发访问问题。

3)主键生成策略sequence:序列:

由数据库自动完成递增,要求数据库必须支持序列。mysql不支持,oracle支持。OID必须为long、int或short类型。

Oracle:

1创建序列:create sequence myseq;

2调用序列:insert into customer values (myseq.nextval); #序列加一

4)主键生成策略native:本地:

\

采用数据库支持的自增策略。 mysql:identity,oracle:sequence。OID必须为long、int或short类型。

优点:跨数据库平台。

5)主键生成策略uuid:标识符:

\

由数据库自动创建。 uuid是32位唯一字符串,表单主键使用varchar类型,POJO对应属性是String类型。

6)主键生成策略assigned:手动指定:

\

在调用hibernate时手动指定主键的值,用于自然主键(有意义的)。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 public <code class="java keyword"> void <code class="java plain"> testInsertAssigned() { <code class="java spaces"> <code class="java plain">Session session = UtilGetSesstion.openSession(); <code class="java spaces"> <code class="java plain">Transaction transaction = session.beginTransaction(); <code class="java spaces"> <code class="java spaces"> <code class="java plain">User user = <code class="java keyword">new <code class="java plain"> User(); <code class="java spaces"> <code class="java plain">user.setId(<code class="java value">20130124<code class="java plain">); <code class="java comments">//手动设置主键 <code class="java spaces"> <code class="java plain">user.setName(<code class="java string">"EminemXXX"<code class="java plain">); <code class="java spaces"> <code class="java plain">user.setAge(<code class="java value">42<code class="java plain">); <code class="java spaces"> <code class="java plain">user.setCity(<code class="java string">"NewYorkXXX"<code class="java plain">); <code class="java spaces"> <code class="java plain">session.save(user); <code class="java spaces"> <code class="java spaces"> <code class="java plain">transaction.commit(); <code class="java spaces"> <code class="java plain">session.close(); <code class="java spaces"> <code class="java plain">}

7)主键生成策略:复合主键:

(1)编写POJO类 Person.java:

1 2 3 4 5 6 7 8 9 10 11 package <code class="java plain"> cn.cvu.domain; <code class="java keyword">import <code class="java plain"> java.io.Serializable; <code class="java comments">//务必事先序列化接口 <code class="java keyword">public <code class="java keyword"> class <code class="java plain"> Person <code class="java keyword">implements <code class="java plain"> Serializable { <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String firstName; <code class="java comments">//对应表单的复合主键 <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String secondName; <code class="java comments">//对应表单的复合主键 <code class="java spaces"> <code class="java keyword">private <code class="java plain"> String address; <code class="java spaces"> <code class="java spaces"> <code class="java comments">//get/set <code class="java spaces"> <code class="java comments">//toString <code class="java plain">}

(2)配置POJO.hbm.xml:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <code class="xml keyword">xml <code class="xml color1"> version<code class="xml plain">=<code class="xml string">"1.0" <code class="xml color1"> encoding<code class="xml plain">=<code class="xml string">"UTF-8"<code class="xml plain">?> <code class="xml plain">DOCTYPE <code class="xml plain"> hibernate-mapping PUBLIC <code class="xml spaces"> <code class="xml plain">"-//Hibernate/Hibernate Mapping DTD 3.0//EN" <code class="xml spaces"> <code class="xml plain">"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <code class="xml plain">hibernate-mapping <code class="xml color1"> package<code class="xml plain">=<code class="xml string">"cn.cvu.domain"<code class="xml plain">> <code class="xml spaces"> <code class="xml plain">class <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"Person" <code class="xml color1"> table<code class="xml plain">=<code class="xml string">"table_person" <code class="xml color1"> catalog<code class="xml plain">=<code class="xml string">"db_hibernate"<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 复合id --> <code class="xml spaces"> <code class="xml plain">composite-id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 主键列 name="POJO属性" column="表单列名" --> <code class="xml spaces"> <code class="xml plain">key-property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"firstName" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"nameFirst"<code class="xml plain">><code class="xml keyword">key-property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain">key-property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"secondName" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"nameFirst"<code class="xml plain">><code class="xml keyword">key-property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">composite-id<code class="xml plain">> <code class="xml spaces"> <code class="xml comments"><!-- 普通列 name="POJO属性" column="表单列名" type="数据类型" --> <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"address" <code class="xml color1"> column<code class="xml plain">=<code class="xml string">"addr" <code class="xml color1"> type<code class="xml plain">=<code class="xml string">"string"<code class="xml plain">><code class="xml keyword">property<code class="xml plain">> <code class="xml spaces"> <code class="xml plain"><code class="xml keyword">class<code class="xml plain">> <code class="xml plain"><code class="xml keyword">hibernate-mapping<code class="xml plain">>

(3)配置hibernate.cfg.xml,加载Person.hbm.xml文件:

\

(4)操作类的方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public <code class="java keyword"> void <code class="java plain"> testInsert() { <code class="java spaces"> <code class="java plain">Configuration configuration = <code class="java keyword">new <code class="java plain"> Configuration().configure(); <code class="java spaces"> <code class="java plain">SessionFactory sessionFactory = configuration.buildSessionFactory(); <code class="java spaces"> <code class="java plain">Session session = sessionFactory.openSession(); <code class="java spaces"> <code class="java plain">Transaction transaction = session.beginTransaction(); <code class="java spaces"> <code class="java spaces"> <code class="java plain">Person person = <code class="java keyword">new <code class="java plain"> Person(); <code class="java spaces"> <code class="java plain">person.setFirstName(<code class="java string">"C"<code class="java plain">); <code class="java spaces"> <code class="java plain">person.setSecondName(<code class="java string">"Vigiles"<code class="java plain">); <code class="java spaces"> <code class="java plain">person.setAddress(<code class="java string">"Beijng"<code class="java plain">); <code class="java spaces"> <code class="java plain">session.save(person); <code class="java spaces"> <code class="java spaces"> <code class="java plain">transaction.commit(); <code class="java spaces"> <code class="java plain">session.close(); <code class="java spaces"> <code class="java plain">sessionFactory.close(); <code class="java spaces"> <code class="java plain">}

(5)结果:

\

INFO: HHH000262: Table not found: tb_person
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: create table db_hibernate.tb_person (namef varchar(255) not null, names varchar(255) not null, age integer, city varchar(255), primary key (namef, names)) type=InnoDB
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 7
2013-11-5 10:07:12 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
\

1 2 3 <!-- 方言-指定数据库为5.1版本之后的SQL语言 --> <code class="xml spaces"> <code class="xml plain">property <code class="xml color1"> name<code class="xml plain">=<code class="xml string">"hibernate.dialect"<code class="xml plain">> <code class="xml plain">  org.hibernate.dialect.MySQL5InnoDBDialect<code class="xml keyword">property<code class="xml plain">>

-end
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

'Naruto: Ninja Next Generation' overcomes the weak self and blooms a white flower - Happy birthday Hinata Hinata! 'Naruto: Ninja Next Generation' overcomes the weak self and blooms a white flower - Happy birthday Hinata Hinata! Dec 29, 2023 pm 10:48 PM

Whether in the world of ninjas or in "Naruto: Ninja New Generations", she is one of the descendants of Hamura Otsutsuki. She is a ninja of the Country of Fire, Konoha Ninja Village, and the clan of the wealthy Hyuga clan of Konoha. The eldest daughter of Hinata Hinata, she has the "White Eyes" that are the limit of blood inheritance. She is a white flower with a somewhat weak personality, but still blooms bravely - Hinata Hinata! Hinata was originally the heir who had high hopes for her family, but because of herself He is not good at using the "soft fist" passed down by the Hinata family, and lost to his sister Hinata Hanabi, who is five years younger than him, in a competition, which made Hinata's father Hinata Hinata very disappointed with him. Hinata then ran out of the house sadly and was bullied by three boys. At this time, Uzumaki Naruto appeared and rescued Hinata, but Naruto was beaten violently by three boys. subsequently

What is the best graphics card for i7 3770? What is the best graphics card for i7 3770? Dec 29, 2023 am 09:12 AM

What graphics card is good for Core i73770? RTX3070 is a very powerful graphics card with excellent performance and advanced technology. Whether you're playing games, rendering graphics, or performing machine learning, the RTX3070 can handle it with ease. It uses NVIDIA's Ampere architecture, has 5888 CUDA cores and 8GB of GDDR6 memory, which can provide a smooth gaming experience and high-quality graphics effects. RTX3070 also supports ray tracing technology, which can present realistic light and shadow effects. All in all, the RTX3070 is a powerful and advanced graphics card suitable for those who pursue high performance and high quality. RTX3070 is an NVIDIA series graphics card. Powered by 2nd generation NVID

How to implement data caching and persistence in Vue How to implement data caching and persistence in Vue Oct 15, 2023 pm 01:06 PM

How to implement data caching and persistence in Vue In Vue, data caching and persistence is a common requirement. Caching data can improve application performance, while persistent data allows users to still see previously saved data after refreshing the page or reopening the application. The following will introduce how to cache and persist data through some common methods. Using Vuex to implement data caching Vuex is Vue's official state management library, which can be used to centrally manage the state of all components of the application. We can leverage Vuex

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

i73770 with rx5600xt (i73770 with rx5600xt) i73770 with rx5600xt (i73770 with rx5600xt) Jan 04, 2024 am 11:26 AM

i73770 with rx5600xt Because the RX5600XT graphics card is matched with the R53600CPU, we chose the i7-3770. The evaluation results of the RX5600XT graphics card are as follows: The RX5600XT graphics card is an excellent graphics card and performed very well after testing. It adopts AMD's RDNA architecture, has 6GBGDDR6 video memory and 192-bit memory interface, supports PCIe4.0 bus, and has excellent gaming performance. In all tests, the RX5600XT graphics card performed well. At high resolutions, it delivers a smooth gaming experience and maintains frame rates above 60 FPS in most games. In the latest games, it can also provide good

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Selected Java JPA interview questions: Test your mastery of the persistence framework Selected Java JPA interview questions: Test your mastery of the persistence framework Feb 19, 2024 pm 09:12 PM

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

See all articles