Mapping of enum types to database fields in the Model class

Source: Internet
Author: User

This thing for a long time, not difficult, but small details did not notice has been wrong, now finally made out, hurriedly write down.

Here is the mapping between the entity class user and the corresponding table user:

1. Prepare the database table user

SET NAMES UTF8;
SET foreign_key_checks = 0;

-- ----------------------------
--Table structure for ' user '
-- ----------------------------
DROP TABLE IF EXISTS ' user ';
CREATE TABLE ' user ' (
' id ' int (one) not NULL auto_increment,
' Name ' varchar (not NULL),
' Gender ' int (3) is not NULL,
' Level ' int (1) is not NULL,
' Birth ' timestamp not NULL the DEFAULT current_timestamp on UPDATE current_timestamp,
' is_locked ' bit (1) not NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=2 DEFAULT Charset=utf8;

-- ----------------------------
--Records of ' user '
-- ----------------------------
BEGIN;
INSERT into ' user ' VALUES (' 1 ', ' C1 ', ' 1 ', ' 0 ', ' 2016-02-28 10:44:34 ', b ' 0 ');
COMMIT;

SET foreign_key_checks = 1;

2. Model class User

Package Pr.cgl.model;
Import javax.persistence.*;
/**
* Created by LL on 16/1/25.
*/
@Entity
@Table (name = "User")
public class User {
Private Integer ID;

private String name;

Private Boolean isLocked;

public enum gender{
MALE,
FEMALE
}

public enum level{
Cardmember,//0
Goldcardmember,//1
Platinumcardmember,//2
}

Private Gender Gender;

private level level;

@Id
@GeneratedValue (strategy = Generationtype.auto)
Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

@Column (name = "Name")
Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

@Column (name = "is_locked")
Public Boolean getislocked () {
return isLocked;
}

public void setislocked (Boolean isLocked) {
this.islocked = isLocked;
}

@Column (name = "Gender")
Public Gender Getgender () {
return gender;
}

public void Setgender (Gender Gender) {
This.gender = gender;
}

@Column (name= "level")
Public level Getlevel () {
return level;
}

public void SetLevel (level level) {
This.level = level;
}
}
3. Test class

Package pr.cgl.test;
Import Pr.cgl.model.User;
Import Pr.cgl.util.EntityUtil;

Import Javax.persistence.EntityManager;
Import Javax.persistence.Query;
Import java.util.List;

/**
* Created by LL on 16/1/25.
*/
public class Enumtest {
public static void Main (string[] args) {
Entitymanager em = entityutil.em;
String jql = "from User u";
Query query = em.createquery (JQL);
List<user> userlist = Query.getresultlist ();
for (User u:userlist) {
System.out.println ("id=" +u.getid () + "Name=" +u.getname () + "is_locked=" +u.getislocked () + "gender=" +u.getgender () + " Level= "+u.getlevel ());
}
}
}
4. Output results

Hibernate:select user0_.id as id0_, User0_.gender as gender0_, user0_.is_locked as is3_0_, user0_.level as level0_, User0 _.name as name0_ from user user0_
Id=1 name=c1 is_locked=false Gender=female level=cardmember

Mapping of enum types to database fields in the Model class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.