For enumeration types, you cannot simply map the corresponding database field type (unless the custom type) by Org.hibernate.annotations.Type annotations. In this case, Hibernate provides javax.persistence.Enumerated annotations, which have two scenarios for the parameters of the annotation:
1.enumtype.string: When using this parameter, the string value of the enumeration is stored in the database table;
2.enumtype.ordinal: When using this parameter, the value of the enumerated int is present in the database table;
If a new Type field is added to the data class Movie.java, the field is defined as the enumeration type Movietype, which is used to identify the movie, TV, or animation. The enumeration Movietype code is as follows:
1 Package Study.hibernate.model; 2 3 Public enum Movietype { 4 5 6 7 8 9 Ten VARIETY}
To configure a mapping relationship, add @enumerated annotations to the type attribute in the data class:
1 PackageStudy.hibernate.model;2 3 ImportJavax.persistence.Column;4 ImportJavax.persistence.Convert;5 Importjavax.persistence.Entity;6 ImportJavax.persistence.EnumType;7 Importjavax.persistence.Enumerated;8 Importjavax.persistence.Id;9 Importjavax.persistence.Table;Ten One ImportOrg.hibernate.annotations.Type; A - /** - * Movie Data class the * - */ - @Entity -@Table (name= "MOVIE") + Public classMovie { - @Id +@Column (name= "movie_id") A Private intID; at -@Column (name= "name") -@Type (type= "string") - PrivateString name; - -@Column (name= "DESCRIPTION") in@Type (type= "text") - PrivateString description; to +@Column (name= "TYPE") - @Enumerated (enumtype.ordinal) the Privatemovietype type; * $ Public intgetId () {Panax Notoginseng returnID; - } the + Public voidSetId (intID) { A This. ID =ID; the } + - PublicString GetName () { $ returnname; $ } - - Public voidsetName (String name) { the This. Name =name; - }Wuyi the PublicString getdescription () { - returndescription; Wu } - About Public voidsetdescription (String description) { $ This. Description =description; - } - - PublicMovietype GetType () { A returntype; + } the - Public voidsetType (movietype type) { $ This. Type =type; the } the the}
When the app starts, set the corresponding type property:
New Movie (); Movie.setid (1); Movie.setname ("Speed and Passion 8"); Movie.setdescription ("Domenek (Vin Diesel Vin Diesel adorn) and the (Michelle Rodriguez Michelle Rodriguez) Honeymoon, Brian and Mia withdrew from the racing world, The life of the world's top speed family is becoming dull. However, a mysterious woman cipher (Charlize Theron Charlize T Heron adorn) The emergence of the entire team involved in the crisis of trust and betrayal, faced with an unprecedented test. "); Movie.settype (Movietype.cartoon);
After running, look at the database and discover that the Type field in the movie table is assigned a value of 2:
Select name, type from movie; +------------------+------+| Name | type |+------------------+------+| speed and passion 8 | 2 |+------------------+------+1 in Set (0.00 sec)
If the @enumerated annotation parameter is configured with Enumtype.string, the corresponding string value for the enumeration is stored in the database:
Select name, type from movie; +------------------+---------+| Name | type |+------------------+---------+| speed and passion 8 | CARTOON |+------------------+---------+1 in Set (0.00 sec)
The values stored in the database through @enumerated are fixed and are not customizable, if the enumeration class Movietype is declared as follows:
1 PackageStudy.hibernate.model;2 3 Public enumMovietype {4FILM ("movie"), 5 6TV ("Serial"), 7 8CARTOON ("Anime"), 9 TenVARIETY ("Entertainment"); One A PrivateString type; - - Privatemovietype (String type) { the This. Type =type; - } - - @Override + PublicString toString () { - returntype; + } A}
For the above enumeration, if the value in the database exists as "anime" if the type is Movietype.cartoon, and if the type is movietype.film, there is a value in the database for "movie". The functionality of @enumerated is not sufficient at this point, and can be implemented by specifying the @convert annotation to declare the corresponding transformation class.
@Convert annotations are used to declare how an object can be converted to a database-recognizable type when it is written to a database, and how to revert to an object when it is read from a database, as a rule of serialization and deserialization.
To use @convert annotations, you first need to define a class that implements the Javax.persistence.attributeconverter<x, Y> interface, which has two methods:
Javax.persistence.AttributeConverter.convertToDatabaseColumn (x) is used to illustrate how to convert Java object X to data object y stored in the database;
Javax.persistence.AttributeConverter.convertToEntityAttribute (y) is used to illustrate how to convert the data object y in the database to the corresponding Java object x;
If our implementation class name is Movietypeconvertor, the code is as follows:
1 PackageStudy.hibernate.model;2 3 ImportJavax.persistence.AttributeConverter;4 5 Public classMovietypeconvertorImplementsAttributeconverter<movietype, string> {6 7 PublicString converttodatabasecolumn (Movietype attribute) {8 returnstring.valueof (attribute);9 }Ten One Publicmovietype Converttoentityattribute (String dbdata) { A returnmovietype.valueof (dbdata); - } - the}
Second, add @covert annotations on the Type property of the database Movie.java
1 @Column (name= "TYPE")2 @Convert (converter=movietypeconvertor.class)3 private movietype type;
Finally, run the program, view the values in the database, and find that the value of the data has been set to "anime"
Mysql> SelectName, type frommovie;+------------------+--------+|Name|Type|+------------------+--------+|Speed and Passion 8|Animation|+------------------+--------+1Rowinch Set(0.00Sec
@Convert annotations are not only used to match annotations, it can be used on any type of data, for example, the contents of a collection can be separated by a space in the database, read out as a space separated into a collection, and so on.
Hibernate learning Notes (iii)---Mapping enumeration types