高分答谢,Hibernate动态创建表语句,路过有分(散分贴)。。。
我想使用Hql动态创建一个表(最好也能生成相应的类文件和xml文件里的属性),我现在使用SQLQuery(原生SQL语句)可以动态的生成表,但是会报一个异常,可能与我用SQLQuery.list()方法的关系,但是表确实可以生成,语句如下:
SQLQuery query = session.createSQLQuery("create table test(name varchar(20),pass varchar(20))");
query.list();
这样虽然能生成表,但不能生成映射类,还报异常,希望能用Hql解决,和答复这个异常的原因,高分相赠。。。
异常如下
Hibernate: create table test(name varchar(20),pass varchar(20))
2008-1-28 10:50:43 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet MServlet threw exception
org.hibernate.HibernateException: Exception while trying to autodiscover types.
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:153)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1678)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
at MServlet.doGet(MServlet.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Caused by:
java.sql.SQLException: ORA-01003: 语句未进行语法分析
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.v8Odscrarr.receive(v8Odscrarr.java:205)
at oracle.jdbc.ttc7.TTC7Protocol.describe(TTC7Protocol.java:754)
at oracle.jdbc.driver.OracleStatement.describe(OracleStatement.java:6195)
at oracle.jdbc.driver.OracleResultSetMetaData.<init>(OracleResultSetMetaData.java:55)
at oracle.jdbc.driver.OracleResultSetImpl.getMetaData(OracleResultSetImpl.java:152)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:122)
... 25 more
------解决方案--------------------路过帮顶!
------解决方案--------------------我觉得的吧,生成xml文件好办,因为你动态生成的表肯定格式类似吧,所以xml你可以先写一个,然后用文件操作将其中的特定文字改变成你对应的表和其中的字段就OK了,至于HQL创建表,你可以看看HQL的文档,我也是刚用hibernate,不怎么清楚阿……
------解决方案--------------------建表,你这样写看:
HibernateUtil.setup("create table EVENTS ( uid int, name VARCHAR, start_Date
date, duration int, location_id int);");
HibernateUtil.setup("create table locations ( uid int, name VARCHAR, address