码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate中调用带有underscore的Column Name

时间:2015-03-05 23:31:11      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

Hibernate中默认的NamingStrategy不支持调用带有下划线的column name。在hibernate的bean中必须使用camel case。使用ImprovedNamingStrategy可以使hibernate成功map到带有下划线的column name。代码如下:

1. Configure文件

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
          Configuration configuration = new Configuration().configure();
          System.setProperty("hibernate.connection.password", DEFAULT_PASSWORD);
          System.setProperty("hibernate.connection.username", DEFAULT_USER);
          System.setProperty("hibernate.connection.driver_class", DEFAULT_DRIVER_CLASS);
          System.setProperty("hibernate.connection.url", DEFAULT_URI);
          System.setProperty("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
          configuration.setProperties(System.getProperties());
          sessionFactory = configuration.buildSessionFactory();
        }
        return sessionFactory;
    } 

2. Bean

@Entity
@Table(name = "version")
public class Version {
  private int versionId;
  @Id
  @Column(name="version_id")
  public int getVersionId() {
	return versionId;
  }
  public void setVersionId(int versionId) {
	this.versionId = versionId;
  }
}

3. Unit Test

@Test
public void testVersionBean() {
	 Session session = Config.getSessionFactory().openSession();
	 session.beginTransaction();
	
	 Query query = session.createQuery("select versionId from Version");
	
	 @SuppressWarnings("unchecked")
	 List<Object> version = query.list();
	 assertEquals(version.get(0), 1);
	 
	 session.getTransaction().commit();
	 session.close();
}

  

Hibernate中调用带有underscore的Column Name

标签:

原文地址:http://www.cnblogs.com/codingforum/p/4316846.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!