Data Persistence Layer
1. domain objects and ing files
You can use tools such as Hibernate middlegen, Hibernate tools, and hibernate syhchronizer or manually compile hibernate domain objects and ing files. The domain object tfile. Java corresponding to the t_file table is:
Code 1 domain object tfile
1. Package sshfile. model; 2. Public class tfile 3 .{ 4. Private string fileid; 5. Private string filename; 6. Private byte [] filecontent; 7. Private string remark; 8 .... // Getter and setter 9 .} |
Note that the filecontent type of fields in the database table of Blob type in tfile is byte []. The tfile's hibernate ing file tfile. HBM. XML is placed in the same directory of the tfile. Java class file:
Code 2 domain object ing File
1. <? XML version = "1.0"?> 2. <! Doctype hibernate-mapping public 3. "-// hibernate/hibernate mapping DTD 3.0 // en" 4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5. 6. <class name = "sshfile. model. tfile" table = "t_file"> 7. <ID name = "fileid" type = "Java. Lang. String" column = "file_id"> 8. <generator class = "UUID. Hex"/> 9. </ID> 10. <property name = "filecontent" 11. type = "org. springframework. Orm. hibernate3.support. blobbytearraytype" 12. Column = "file_content" lazy = "true"/> 13 .... // Map other common fields 14. </class> 15. |
The filecontent field is mapped to the blobbytearraytype type provided by spring. blobbytearraytype is a user-defined data type, which implements the org. hibernate. usertype. usertype interface of hibernate. Blobbytearraytype saves byte [] data to blob database fields using the lob operation handle lobhandler obtained from sessionfactory. In this way, we no longer need to use a hard-coded method, First insert and then update to complete blob-type data persistence. This old man who was difficult to serve was finally popularized. For more information about lobhandler configuration, see the content below.
In addition, lazy = "true" indicates that when the entire tfile object is returned, the data of the filecontent field is not returned, only when the tfile is explicitly called. the getfilecontent () method actually obtains the filecontent data from the database. This is a new feature introduced by hibernate3. For table fields that contain heavyweight big data, this extraction method improves the flexibility of large field operations. Otherwise, if filecontent is always returned when a tfile object is loaded, this batch data extraction can lead to the database's "flooding effect ".
2. Write and configure Dao
Spring emphasizes interface-oriented programming, So we define all the methods for tfile data operations in the tfiledao interface. These methods are:
· Findbyfildid (string fileid)
· Save (tfile)
· List findall ()
Tfiledaohibernate provides the implementation of tfiledao Interface Based on Hibernate, as shown in code 3:
Code 3: hibernate-based filedao implementation class
1. Package sshfile. Dao; 2. 3. Import sshfile. model .*; 4. Import org. springframework. Orm. hibernate3.support. hibernatedaosupport; 5. Import java. util. List; 6. 7. Public class tfiledaohibernate 8. extends hibernatedaosupport implements tfiledao 9 .{ 10. Public tfile findbyfildid (string fileid) 11 .{ 12. Return (tfile) gethibernatetemplate (). Get (tfile. Class, fileid ); 13 .} 14. Public void save (tfile) 15 .{ 16. gethibernatetemplate (). Save (tfile ); 17. gethibernatetemplate (). Flush (); 18 .} 19. Public list findall () 20 .{ 21. Return gethibernatetemplate (). loadall (tfile. Class ); 22 .} 23 .} |
Tfiledaohibernate is built by extending the hibernate Support class hibernatedaosupport provided by spring. hibernatedaosupport encapsulates hibernatetemplate, while hibernatetemplate encapsulates almost all data operation methods provided by hibernate, such as execute (hibernatecallback action ), load (class entityclass, serializable ID), save (final object entity) and so on.
Therefore, the DaO simply needs to call the hibernatetemplate of the parent class to complete almost all database operations.
Since spring completes data layer operations through proxy hibernate, the information in the source hibernate configuration file hibernate. cfg. XML is also transferred to the spring configuration file:
Code 4: Configure hibernate in spring
1. <beans> 2. <! -- Data source configuration // --> 3. <bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource" 4. Destroy-method = "close"> 5. <property name = "driverclassname" value = "oracle. JDBC. Driver. oracledriver"/> 6. <property name = "url" value = "JDBC: oracle: thin: @ localhost: 1521: ora9i"/> 7. <property name = "username" value = "test"/> 8. <property name = "password" value = "test"/> 9. </bean> 10. <! -- Hibernate session factory configuration // --> 11. <bean id = "sessionfactory" 12. Class = "org. springframework. Orm. hibernate3.localsessionfactorybean"> 13. <property name = "datasource" ref = "datasource"/> 14. <property name = "mappingdirectorylocations"> 15. <list> 16. <value> classpath:/sshfile/Model </value> 17. </List> 18. </property> 19. <property name = "hibernateproperties"> 20. <props> 21. <prop key = "hibernate. dialect"> org. hibernate. dialect. oracledialect </prop> 22. <prop key = "hibernate. cglib. use_reflection_optimizer"> true </prop> 23. </props> 24. </property> 25. </bean> 26. <! -- Hibernate template // --> 27. <bean id = "hibernatetemplate" 28. Class = "org. springframework. Orm. hibernate3.hibernatetemplate"> 29. <property name = "sessionfactory" ref = "sessionfactory"/> 30. </bean> 31. <! -- Dao configuration // --> 32. <bean id = "tfiledao" class = "sshfile. Dao. tfiledaohibernate"> 33. <property name = "hibernatetemplate" ref = "hibernatetemplate"/> 34. </bean> 35 .... 36. </beans> |
3rd ~ Line 9 defines a data source. Its implementation class is Apache's basicdatasource, 11th ~ Line 25 defines the hibernate session factory. The session factory class is maintained using localsessionfactorybean provided by spring. It injects data source and resource ing files, in addition, some key-value pairs are used to set the attributes required by hibernate.
In the first row, the sshfile is mapped to the class path. the ing files of all domain objects under the model class package directory are loaded into the tfile. HBM. XML ing file. If multiple ing files need to be declared, it is easier to use the class path ing method than directly specifying the ing file name.
27th ~ The 30 rows define the hibernatetemplate template for spring proxy hibernate data operations, while the 32nd ~ Line 34 injects the Template into tfiledao.
You need to specify that spring 1.2.5 provides two sets of hibernate support packages. The encapsulation classes related to hibernate 2 are located at Org. springframework. orm. in the hibernate2. * package, while the hibernate 3.0 encapsulation class is located at Org. springframework. orm. in the hibernate3. * package, you must make the correct selection based on your hibernate version.
3. configuration of Lob Field Processing
We have already pointed out that Oracle's lob field and general type field have a significant difference in operation-that is, you must first use Oracle's empty_blob ()/empty_clob () initialize the lob field, obtain the reference of this field, and change its value through this reference. Therefore, to complete the lob field operation, Hibernate must perform two steps to access the database, First insert and then update.
After the blobbytearraytype field type is used, why can we operate BLOB fields like the common field type? It is certain that blobbytearraytype cannot pass the natural blob operation method. It turns out to be the specific data access function of the blobbytearraytype data type. It hides the two data access operations through lobhandler, so that the operations on BLOB fields are similar to other general field types, so lobhandler is the one behind the scenes Hero "suffering me, happiness, and billions of people.
Lobhandler must be injected into the sessionfactory of the hibernate session factory, because sessionfactory is responsible for generating sessions that interact with the database. The lobhandler configuration is shown in code 5:
Code 5 lob Field Processing handle Configuration
1. <beans> 2 .... 3. <bean id = "nativejdbcextractor" 4. Class = "org. springframework. JDBC. Support. nativejdbc. commonsdbcpnativejdbcextractor" 5. Lazy-init = "true"/> 6. <bean id = "lobhandler" 7. Class = "org. springframework. JDBC. Support. lob. oraclelobhandler" lazy-init = "true"> 8. <property name = "nativejdbcextractor"> 9. <ref local = "nativejdbcextractor"/> 10. </property> 11. </bean> 12 .... 13. </beans> |
First, you must define the nativejdbcextractor that can extract JDBC objects (such as oracleconnection and oracleresultset) from the connection pool to perform operations on specific databases. For simple data connection pools that only encapsulate connection and do not include statement, simplenativejdbcextractor is the most efficient extraction implementation class, but specific to the Apache basicdatasource connection pool, it encapsulates all JDBC objects, so you need to use commonsdbcpnativejdbcextractor. Spring provides JDBC extractors for data sources of several famous web servers:
· WebLogic: weblogicnativejdbcextractor
· Webfe-Ere: Webfe-EreNativejdbcextractor
· JBoss: jbossnativejdbcextractor
After defining the JDBC extract, define lobhandler. Spring 1.2.5 provides two lobhandler:
· Defaultlobhandler: applicable to most databases, such as sqlserver and MySQL. It also applies to Oracle 10 Gb, but not to Oracle 9i (it seems that Oracle 9i is indeed a freak, oracle 9i is a transitional product ).
· Oraclelobhandler: Applicable to Oracle 9i and Oracle 10g.
Because our database is Oracle9i, oraclelobhandler is used.
After configuring lobhandler, you also need to inject it into the bean of sessionfactory. The following is the configuration of the sessionfactory Bean after the call:
Code 6 inject lobhandler into the configuration in sessionfactory
1. <beans> 2 .... 3. <bean id = "sessionfactory" 4. Class = "org. springframework. Orm. hibernate3.localsessionfactorybean"> 5. <property name = "datasource" ref = "datasource"/> 6. <! -- Handle declaration for processing BLOB fields // --> 7. <property name = "lobhandler" ref = "lobhandler"/> 8 .... 9. </bean> 10 .... 11. </beans> |
As shown in figure 7th, injection is performed through the lobhandler attribute of sessionfactory.