Operate MySQL view using hibernate

Source: Internet
Author: User
Tags mysql view

Hibernate operates databases as objects and complies with the OOP programming mode. This operation greatly simplifies the process of interaction between programmers and databases, reduces the amount of database code (you do not need to spell SQL statements yourself), and ing between entities and databases, A programmer can perform addition, deletion, modification, and query of data just like an operation object and its attributes. This is very convenient.

Although it is simple and efficient to operate databases through hibernate, this method is not so flexible in the face of some special requirements, such as querying the required information from multiple tables. If Hibernate is used to query results from multiple tables and cartesian products of multiple tables, for example, from Table A (five fields) and Table B (three fields, the result is multiple records containing all the fields (8 fields) in table A and Table B, which is equivalent to generating a new object, in this way, a new object corresponds to it before the query results can be operated. Of course, we can use the new entity method in hql to obtain the query result, however, if the query result record contains other entities (that is, the result record has a foreign key relationship with other tables), it will be more complex.

To solve the above problems, I adopted the hibernate operation view.

A view is a virtual table whose content is defined by the query. Like a real table, a view contains a series of columns and row data with names. However, a view does not exist in the database as a stored data value set. Rows and columns are used to define tables referenced by View queries and dynamically generate tables when views are referenced.

For the referenced basic table, the view function is similar to filtering. The filtering of the definition view can be from one or more tables of the current or other databases, or other views. There are no restrictions on query through views, and there are few restrictions on data modification through them.

A view is an SQL statement stored in a database. It is mainly for two reasons: security reasons. A view can hide some data, such as the Social Insurance Fund table, you can use the view to display only the name and address, but not the social insurance number and wage number. Another reason is that complex queries are easy to understand and use.

View: displays images or documents.

A view is a table exported from one or more tables or views. Its structure and data are based on table queries. Like a table, a view also contains several defined data columns and multiple data rows. However, in essence, these data columns and data rows come from the referenced table.

Therefore, a view is not a basic table that actually exists, but a virtual table. The data of the view is not actually stored in the database in the view structure, but in the table referenced by the view.

Once defined, the view is stored in the database. The corresponding data is not stored in the database as the table. The data displayed in the view is only stored in the basic table. Operations on a view are the same as those on a table. You can query, modify (with certain restrictions), and delete a view.

When you modify the data seen through the view, the data of the corresponding basic table also needs to change. At the same time, if the data of the basic table changes, this change can also be automatically reflected in the view.

View has many advantages, mainly manifested in:
• Centralized Viewpoint
• Simplified operations
• Custom Data
• Merge and split data
• Security
Of course, the view also has some disadvantages. The biggest drawback is the update burden of the view. For example, if the source data is changed, the view must be updated accordingly, and the data in the view must be synchronized when the source data is changed, this is similar to the MySQL cache.

The operation view is basically the same as the actual table in the operation. However, the entity file generated by the view ing regards all fields in the view as the view ID, that is, a primary key is generated, in this case, a new problem occurs. If a field in the query result is null, the entire record is null, which is why all the query results objects are null in many cases.

We can solve this problem by modifying the configuration file and using a unique flag as the primary key of the view. The Code is as follows.

My roleinfo table does not have a primary key. hibernate generates the following ing:
<Hibernate-mapping>
<Class name = "model. roleinfo" table = "roleinfo" schema = "'file'">
<Composite-ID name = "ID" class = "model. roleinfoid">
<Key-property name = "ID" type = "Java. Lang. String">
<Columnname = "ID" length = "32"/>
</Key-property>
<Key-propertyname = "purview" type = "Java. Lang. String">
<Columnname = "purview" length = "10"/>
</Key-property>
<Key-propertyname = "role" type = "Java. Lang. String">
<Columnname = "role" length = "10"/>
</Key-property>
<Key-propertyname = "remark" type = "Java. Lang. String">
<Columnname = "remark"/>
</Key-property>
<Key-propertyname = "fileid" type = "Java. Lang. String">
<Columnname = "fileid" length = "20"/>
</Key-property>
<Key-propertyname = "islock" type = "Java. Lang. String">
<Columnname = "islock" length = "10"/>
</Key-property>
</Composite-ID>
</Class>
</Hibernate-mapping>

Modify the composite primary key in the above ing file to a unique primary key:

<Hibernate-mapping>
<Class name = "model. roleinfo" table = "roleinfo" schema = "'file'">
<Composite-ID name = "ID" class = "model. roleinfoid">
<Key-property name = "ID" type = "Java. Lang. String">
<Columnname = "ID" length = "32"/>
</Key-property>
</Composite-ID>
<Property name = "purview" type = "Java. Lang. String">
<Column name = "purview" length = "10"/>
</Property>
<Property name = "role" type = "Java. Lang. String">
<Column name = "role" length = "10"/>
</Property>
<Property name = "remark" type = "Java. Lang. String">
<Column name = "remark"/>
</Property>
<Property name = "fileid" type = "Java. Lang. String">
<Column name = "fileid" length = "20"/>
</Property>
<Property name = "islock" type = "Java. Lang. String">
<Column name = "islock" length = "10"/>
</Property>
</Class>
</Hibernate-mapping>
Meanwhile, the get and set methods are added to the roleinfo object class (roleinfo. Java.

In this way, you can use the view to find the desired result.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.