Home > Back-end >  How to use Enum in the Entity?
How to use Enum in the Entity?

Time:02-15

Recently in the "Spring of actual combat", see chapter 3, using JPA,
In the class definition using the @ the Entity annotation can be automatically according to the attributes of a class to create the corresponding table, and can easily read and write database,
I use Database dependency is H2 Database,
I want to use a custom Enum as Entity class attribute, but in the discovery database console class Enum field in the database in the corresponding field is of type INT, if I am to write data in the database is a String type, because of the type String cannot be converted to INT, write failure,
I hope I can write to the database Enum name corresponding type String data, rather than an INT type data, how to do?
Below is a code,
Ingredient. Java
 package tacos. 
The import javax.mail. Persistence. The Entity;
The import lombok. AccessLevel;
The import lombok. Data;
The import lombok. NoArgsConstructor;
The import lombok. RequiredArgsConstructor;

@ Data
@ RequiredArgsConstructor
@ NoArgsConstructor (access=AccessLevel. PRIVATE force=true)
@ the Entity
Public class Ingredient {
Private final Type Type;

Public static enum Type {
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}

IngredientRepository. Java
 package tacos. Data; 
The import org. Springframework. Data. The repository. CrudRepository;
The import org. Springframework. Data. The repository. NoRepositoryBean;
The import tacos. Ingredient;

@ NoRepositoryBean
Public interface IngredientRepository
Extends CrudRepository {
Iterable The.findall ();

@ SuppressWarnings (" unchecked ")
Ingredient save (Ingredient Ingredient);
}

JdbcIngredientRepository. Java
 package tacos. Data; 
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Import the Java. Util. Optional;
import org.springframework.beans.factory.annotation.Autowired;
The import org. Springframework. JDBC. Core. JdbcTemplate;
The import org. Springframework. Stereotype. The Repository;
The import tacos. Ingredient;
The import tacos. Err. UnImplementError;

@ Repository
Public class JdbcIngredientRepository implements IngredientRepository {
Private JdbcTemplate JDBC.

The @autowired
Public JdbcIngredientRepository (JdbcTemplate JDBC) {
Enclosing the JDBC=JDBC;
}

@ Override
Public Iterable The.findall () {
Return the JDBC. Query (" select id, name, type the from Ingredient ",
This: : mapRowToIngredient);
}

@ SuppressWarnings (" unchecked ")
@ Override
Public Ingredient save (Ingredient Ingredient) {
JDBC. Update (" insert into Ingredient (id, name, type) values (?,?,?,?,? ,? ,?) "
Ingredient. GetId (),
Ingredient. The getName (),
Ingredient. GetType (). The toString ());
Return ingredient;
}

Private Ingredient mapRowToIngredient (ResultSet rs, int rowNum)
Throws SQLException {
The return of new Ingredient (
Rs. Get string (" id "),
Rs. Get string (" name "),
Ingredient. Type. The valueOf (rs. Get string (" Type "))
);
}

@ Override
The public & lt; S extends Ingredient> Iterable SaveAll (Iterable Entities) {
Throw new UnImplementError ();
}

@ Override
Public Optional FindById (String id) {
Throw new UnImplementError ();
}

@ Override
Public Boolean existsById (String id) {
Throw new UnImplementError ();
}

@ Override
Public Iterable FindAllById (Iterable Ids) {
Throw new UnImplementError ();
}

@ Override
Public long count () {
Throw new UnImplementError ();
}

@ Override
Public void deleteById (String id) {
Throw new UnImplementError ();
}

@ Override
Public void the delete (Ingredient entity) {
Throw new UnImplementError ();
}

@ Override
Public void deleteAll (Iterable<? Extends Ingredient> Entities) {
Throw new UnImplementError ();
}

@ Override
Public void deleteAll () {
Throw new UnImplementError ();
}
}

-- -- -- -- -- -- -- -- -- -- - update: the problem is resolved, -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
We can use @ Column annotation Ingredient type field, the value of the annotation of columnDefinition can write this field in the database data definition of the corresponding field (DDL), here is the Ingredient. Java the modified part,
 package tacos. 

The import javax.mail. Persistence. The Column;

@ the Entity
Public class Ingredient {
@ the Column (columnDefinition="varchar (255) not null")
Private final Type Type;
}
  • Related