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(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。