无法使用Hibernate获取表的列名

时间:2014-02-10 12:11:38

标签: hibernate

我正在使用Hibernate获取表的列名。我创建了一个方法,我将表名传递给该方法执行时我得到了这个例外: 没有JDBC类型的Dialect映射:-1 任何人都可以帮助我。谢谢

在Mycode下面:

道上课:

public class Dao {
        public static void main(String[] args) {
            Dao o=new Dao();
             ArrayList list=o.getTableDesc("examtype");
        System.out.println ("listlistlist"+list);
        }
    public  ArrayList<String>  getTableDesc(String tableName) {
            System.out.println("getFieldNames:start" + tableName);
            Object[] a;
            List<Object[]> fieldNames = new ArrayList<Object[]>();
            ArrayList<String> tabFieldNames = new ArrayList<String>();
            Session ses = HibernateUtil.getSessionFactory().openSession();
            try {
                String queryStr = "DESCRIBE "  + tableName;                 
                fieldNames = (List<Object[]>) ses.createSQLQuery(queryStr).list();
                System.out.println("fieldNamesfieldNames"+fieldNames);
                for (int i = 0; i < fieldNames.size(); i++) {
                    a = fieldNames.get(i);
                    tabFieldNames.add(a[0].toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("exception " + e);
            } finally {
                ses.close();
            }
            System.out.println("getFieldNames:end" + tabFieldNames.toString());
            return tabFieldNames;
        }  
    }

hibernate.cfg.xml中:

    <hibernate-configuration>
        <session-factory>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306treamisdemo</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password"></property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.connection.pool_size">30</property>
            <property name="hibernate.jdbc.batch_size">100</property>
            <!--<property name="hibernate.hbm2ddl.auto">update</property>-->
            <property name="hibernate.connection.isolation">2</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>       
        </session-factory>
    </hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

更改方言的属性名称,如下所示:

名称= “方言” 改为 的名称= “hibernate.dialect”

并且还应该添加映射资源=“相应的实体类(实体类相关,如同Person实体 - > Person.hbm.xml)hbm.xml文件”

并运行它。