当前位置: 代码迷 >> Java Web开发 >> 怎么设置Hibernate不使用PreparedStatement
  详细解决方案

怎么设置Hibernate不使用PreparedStatement

热度:5541   发布时间:2013-02-25 21:12:43.0
如何设置Hibernate不使用PreparedStatement?
我使用Hibernate查询数据库出现参数个数超过了2000个的问题,应该是JDBC驱动不支持超过2000个参数的SQL语句,请问如何设置Hibernate不使用PreparedStatement的查询方式,或者如何解决参数个数超过2000个的问题呢?
Hibernate版本:3.2
数据库:SQL Server 2000
JDBC驱动:jtds 1.2

日志如下:
2008-09-23 10:25:23,660 DEBUG [org.hibernate.util.JDBCExceptionReporter] could not execute query [select this_.ID as ID18_0_, this_.GUID as GUID18_0_, this_.User_Name as User3_18_0_, this_.Log_Time as Log4_18_0_, this_.Host_IP as Host5_18_0_, this_.Policy_Name as Policy6_18_0_, this_.Policy_Status as Policy7_18_0_ from My_Feedback_log this_ where this_.User_Name in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (帖子过长,省略若干问号)?, ?) and this_.Log_Time between ? and ?]
java.sql.SQLException: Prepared or callable statement has more than 2000 parameter markers.
at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:1139)
at net.sourceforge.jtds.jdbc.SQLParser.parse(SQLParser.java:156)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.<init>(JtdsPreparedStatement.java:104)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2221)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.prepareStatement(ConnectionJDBC2.java:2179)
at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:213)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:497)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:415)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)


------解决方案--------------------------------------------------------
userName 是数据库的字段
------解决方案--------------------------------------------------------
可以直接 使用 Hibernate 的 JDBC Session Factory , 配置文件利用 这个 
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


Java code
public class HibernateInit    implements ServletContextListener {  public void contextDestroyed(ServletContextEvent servletContextEvent) {  }  public void contextInitialized(ServletContextEvent servletContextEvent) {    try {      Configuration conf = new Configuration().configure();      SessionFactory sf = conf.buildSessionFactory();    }    catch (Exception e) {      e.printStackTrace();    }  }}
------解决方案--------------------------------------------------------
你这样写超级慢,性能 不好,为什么不考虑换种实现方式呢 ?
  相关解决方案