zoukankan      html  css  js  c++  java
  • 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();
      }
    

      

  • 相关阅读:
    订单生成案例详解
    分页案例详解
    简单的多条件查询案例
    删除选中案例详解
    转账汇款案例
    登录操作记住用户名实现
    根据自定义异常来回显错误信息
    会话技术cookie和session详解
    JDBC
    Netty入门教程——认识Netty
  • 原文地址:https://www.cnblogs.com/codingforum/p/4316989.html
Copyright © 2011-2022 走看看