Struts+spring+hibernate processing LOB (BLOB,CLOB)

Source: Internet
Author: User

In the development of using struts+spring+hibernate, there are times when users have the need for a database to store files, and in a database, a BLOB field or CLOB field is typically used to store binary images, streaming media, or files. Now will be in the actual development of problems encountered and solutions to tell.

first, the problem needs:

1. What type of field is used in the persisted class?
2. What is the data type of the file in struts?
3. What are the types of fields in hibernate?
4. How do I process these files in spring?

Second, the solution:

1. In the database these files end up storing binary objects as bytes, so the corresponding attribute is defined as a byte[] type in the persisted class.

2. In struts you can use the tag to upload a file, so the corresponding Actionform property type is: Formfile ( Org.apache.struts.upload.FormFile) type, even if a dynamic form is used, the same type is used.
  Tip: Also change the form label to

  3. The type of file column in Hibernate can be:

(1) Org.springframework.orm.hibernate3.support.BlobByteArrayType

(2) Org.springframework.orm.hibernate3.support.ClobStringType

(3) Clob

(4) Blob

(5) Binary

When do you use Blobbytearraytype? When do you use Clobstringtype?

  Generally, if the object to be processed is the database field type is blob (mainly pictures, binary objects, etc.), the mapping file is set to: Org.springframework.orm.hibernate3.support.BlobByteArrayType Database field is Clob (large text object)
Type, to set the type of the Java property to string, the mapping file setting is: Org.springframework.orm.hibernate3.support.ClobStringType.

4. If you want to use the upload feature that comes with struts in spring, it must be declared in the spring configuration file. No will appear java.lang.IllegalStateException:No Lobhandler found for Configuration-lobhandler Property
Must is set on Localsessionfactorybean exception. The configuration is as follows:

(1) Declare a handle:

  

1 
   

(2) Inject Lobhandler into the sessionfactory:

1 
     2 
    3 

 Tip: When specifying Lobhandler, for MySQL, DB2, MS SQL Server, Oracle 10g, use Defaultlobhandler, and Oracle 9i, you can use Oraclelobhandler. Because Oracle9i handles lobs in a different way than it does, the simplenativejdbcextractor provided by spring is used here. Special declarations for handling oracle9i LOB types:

1 2 simplenativejdbcextractor "/>3 4 
    5 
    6 
    7 8 9 Ten 
     One     A !--handle declaration for processing BLOB type fields--> - 
     - 
     the  -</bean>

You should also use the corresponding JDBC driver.

Clob field definition: length here refers to bytes, up to 2G. This field is declared in a Java object as a string type.

If you are using a MySQL database, its default upload file size is only 1047552 bytes: If the uploaded file is larger than 1047552 bytes, an exception is org.springframework.jdbc.UncategorizedSQLException: You can change this value on the server by setting the Max_allowed_packet ' variable. Follow the prompts to modify the Max_allowed_packet properties of the MySQL database Size.
Open the # SERVER section in the My.ini file and add max_allowed_packet= under port=3306? After M modification, restart the MySQL service to upload successfully.

Struts+spring+hibernate processing LOB (BLOB,CLOB)

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.