Migrate Enums, Clob, Blob, and Nationalized User Guide examples to unit tests
This commit is contained in:
parent
5c1f4238c6
commit
e99b60cbb6
|
@ -1,4 +1,5 @@
|
|||
= Hibernate User Guide
|
||||
= Hibernate ORM {majorMinorVersion} User Guide
|
||||
Steve Ebersole, Vlad Mihalcea, Andrea Boriero, Brett Meyer, Radim Vansa
|
||||
:toc:
|
||||
:toclevels: 3
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
[[basic]]
|
||||
=== Basic Types
|
||||
:modeldir: ../../../../../main/java/org/hibernate/userguide/model
|
||||
:sourcedir: ../../../../../test/java/org/hibernate/userguide/mapping
|
||||
:extrasdir: extras
|
||||
|
||||
|
@ -399,86 +400,146 @@ include::{extrasdir}/basic/basic-custom-type-BitSetUserType-persistence-sql-exam
|
|||
|
||||
Hibernate supports the mapping of Java enums as basic value types in a number of different ways.
|
||||
|
||||
==== @Enumerated
|
||||
[[basic-enums-Enumerated]]
|
||||
===== @Enumerated
|
||||
|
||||
The original JPA-compliant way to map enums was via the `@Enumerated` and `@MapKeyEnumerated` for map keys annotations which works on the principle that the enum values are stored according to one of 2 strategies indicated by `javax.persistence.EnumType`:
|
||||
|
||||
* `ORDINAL` - stored according to the enum value's ordinal position within the enum class, as indicated by java.lang.Enum#ordinal
|
||||
* `STRING` - stored according to the enum value's name, as indicated by java.lang.Enum#name
|
||||
`ORDINAL`:: - stored according to the enum value's ordinal position within the enum class, as indicated by java.lang.Enum#ordinal
|
||||
`STRING`:: - stored according to the enum value's name, as indicated by java.lang.Enum#name
|
||||
|
||||
Assuming the following enumeration:
|
||||
|
||||
.`PhoneType` enumeration
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{modeldir}/PhoneType.java[tags=hql-examples-domain-model-example]
|
||||
----
|
||||
====
|
||||
|
||||
In the ORDINAL example, the `phone_type` column is defined as an (nullable) INTEGER type and would hold:
|
||||
|
||||
`NULL`:: For null values
|
||||
`0`:: For the `LAND_LINE` enum
|
||||
`1`:: For the `MOBILE` enum
|
||||
|
||||
[[basic-enums-Enumerated-ordinal-example]]
|
||||
.`@Enumerated(ORDINAL)` example
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/EnumeratedOrdinal.java[]
|
||||
include::{sourcedir}/basic/PhoneTypeEnumeratedOrdinalTest.java[tags=basic-enums-Enumerated-ordinal-example]
|
||||
----
|
||||
====
|
||||
|
||||
In the ORDINAL example, the gender column is defined as an (nullable) INTEGER type and would hold:
|
||||
When persisting this entity, Hibernate generates the following SQL statement:
|
||||
|
||||
* `NULL` - null
|
||||
* `0` - MALE
|
||||
* `1` - FEMALE
|
||||
[[basic-enums-Enumerated-ordinal-persistence-example]]
|
||||
.Persisting an entity with an `@Enumerated(ORDINAL)` mapping
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/PhoneTypeEnumeratedOrdinalTest.java[tags=basic-enums-Enumerated-ordinal-persistence-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/basic-enums-Enumerated-ordinal-persistence-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
In the STRING example, the `phone_type` column is defined as an (nullable) VARCHAR type and would hold:
|
||||
|
||||
`NULL`:: For null values
|
||||
`LAND_LINE`:: For the `LAND_LINE` enum
|
||||
`MOBILE`:: For the `MOBILE` enum
|
||||
|
||||
[[basic-enums-Enumerated-string-example]]
|
||||
.`@Enumerated(STRING)` example
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/EnumeratedString.java[]
|
||||
include::{sourcedir}/basic/PhoneTypeEnumeratedStringTest.java[tags=basic-enums-Enumerated-string-example]
|
||||
----
|
||||
====
|
||||
|
||||
In the STRING example, the gender column is defined as an (nullable) VARCHAR type and would hold:
|
||||
Persisting the same entity like in the `@Enumerated(ORDINAL)` example, Hibernate generates the following SQL statement:
|
||||
|
||||
* `NULL` - null
|
||||
* `MALE` - MALE
|
||||
* `FEMALE` - FEMALE
|
||||
[[basic-enums-Enumerated-string-persistence-example]]
|
||||
.Persisting an entity with an `@Enumerated(STRING)` mapping
|
||||
====
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/basic-enums-Enumerated-string-persistence-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
[[basic-attribute-converter]]
|
||||
==== AttributeConverter
|
||||
[[basic-enums-attribute-converter]]
|
||||
===== AttributeConverter
|
||||
|
||||
You can also map enums in a JPA compliant way using a JPA 2.1 AttributeConverter.
|
||||
Let's revisit the Gender enum example, but instead we want to store the more standardized `'M'` and `'F'` codes.
|
||||
Let's consider the following `Gender` enum which stores its values using the `'M'` and `'F'` codes.
|
||||
|
||||
.Enum mapping with AttributeConverter example
|
||||
[[basic-enums-converter-example]]
|
||||
.Enum with custom constructor
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/EnumAttributeConverter.java[]
|
||||
include::{sourcedir}/basic/Gender.java[tags=basic-enums-converter-example]
|
||||
----
|
||||
====
|
||||
|
||||
You can map enums in a JPA compliant way using a JPA 2.1 AttributeConverter.
|
||||
|
||||
[[basic-enums-attribute-converter-example]]
|
||||
.Enum mapping with `AttributeConverter` example
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/EnumerationConverterTest.java[tags=basic-enums-attribute-converter-example]
|
||||
----
|
||||
====
|
||||
|
||||
Here, the gender column is defined as a CHAR type and would hold:
|
||||
|
||||
* `NULL` - null
|
||||
* `'M'` - MALE
|
||||
* `'F'` - FEMALE
|
||||
`NULL`:: For null values
|
||||
`'M'`:: For the `MALE` enum
|
||||
`'F'`:: For the `FEMALE` enum
|
||||
|
||||
For additional details on using AttributeConverters, see <<basic-jpa-convert>> section.
|
||||
|
||||
Note that JPA explicitly disallows the use of an AttributeConverter with an attribute marked as `@Enumerated`.
|
||||
[NOTE]
|
||||
====
|
||||
JPA explicitly disallows the use of an AttributeConverter with an attribute marked as `@Enumerated`.
|
||||
So if using the AttributeConverter approach, be sure to not mark the attribute as `@Enumerated`.
|
||||
====
|
||||
|
||||
==== Custom type
|
||||
[[basic-enums-custom-type]]
|
||||
===== Custom type
|
||||
|
||||
You can also map enums using a Hibernate custom type mapping.
|
||||
Let's again revisit the Gender enum example, this time using a custom Type to store the more standardized `'M'` and `'F'` codes.
|
||||
|
||||
[[basic-enums-custom-type-example]]
|
||||
.Enum mapping with custom Type example
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/EnumCustomType.java[]
|
||||
include::{sourcedir}/basic/EnumerationCustomTypeTest.java[tags=basic-enums-custom-type-example, indent=0]
|
||||
|
||||
include::{sourcedir}/basic/GenderType.java[tags=basic-enums-custom-type-example, indent=0]
|
||||
|
||||
include::{sourcedir}/basic/GenderJavaTypeDescriptor.java[tags=basic-enums-custom-type-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
Again, the gender column is defined as a CHAR type and would hold:
|
||||
|
||||
* `NULL` - null
|
||||
* `'M'` - MALE
|
||||
* `'F'` - FEMALE
|
||||
`NULL`:: For null values
|
||||
`'M'`:: For the `MALE` enum
|
||||
`'F'`:: For the `FEMALE` enum
|
||||
|
||||
For additional details on using custom types, see <<basic-custom-type>> section..
|
||||
For additional details on using custom types, see <<basic-custom-type>> section.
|
||||
|
||||
[[basic-lob]]
|
||||
==== Mapping LOBs
|
||||
|
@ -500,84 +561,135 @@ The JDBC LOB locator types include:
|
|||
* `java.sql.Clob`
|
||||
* `java.sql.NClob`
|
||||
|
||||
Mapping materialized forms of these LOB values would use more familiar Java types such as String, char[], byte[], etc.
|
||||
The trade off for "more familiar" is usually performance.
|
||||
Mapping materialized forms of these LOB values would use more familiar Java types such as `String`, `char[]`, `byte[]`, etc.
|
||||
The trade off for _more familiar_ is usually performance.
|
||||
|
||||
For a first look, let's assume we have a CLOB column that we would like to map (NCLOB character LOB data will be covered in <<basic-nationalized>> section.
|
||||
For a first look, let's assume we have a `CLOB` column that we would like to map (`NCLOB` character `LOB` data will be covered in <<basic-nationalized>> section).
|
||||
|
||||
[[basic-clob-sql-example]]
|
||||
.CLOB - SQL
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/Clob.sql[]
|
||||
include::{extrasdir}/basic/basic-clob-sql-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
Let's first map this using the JDBC locator.
|
||||
Let's first map this using the `@Lob` JPA annotation and the `java.sql.Clob` type:
|
||||
|
||||
.CLOB - locator mapping
|
||||
[[basic-clob-example]]
|
||||
.`CLOB` mapped to `java.sql.Clob`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/ClobLocator.java[]
|
||||
include::{sourcedir}/basic/ClobTest.java[tags=basic-clob-example]
|
||||
----
|
||||
====
|
||||
|
||||
We could also map a materialized form.
|
||||
To persist such an entity, you have to create a `Clob` using plain JDBC:
|
||||
|
||||
.CLOB - materialized mapping
|
||||
[[basic-clob-persist-example]]
|
||||
.Persisting a `java.sql.Clob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/ClobMaterialized.java[]
|
||||
include::{sourcedir}/basic/ClobTest.java[tags=basic-clob-persist-example]
|
||||
----
|
||||
====
|
||||
|
||||
To retrieve the `Clob` content, you need to transform the underlying `java.io.Reader`:
|
||||
|
||||
[[basic-clob-find-example]]
|
||||
.Returning a `java.sql.Clob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/ClobTest.java[tags=basic-clob-find-example]
|
||||
----
|
||||
====
|
||||
|
||||
We could also map the CLOB in a materialized form. This way, we can either use a `String` or a `char[]`.
|
||||
|
||||
[[basic-clob-string-example]]
|
||||
.`CLOB` mapped to `String`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/ClobStringTest.java[tags=basic-clob-string-example]
|
||||
----
|
||||
====
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
How JDBC deals with LOB data varies from driver to driver.
|
||||
Hibernate tries to handle all these variances for you.
|
||||
However some drivers do not allow Hibernate to always do that in an automatic fashion (looking directly at you PostgreSQL JDBC drivers).
|
||||
In such cases you may have to do some extra to get LOBs working. Such discussions are beyond the scope of this guide however.
|
||||
How JDBC deals with `LOB` data varies from driver to driver, and Hibernate tries to handle all these variances on your behalf.
|
||||
|
||||
However, some drivers are trickier (e.g. PostgreSQL JDBC drivers), and, in such cases, you may have to do some extra to get LOBs working.
|
||||
Such discussions are beyond the scope of this guide.
|
||||
====
|
||||
|
||||
We might even want the materialized data as a char array (for some crazy reason).
|
||||
|
||||
.CLOB - materialized char[] mapping
|
||||
[[basic-clob-char-array-example]]
|
||||
.CLOB - materialized `char[]` mapping
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/ClobMaterializedCharArray.java[]
|
||||
include::{sourcedir}/basic/ClobCharArrayTest.java[tags=basic-clob-char-array-example]
|
||||
----
|
||||
====
|
||||
|
||||
We'd map BLOB data in a similar fashion.
|
||||
`BLOB` data is mapped in a similar fashion.
|
||||
|
||||
[[basic-blob-sql-example]]
|
||||
.BLOB - SQL
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/Blob.sql[]
|
||||
include::{extrasdir}/basic/basic-blob-sql-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
Let's first map this using the JDBC locator.
|
||||
Let's first map this using the JDBC `java.sql.Blob` type.
|
||||
|
||||
.BLOB - locator mapping
|
||||
[[basic-blob-example]]
|
||||
.`BLOB` mapped to `java.sql.Blob`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/BlobLocator.java[]
|
||||
include::{sourcedir}/basic/BlobTest.java[tags=basic-blob-example]
|
||||
----
|
||||
====
|
||||
|
||||
We could also map a materialized BLOB form.
|
||||
To persist such an entity, you have to create a `Blob` using plain JDBC:
|
||||
|
||||
.BLOB - materialized mapping
|
||||
[[basic-blob-persist-example]]
|
||||
.Persisting a `java.sql.Blob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/BlobMaterialized.java[]
|
||||
include::{sourcedir}/basic/BlobTest.java[tags=basic-blob-persist-example]
|
||||
----
|
||||
====
|
||||
|
||||
To retrieve the `Blob` content, you need to transform the underlying `java.io.Reader`:
|
||||
|
||||
[[basic-blob-find-example]]
|
||||
.Returning a `java.sql.Blob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/BlobTest.java[tags=basic-blob-find-example]
|
||||
----
|
||||
====
|
||||
|
||||
We could also map the BLOB in a materialized form (e.g. `byte[]`).
|
||||
|
||||
[[basic-blob-byte-array-example]]
|
||||
.`BLOB` mapped to `byte[]`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/BlobByteArrayTest.java[tags=basic-blob-byte-array-example]
|
||||
----
|
||||
====
|
||||
|
||||
|
@ -592,34 +704,97 @@ To this end it added specific nationalized character data types.
|
|||
* `LONGNVARCHAR`
|
||||
* `NCLOB`
|
||||
|
||||
[[basic-nationalized-sql-example]]
|
||||
.`NVARCHAR` - SQL
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/basic-nationalized-sql-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
To map a specific attribute to a nationalized variant data type, Hibernate defines the `@Nationalized` annotation.
|
||||
|
||||
.NVARCHAR mapping
|
||||
[[basic-nationalized-example]]
|
||||
.`NVARCHAR` mapping
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/NVARCHAR.java[]
|
||||
include::{sourcedir}/basic/NationalizedTest.java[tags=basic-nationalized-example]
|
||||
----
|
||||
====
|
||||
|
||||
.NCLOB (locator) mapping
|
||||
Just like with `CLOB`, Hibernate can also deal with `NCLOB` SQL data types:
|
||||
|
||||
[[basic-nclob-sql-example]]
|
||||
.`NCLOB` - SQL
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/NCLOB_locator.java[]
|
||||
include::{extrasdir}/basic/basic-nclob-sql-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
.NCLOB (materialized) mapping
|
||||
Hibernate can map the `NCLOB` to a `java.sql.NClob`
|
||||
|
||||
[[basic-nclob-example]]
|
||||
.`NCLOB` mapped to `java.sql.NClob`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/NCLOB_materialized.java[]
|
||||
include::{sourcedir}/basic/NClobTest.java[tags=basic-nclob-example]
|
||||
----
|
||||
====
|
||||
|
||||
To persist such an entity, you have to create a `NClob` using plain JDBC:
|
||||
|
||||
[[basic-nclob-persist-example]]
|
||||
.Persisting a `java.sql.NClob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/NClobTest.java[tags=basic-nclob-persist-example]
|
||||
----
|
||||
====
|
||||
|
||||
To retrieve the `NClob` content, you need to transform the underlying `java.io.Reader`:
|
||||
|
||||
[[basic-nclob-find-example]]
|
||||
.Returning a `java.sql.NClob` entity
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/NClobTest.java[tags=basic-nclob-find-example]
|
||||
----
|
||||
====
|
||||
|
||||
We could also map the `NCLOB` in a materialized form. This way, we can either use a `String` or a `char[]`.
|
||||
|
||||
[[basic-nclob-string-example]]
|
||||
.`NCLOB` mapped to `String`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/NClobStringTest.java[tags=basic-nclob-string-example]
|
||||
----
|
||||
====
|
||||
|
||||
We might even want the materialized data as a char array.
|
||||
|
||||
[[basic-nclob-char-array-example]]
|
||||
.NCLOB - materialized `char[]` mapping
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/NClobCharArrayTest.java[tags=basic-nclob-char-array-example]
|
||||
----
|
||||
====
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
If you application and database are entirely nationalized you may instead want to enable nationalized character data as the default.
|
||||
You can do this via the `hibernate.use_nationalized_character_data` setting or by calling `MetadataBuilder#enableGlobalNationalizedCharacterDataSupport` during bootstrap.
|
||||
====
|
||||
|
||||
[[basic-uuid]]
|
||||
==== Mapping UUID Values
|
||||
|
@ -702,7 +877,7 @@ entityManager.persist(dateEvent);
|
|||
Hibernate generates the following INSERT statement:
|
||||
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
INSERT INTO DateEvent
|
||||
( timestamp, id )
|
||||
|
@ -725,7 +900,7 @@ private Date timestamp;
|
|||
Hibernate will issue an INSERT statement containing the hour, minutes and seconds.
|
||||
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
INSERT INTO DateEvent
|
||||
( timestamp, id )
|
||||
|
@ -746,7 +921,7 @@ private Date timestamp;
|
|||
Hibernate will include both the DATE, the TIME and the nanoseconds in the INSERT statement:
|
||||
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
INSERT INTO DateEvent
|
||||
( timestamp, id )
|
||||
|
@ -826,7 +1001,7 @@ When persisting such entity, Hibernate will do the type conversion based on the
|
|||
|
||||
.Persisting entity using the custom `AttributeConverter`
|
||||
====
|
||||
[source,sql]
|
||||
[source, sql, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/PeriodStringConvert.sql[]
|
||||
----
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
create table step(
|
||||
...
|
||||
instruction BLOB not null,
|
||||
...
|
||||
)
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Step {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
public Blob instructions;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Step {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
public byte[] instructions;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
create table product(
|
||||
...
|
||||
description CLOB not null,
|
||||
...
|
||||
)
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
public Clob description;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
public String description;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
public char[] description;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
public enum Gender {
|
||||
|
||||
MALE('M'),
|
||||
FEMALE('F');
|
||||
|
||||
private final char code;
|
||||
|
||||
private Gender( char code ) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static Gender fromCode( char code ) {
|
||||
if ( code == 'M' || code == 'm' ) {
|
||||
return MALE;
|
||||
}
|
||||
if ( code == 'F' || code == 'f' ) {
|
||||
return FEMALE;
|
||||
}
|
||||
throw...
|
||||
}
|
||||
|
||||
public char getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
...
|
||||
|
||||
@Basic
|
||||
@Convert( converter = GenderConverter.class )
|
||||
public Gender gender;
|
||||
}
|
||||
|
||||
@Converter
|
||||
public class GenderConverter implements AttributeConverter<Character, Gender> {
|
||||
|
||||
public Character convertToDatabaseColumn( Gender value ) {
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return value.getCode();
|
||||
}
|
||||
|
||||
public Gender convertToEntityAttribute( Character value ) {
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Gender.fromCode( value );
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
import org.hibernate.type.descriptor.java.CharacterTypeDescriptor;
|
||||
|
||||
public enum Gender {
|
||||
|
||||
MALE('M'),
|
||||
FEMALE('F');
|
||||
|
||||
private final char code;
|
||||
|
||||
private Gender( char code ) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static Gender fromCode( char code ) {
|
||||
if ( code == 'M' || code == 'm' ) {
|
||||
return MALE;
|
||||
}
|
||||
if ( code == 'F' || code == 'f' ) {
|
||||
return FEMALE;
|
||||
}
|
||||
throw...
|
||||
}
|
||||
|
||||
public char getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
public static class GenderJavaTypeDescriptor extends AbstractTypeDescriptor<Gender> {
|
||||
public static final GenderJavaTypeDescriptor INSTANCE = new GenderJavaTypeDescriptor();
|
||||
|
||||
public String toString( Gender value ) {
|
||||
return value == null ? null : value.name();
|
||||
}
|
||||
|
||||
public Gender fromString( String string ) {
|
||||
return string == null ? null : Gender.valueOf( string );
|
||||
}
|
||||
|
||||
public <X> X unwrap( Gender value, Class<X> type, WrapperOptions options ) {
|
||||
return CharacterTypeDescriptor.INSTANCE.unwrap(
|
||||
value == null ? null : value.getCode(),
|
||||
type,
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
public <X> Gender wrap( X value, WrapperOptions options ) {
|
||||
return CharacterTypeDescriptor.INSTANCE.wrap( value, options );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
...
|
||||
|
||||
@Basic
|
||||
@Type( type = GenderType.class )
|
||||
public Gender gender;
|
||||
}
|
||||
|
||||
@Converter
|
||||
public class GenderType extends AbstractSingleColumnStandardBasicType<Gender> {
|
||||
|
||||
public static final GenderType INSTANCE = new GenderType();
|
||||
|
||||
private GenderType() {
|
||||
super(
|
||||
CharTypeDescriptor.INSTANCE,
|
||||
GenderJavaTypeDescriptor.INSTANCE
|
||||
);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "gender";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean registerUnderJavaType() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
@Entity
|
||||
public class Person {
|
||||
...
|
||||
|
||||
@Enumerated
|
||||
public Gender gender;
|
||||
|
||||
public static enum Gender {
|
||||
MALE,
|
||||
FEMALE
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
@Entity
|
||||
public class Person {
|
||||
...
|
||||
|
||||
@Enumerated( STRING )
|
||||
public Gender gender;
|
||||
|
||||
public static enum Gender {
|
||||
MALE,
|
||||
FEMALE
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
@Nationalized
|
||||
public NClob description;
|
||||
// Clob also works, because NClob
|
||||
// extends Clob. The db type is
|
||||
// still NCLOB either way and
|
||||
// handled as such
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Lob
|
||||
@Basic
|
||||
@Nationalized
|
||||
public String description;
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
@Entity
|
||||
public class Product {
|
||||
...
|
||||
|
||||
@Basic
|
||||
@Nationalized
|
||||
public String description;
|
||||
|
||||
...
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE Product (
|
||||
id INTEGER NOT NULL ,
|
||||
image blob ,
|
||||
name VARCHAR(255) ,
|
||||
PRIMARY KEY ( id )
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE Product (
|
||||
id INTEGER NOT NULL
|
||||
image clob
|
||||
name VARCHAR(255)
|
||||
PRIMARY KEY ( id )
|
||||
)
|
|
@ -0,0 +1,2 @@
|
|||
INSERT INTO Phone (phone_number, phone_type, id)
|
||||
VALUES ('123-456-78990', 2, 1)
|
|
@ -0,0 +1,2 @@
|
|||
INSERT INTO Phone (phone_number, phone_type, id)
|
||||
VALUES ('123-456-78990', 'MOBILE', 1)
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE Product (
|
||||
id INTEGER NOT NULL ,
|
||||
name VARCHAR(255) ,
|
||||
warranty NVARCHAR(255) ,
|
||||
PRIMARY KEY ( id )
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE Product (
|
||||
id INTEGER NOT NULL ,
|
||||
name VARCHAR(255) ,
|
||||
warranty nclob ,
|
||||
PRIMARY KEY ( id )
|
||||
)
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class BlobByteArrayTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Product product = new Product( );
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setImage( new byte[] {1, 2, 3} );
|
||||
|
||||
entityManager.persist( product );
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertArrayEquals( new byte[] {1, 2, 3}, product.getImage() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-blob-byte-array-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
private byte[] image;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-blob-byte-array-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(byte[] image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
//tag::basic-blob-byte-array-example[]
|
||||
}
|
||||
//end::basic-blob-byte-array-example[]
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Blob;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class BlobTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
|
||||
//tag::basic-blob-persist-example[]
|
||||
byte[] image = new byte[] {1, 2, 3};
|
||||
|
||||
final Product product = new Product();
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
|
||||
session.doWork( connection -> {
|
||||
product.setImage( connection.createBlob() );
|
||||
product.getImage().setBytes( 1, image );
|
||||
} );
|
||||
|
||||
entityManager.persist( product );
|
||||
//end::basic-blob-persist-example[]
|
||||
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
try {
|
||||
//tag::basic-blob-find-example[]
|
||||
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
|
||||
try (InputStream inputStream = product.getImage().getBinaryStream()) {
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, toBytes( inputStream ) );
|
||||
}
|
||||
//end::basic-blob-find-example[]
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private byte[] toBytes(InputStream inputStream) throws IOException {
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream( inputStream);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
int result = bufferedInputStream.read();
|
||||
while(result != -1) {
|
||||
byteArrayOutputStream.write((byte) result);
|
||||
result = bufferedInputStream.read();
|
||||
}
|
||||
return byteArrayOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
//tag::basic-blob-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
private Blob image;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-blob-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Blob getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(Blob image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
//tag::basic-blob-example[]
|
||||
}
|
||||
//end::basic-blob-example[]
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class ClobCharArrayTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Product product = new Product( );
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setWarranty( "My product warranty".toCharArray() );
|
||||
|
||||
entityManager.persist( product );
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertArrayEquals( "My product warranty".toCharArray(), product.getWarranty() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-clob-char-array-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
private char[] warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-clob-char-array-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public char[] getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(char[] warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-clob-char-array-example[]
|
||||
}
|
||||
//end::basic-clob-char-array-example[]
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class ClobStringTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Product product = new Product( );
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setWarranty( "My product warranty" );
|
||||
|
||||
entityManager.persist( product );
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertEquals( "My product warranty", product.getWarranty() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-clob-string-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
private String warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-clob-string-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(String warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-clob-string-example[]
|
||||
}
|
||||
//end::basic-clob-string-example[]
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.sql.Clob;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class ClobTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
|
||||
//tag::basic-clob-persist-example[]
|
||||
String warranty = "My product warranty";
|
||||
|
||||
final Product product = new Product();
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
|
||||
session.doWork( connection -> {
|
||||
product.setWarranty( connection.createClob() );
|
||||
product.getWarranty().setString( 1, warranty );
|
||||
} );
|
||||
|
||||
entityManager.persist( product );
|
||||
//end::basic-clob-persist-example[]
|
||||
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
try {
|
||||
//tag::basic-clob-find-example[]
|
||||
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
try (Reader reader = product.getWarranty().getCharacterStream()) {
|
||||
assertEquals( "My product warranty", toString( reader ) );
|
||||
}
|
||||
//end::basic-clob-find-example[]
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private String toString(Reader reader) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader( reader);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
int result = bufferedReader.read();
|
||||
while(result != -1) {
|
||||
byteArrayOutputStream.write((byte) result);
|
||||
result = bufferedReader.read();
|
||||
}
|
||||
return byteArrayOutputStream.toString();
|
||||
}
|
||||
|
||||
|
||||
//tag::basic-clob-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
private Clob warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-clob-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Clob getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(Clob warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-clob-example[]
|
||||
}
|
||||
//end::basic-clob-example[]
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Convert;
|
||||
import javax.persistence.Converter;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class EnumerationConverterTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Person.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( );
|
||||
person.setId( 1L );
|
||||
person.setName( "John Doe" );
|
||||
person.setGender( Gender.MALE );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-enums-attribute-converter-example[]
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Convert( converter = GenderConverter.class )
|
||||
public Gender gender;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-enums-attribute-converter-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
//tag::basic-enums-attribute-converter-example[]
|
||||
}
|
||||
|
||||
@Converter
|
||||
public static class GenderConverter implements AttributeConverter<Gender, Character> {
|
||||
|
||||
public Character convertToDatabaseColumn( Gender value ) {
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return value.getCode();
|
||||
}
|
||||
|
||||
public Gender convertToEntityAttribute( Character value ) {
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Gender.fromCode( value );
|
||||
}
|
||||
}
|
||||
//end::basic-enums-attribute-converter-example[]
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class EnumerationCustomTypeTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Person.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( );
|
||||
person.setId( 1L );
|
||||
person.setName( "John Doe" );
|
||||
person.setGender( Gender.MALE );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-enums-custom-type-example[]
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Type( type = "org.hibernate.userguide.mapping.basic.GenderType" )
|
||||
public Gender gender;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-enums-custom-type-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
//tag::basic-enums-custom-type-example[]
|
||||
}
|
||||
//end::basic-enums-custom-type-example[]
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
//tag::basic-enums-converter-example[]
|
||||
public enum Gender {
|
||||
|
||||
MALE( 'M' ),
|
||||
FEMALE( 'F' );
|
||||
|
||||
private final char code;
|
||||
|
||||
Gender(char code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public static Gender fromCode(char code) {
|
||||
if ( code == 'M' || code == 'm' ) {
|
||||
return MALE;
|
||||
}
|
||||
if ( code == 'F' || code == 'f' ) {
|
||||
return FEMALE;
|
||||
}
|
||||
throw new UnsupportedOperationException(
|
||||
"The code " + code + " is not supported!"
|
||||
);
|
||||
}
|
||||
|
||||
public char getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
//end::basic-enums-converter-example[]
|
|
@ -0,0 +1,42 @@
|
|||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.java.CharacterTypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
//tag::basic-enums-custom-type-example[]
|
||||
public class GenderJavaTypeDescriptor extends AbstractTypeDescriptor<Gender> {
|
||||
|
||||
public static final GenderJavaTypeDescriptor INSTANCE =
|
||||
new GenderJavaTypeDescriptor();
|
||||
|
||||
protected GenderJavaTypeDescriptor() {
|
||||
super( Gender.class );
|
||||
}
|
||||
|
||||
public String toString(Gender value) {
|
||||
return value == null ? null : value.name();
|
||||
}
|
||||
|
||||
public Gender fromString(String string) {
|
||||
return string == null ? null : Gender.valueOf( string );
|
||||
}
|
||||
|
||||
public <X> X unwrap(Gender value, Class<X> type, WrapperOptions options) {
|
||||
return CharacterTypeDescriptor.INSTANCE.unwrap(
|
||||
value == null ? null : value.getCode(),
|
||||
type,
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
public <X> Gender wrap(X value, WrapperOptions options) {
|
||||
return Gender.fromCode(
|
||||
CharacterTypeDescriptor.INSTANCE.wrap( value, options )
|
||||
);
|
||||
}
|
||||
}
|
||||
//end::basic-enums-custom-type-example[]
|
|
@ -0,0 +1,30 @@
|
|||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
|
||||
import org.hibernate.type.descriptor.sql.CharTypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
//tag::basic-enums-custom-type-example[]
|
||||
public class GenderType extends AbstractSingleColumnStandardBasicType<Gender> {
|
||||
|
||||
public static final GenderType INSTANCE = new GenderType();
|
||||
|
||||
public GenderType() {
|
||||
super(
|
||||
CharTypeDescriptor.INSTANCE,
|
||||
GenderJavaTypeDescriptor.INSTANCE
|
||||
);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "gender";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean registerUnderJavaType() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//end::basic-enums-custom-type-example[]
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.annotations.Nationalized;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class NClobCharArrayTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Product product = new Product( );
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setWarranty( "My product warranty".toCharArray() );
|
||||
|
||||
entityManager.persist( product );
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertArrayEquals( "My product warranty".toCharArray(), product.getWarranty() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-nclob-char-array-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
@Nationalized
|
||||
private char[] warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-nclob-char-array-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public char[] getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(char[] warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-nclob-char-array-example[]
|
||||
}
|
||||
//end::basic-nclob-char-array-example[]
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.annotations.Nationalized;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class NClobStringTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Product product = new Product( );
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setWarranty( "My product warranty" );
|
||||
|
||||
entityManager.persist( product );
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertEquals( "My product warranty", product.getWarranty() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-nclob-string-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
@Nationalized
|
||||
private String warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-nclob-string-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(String warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-nclob-string-example[]
|
||||
}
|
||||
//end::basic-nclob-string-example[]
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.sql.NClob;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.annotations.Nationalized;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class NClobTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
|
||||
//tag::basic-nclob-persist-example[]
|
||||
String warranty = "My product warranty";
|
||||
|
||||
final Product product = new Product();
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
|
||||
session.doWork( connection -> {
|
||||
product.setWarranty( connection.createNClob() );
|
||||
product.getWarranty().setString( 1, warranty );
|
||||
} );
|
||||
|
||||
entityManager.persist( product );
|
||||
//end::basic-nclob-persist-example[]
|
||||
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
try {
|
||||
//tag::basic-nclob-find-example[]
|
||||
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
try (Reader reader = product.getWarranty().getCharacterStream()) {
|
||||
assertEquals( "My product warranty", toString( reader ) );
|
||||
}
|
||||
//end::basic-nclob-find-example[]
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail( e.getMessage() );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private String toString(Reader reader) throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader( reader);
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
int result = bufferedReader.read();
|
||||
while(result != -1) {
|
||||
byteArrayOutputStream.write((byte) result);
|
||||
result = bufferedReader.read();
|
||||
}
|
||||
return byteArrayOutputStream.toString();
|
||||
}
|
||||
|
||||
|
||||
//tag::basic-nclob-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Lob
|
||||
@Nationalized
|
||||
// Clob also works, because NClob extends Clob.
|
||||
// The database type is still NCLOB either way and handled as such.
|
||||
private NClob warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-nclob-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public NClob getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(NClob warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-nclob-example[]
|
||||
}
|
||||
//end::basic-nclob-example[]
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.Nationalized;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class NationalizedTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Product.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Integer productId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::basic-nationalized-persist-example[]
|
||||
final Product product = new Product();
|
||||
product.setId( 1 );
|
||||
product.setName( "Mobile phone" );
|
||||
product.setWarranty( "My product warranty" );
|
||||
|
||||
entityManager.persist( product );
|
||||
//end::basic-nationalized-persist-example[]
|
||||
|
||||
return product.getId();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Product product = entityManager.find( Product.class, productId );
|
||||
assertEquals( "My product warranty", product.getWarranty() );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-nationalized-example[]
|
||||
@Entity(name = "Product")
|
||||
public static class Product {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Nationalized
|
||||
private String warranty;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-nationalized-example[]
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getWarranty() {
|
||||
return warranty;
|
||||
}
|
||||
|
||||
public void setWarranty(String warranty) {
|
||||
this.warranty = warranty;
|
||||
}
|
||||
|
||||
//tag::basic-nationalized-example[]
|
||||
}
|
||||
//end::basic-nationalized-example[]
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.userguide.model.PhoneType;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class PhoneTypeEnumeratedOrdinalTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Phone.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::basic-enums-Enumerated-ordinal-persistence-example[]
|
||||
Phone phone = new Phone( );
|
||||
phone.setId( 1L );
|
||||
phone.setNumber( "123-456-78990" );
|
||||
phone.setType( PhoneType.MOBILE );
|
||||
entityManager.persist( phone );
|
||||
//end::basic-enums-Enumerated-ordinal-persistence-example[]
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-enums-Enumerated-ordinal-example[]
|
||||
@Entity(name = "Phone")
|
||||
public static class Phone {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Column(name = "phone_number")
|
||||
private String number;
|
||||
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
@Column(name = "phone_type")
|
||||
private PhoneType type;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-enums-Enumerated-ordinal-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public PhoneType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(PhoneType type) {
|
||||
this.type = type;
|
||||
}
|
||||
//tag::basic-enums-Enumerated-ordinal-example[]
|
||||
}
|
||||
//end::basic-enums-Enumerated-ordinal-example[]
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.basic;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.userguide.model.PhoneType;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class PhoneTypeEnumeratedStringTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Phone.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Phone phone = new Phone( );
|
||||
phone.setId( 1L );
|
||||
phone.setNumber( "123-456-78990" );
|
||||
phone.setType( PhoneType.MOBILE );
|
||||
entityManager.persist( phone );
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::basic-enums-Enumerated-string-example[]
|
||||
@Entity(name = "Phone")
|
||||
public static class Phone {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Column(name = "phone_number")
|
||||
private String number;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "phone_type")
|
||||
private PhoneType type;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
//end::basic-enums-Enumerated-string-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public PhoneType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(PhoneType type) {
|
||||
this.type = type;
|
||||
}
|
||||
//tag::basic-enums-Enumerated-string-example[]
|
||||
}
|
||||
//end::basic-enums-Enumerated-string-example[]
|
||||
}
|
|
@ -53,7 +53,7 @@ public class TypeCategoryTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private boolean starred;
|
||||
|
||||
// getters and setters omitted
|
||||
//Getters and setters are omitted for brevity
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
|
|
|
@ -25,6 +25,7 @@ log4j.logger.org.hibernate.reflection=info
|
|||
log4j.logger.org.hibernate.SQL=debug
|
||||
|
||||
### log JDBC bind parameters ###
|
||||
log4j.logger.org.hibernate.type=trace
|
||||
log4j.logger.org.hibernate.type.descriptor.sql=trace
|
||||
|
||||
### log schema export/update ###
|
||||
|
|
Loading…
Reference in New Issue