Mapping JPA mysql text Type Problems

  • 2021-11-29 06:58:09
  • OfStack

Mapping of directory JPA mysql text type problem background solution JPA mapping processing of various types

Mapping of JPA mysql text Type

Problem background

jpa an error is reported if the text/longtext/tinytext type of mysql is mapped directly to the String field. You need to set @ Lob and @ Column under 1.

@ Lob stands for a long field type, which by default is longtext, so the following attribute is required to specify the corresponding type.

The types in columnDefinition= "text" can be changed at will, and there may be new types in the following mysql. As long as it is the String type corresponding to java, it can be dynamically configured here.

Solutions


@Data
@Entity
@Table(name="question")
public class Question {
    @Id
    @GeneratedValue
    public int questionId;
    //.... Omit other fields 
    @Lob
    @Column(columnDefinition="text")
    public String explainStr;
    public Date createTime;
}

JPA Mapping Processing of Various Types

1. Mapping of date format type fields, annotated with @ Temporal (TemporalType. Date); For example:


private Date birthday;
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}

2. Mapping of enumerated types, using @ Enumerated, whose parameter EnumType indicates the form specified to be stored in the data table, integer or String;

First, create an enumeration:


public enum Gender
  {
Male,Female
}

Called in the entity class:


private Gender gender = Gender.Male;  //  Set default values for enumeration 
@Enumerated(EnumType.STRING)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}

3. Mapping of large text data types, such as weblog, the length of string type is obviously not enough. Use @ Lob annotation, which is used to generate LongText data on the string type in the database; For example:


private String diary;
@Lob
public String getDiary() {
return diary;
}
public void setDiary(String diary) {
this.diary = diary;
}

4. @ Lob annotation is used in Byte [] array type, for example, saving 1 file can use this type, and using this type can generate LongBolb data type in database; For example:


private Byte[] file;
@Lob
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}

5. What if the field does not need to be mapped to the tables in the database in the entity class, but the default is to map all the fields of the entity class to the columns of the data table? Use @ Transient annotations, such as:


private String other;
@Transient
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}

6. If an entity class contains a field of big data type, such as Byte [], we have to query the field of this data type when querying the entity class. If we only use the data of one other field when querying, but query all the data by default, how can we avoid querying the data of this big data type? Use the @ Basic annotation to annotate, and set the fetch attribute value to Lazy, so that it will only be loaded when we call the get method of this attribute;


private Byte[] file;
@Lob @Basic(fetch=FetchType.LAZY)
public Byte[] getFile() {
return file;
}
public void setFile(Byte[] file) {
this.file = file;
}

7. @ Column This annotation represents the mapped column of the data table and is placed on the getter method of the property:

.length This property represents the length of the mapped column; .nullable This attribute indicates whether the column can be null, true indicates it can be null, false indicates it cannot be null; .name This attribute is expressed as an alias for the column, so that the attribute name of the entity class is not the same as the column name of the database;

8. @ Table This annotation represents the mapped table; On top of this entity class:

.name This attribute is expressed as an alias for the table, so that the entity class is different from the data table name;

Related articles: