• Hibernate map enum type


    1. Bean

    @Entity
    @Table(name = "entities")
    public class Entities {
      public enum entityType { 
         location, group; 
      }   
      private entityType locationOrGroup;
    
      @Column(name="location_or_group")
      @Enumerated(EnumType.STRING) //Should set the type of location_or_group to varchar in database
      public entityType getLocationOrGroup() {
    	return locationOrGroup;
      }
      public void setLocationOrGroup(entityType locationOrGroup) {
    	this.locationOrGroup = locationOrGroup;
      }
    
    }
    

    2. Database

    CREATE TABLE entities
    (
      location_or_group varchar
    );
    

    3. Unit Test

      @Test
      public void testEnum() {
    	 Session session = Config.getSessionFactory().openSession();
    	 session.beginTransaction();
    	
    	 Query query = session.createQuery("select locationOrGroup from Entities");
    	
    	 @SuppressWarnings("unchecked")
    	 List<Object> entities = query.list();
    	 System.out.println(entities.get(0));
    	 assertEquals(entities.get(0).toString(), "group");//entities.get(0)here is of type entityType in bean file, should be cast to String
    	 
    	 session.getTransaction().commit();
    	 session.close();
      }
    

      

  • 相关阅读:
    oracle数据段详解
    监听静态注册与动态注册
    Oracle网络相关概念与常用配置文件
    pycharm社区版安装及遇到的问题
    强化学习-K摇臂赌博机
    概率图模型
    半监督学习
    卷积神经网络
    递归神经网络
    玻尔兹曼机及其相关模型
  • 原文地址:https://www.cnblogs.com/codingforum/p/4316989.html
Copyright © 2020-2023  润新知