大对象属性JPA映射

来源:互联网 发布:网络协议分析与仿真 编辑:程序博客网 时间:2024/05/22 01:32

大对象类型的数据库表字段

以MySQL为例,保存字符数据的数据库表字段类型一般选择char,varchar,nchar,nvarchar。保存二进制数据的数据库表字段类型一般选择binary,varbinary。但是,这些类型保存的数据长度非常有限。比如,我们需要保存一篇长文章,一个大文件,这些类型的字段长度往往就不够使用了。MySQL常用的大对象类型有两个,分别是text和blob,分别存储大字符数据,大二进制数据。

CREATE TABLE `person` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `name` varchar(45) NOT NULL,  `resume` longtext,  `headPortrait` longblob,  `height` decimal(4,2) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB;
字段resume类型是longtext,存储大字符数据。字段headPortrait类型是longblob,存储大二进制数据。字段height类型是decimal(4,2),表示整数位和小数位一共4位,其中小数位占用2位,整数位占用2位。如果小数位不足2位,用0填充。比如,1.2,存储为2.20。如果整数位超出2位,比如178报出如下异常。

Operation failed: There was an 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 1SQL Statement:UPDATE `entitymappings`.`person` SET `height`='178' WHERE `id`='1'
如果小数位超出2位,不会报出异常,而是使用四舍五入法,结果保留2位小数。比如15.236,存储为15.27。

实体类

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@Tablepublic 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;}@Lobpublic String getResume() {return resume;}public void setResume(String resume) {this.resume = resume;}@Lobpublic 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;}}
注解javax.persistence.Lob,如果属性的类型是String、char[],则被映射成数据库字段的text类型,比如longtext。如果属性的类型是byte[]、Serializable,则被映射成数据库字段的blob类型,比如longblob。

持久化

Person person = new Person();person.setName("张三");person.setHeight(12.3996);person.setResume("擅长编程、操作应用系统、网络、数据库。擅长编程、操作应用系统、网络、数据库。擅长编程、操作应用系统、网络、数据库。");person.setHeadPortrait(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });manager.persist(person);
查看数据库表数据,如下所示。

基本数据类型映射

JPA可以自动映射实体基本的数据类型,而无需使用注解。下面的列表简单的描述JPA自动把实体的基本数据类型映射为数据库表字段类型,其中,数据库表字段类型以MySQL为例。
int、Integer->int或者其他兼容类型;
long、Long、BigInteger->bigint或者其他兼容类型;
short、Short->smallint或者其他兼容类型;
double、Double->double或者其他兼容类型;
float、Float->float或者其他兼容类型;
BigDecimal->decimal或者其他兼容类型;
byte、Byte->binary或者其他兼容类型;
char、Char->char或者其他兼容类型;
java.sql.Timestamp->datetime或者其他兼容类型;
java.sql.Time->time或者其他兼容类型;
java.sql.Date->date或者其他兼容类型;

有些数据类型,则需要使用注解完成映射。下面的列表简单的描述JPA使用注解把实体的数据类型映射为数据库表字段类型,其中,数据库表字段类型以MySQL为例。
byte[]、Byte[]、Serializable,使用注解javax.persistence.Lob,映射成数据库表字段的blob类型;
char[]、Character[]、String使用注解javax.persistence.Lob,映射成数据库表字段的text类型;
java.util.Date、java.util.Calendar使用注解javax.persistence.Temporal,转为成数据类型java.sql.Timestamp、java.sql.Time、java.sql.Date,接着映射成数据库表字段的datetime类型、time类型、date类型。
Enum,使用注解javax.persistence.Enumerated,映射成数据库表字段的enum类型;

0 0
原创粉丝点击