Large object Property JPA mapping

Source: Internet
Author: User
Tags datetime
database table fields of large object types

In MySQL, for example, the database table field type that holds character data is generally selected Char,varchar,nchar,nvarchar. The database table field type that holds the binary data is generally selected binary,varbinary. However, these types hold very limited data lengths. For example, we need to save a long article, a large file, these types of field lengths are often not enough to use. There are two large object types commonly used in MySQL, namely text and blobs, respectively, storing big character data, and sophomore binary data.

CREATE TABLE ' person ' (
  ' id ' bigint () ' is not null auto_increment,
  ' name ' varchar ' is not null
  ', ' Resume ' longte XT,
  ' headportrait ' longblob,
  ' height ' decimal (4,2) not NULL,
  PRIMARY KEY (' id ')
) Engine=innodb;
The field resume type is Longtext, which stores large character data. The field Headportrait type is Longblob, which stores the sophomore binary data. The field height type is decimal (4,2), which indicates that the integer digits and decimal digits are altogether 4 bits, where the decimal place occupies 2 bits and the integer digit occupies 2 bits. If the number of decimal digits is less than 2 bits, fill with 0. For example, 1.2, storage is 2.20. If the integer digits exceed 2 bits, for example 178, the following exception is reported.

Operation Failed:there was a error while applying the SQL script to the database.
Executing:
UPDATE ' entitymappings '. ' Person ' SET ' height ' = ' 178 ' WHERE ' id ' = ' 1 ';

ERROR 1264:1264:out of range value for column ' Height ' at row 1
SQL Statement:
UPDATE ' entitymappings '. ' Person ' SET ' height ' = ' 178 ' WHERE ' id ' = ' 1 '
If the decimal place exceeds 2 bits, the exception is not reported, but the rounding method is used, and the result retains 2 decimal places. For example, 15.236, storage is 15.27.

entity class

Package com.gxz.entities;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.Lob;

Import javax.persistence.Table;
	@Entity @Table public class Person {private long ID;
	Private String resume;
	Private byte[] headportrait;
	private double height;
	
	private String name;
	@Id @GeneratedValue (strategy = generationtype.identity) public long getId () {return Id;
	} public void SetId (long id) {this.id = ID;
	} @Lob Public String Getresume () {return resume;
	public void Setresume (String resume) {This.resume = resume;
	} @Lob Public byte[] Getheadportrait () {return headportrait;
	} public void Setheadportrait (byte[] headportrait) {this.headportrait = headportrait;
	} public double GetHeight () {return height;
	public void SetHeight (double height) {this.height = height;
	} public String GetName () {return name; } public void SetName (STring name) {this.name = name;
 }
}
Annotation Javax.persistence.Lob, if the type of the property is string, char[], is mapped to the text type of the database field, such as Longtext. If the type of the property is byte[], Serializable, it is mapped to the BLOB type of the database field, such as Longblob. Persistence of
person person = new person ();
Person.setname ("Zhang San");
Person.setheight (12.3996);
Person.setresume ("Good at programming, operating application systems, networks, databases. Good at programming, operating application system, network, database. Good at programming, operating application system, network, database. ");
Person.setheadportrait (new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
Manager.persist (person);
View the database table data as shown below.
basic Data type mappingJPA can automatically map entity basic data types without the use of annotations. The following list simply describes how JPA automatically maps the basic data type of an entity to a database table field type, where the database table field type takes MySQL as an example. int, integer->int, or other compatible type, long, long, biginteger->bigint, or other compatible type; short, short->smallint, or other compatible type; double, Double->double or other compatible type; float, float->float, or other compatible type; Bigdecimal->decimal or other compatible type; byte, byte-> Binary or other compatible type, char, Char->char, or other compatible type; Java.sql.timestamp->datetime or other compatible type; java.sql.time-> Time or other compatible type;
Java.sql.date->date or other compatible type;

Some data types, you need to use annotations to complete the mapping. The following list simply describes how JPA uses annotations to map an entity's data type to a database table field type, where the database table field type takes MySQL as an example. Byte[], byte[], Serializable, using annotation javax.persistence.Lob, the Blob type mapped to the database table field; char[], character[], String is mapped to the text type of the database table field using annotation javax.persistence.Lob;
Java.util.Date, Java.util.Calendar use annotation javax.persistence.Temporal, turn into data type Java.sql.Timestamp, Java.sql.Time, Java.sql.Date, which is then mapped to the datetime type, time type, Date type of the database table field. Enum, which is mapped to the enum type of the database table field using the annotation javax.persistence.Enumerated;

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.