如何解决必须提供jdbc连接,但已经给出?

时间:2016-10-12 02:37:52

标签: java hibernate jdbc

我是hibernate的新手。我正在尝试基本的应用程序这里是我的课程,我得到了。我尝试使用hibernate.properties文件,然后我也得到了相同的异常。

>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for i in [0,1,3,5,7,3,0]:
...   d[i] += 1
...
>>> d
defaultdict(<class 'int'>, {0: 2, 1: 1, 3: 2, 5: 1, 7: 1})
>>> for item, count in d.items():
...    print('Mode: {} Count: {}'.format(item, count))
...
Mode: 0 Count: 2
Mode: 1 Count: 1
Mode: 3 Count: 2
Mode: 5 Count: 1
Mode: 7 Count: 1

这是我的HibernateUtil类:

Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
    at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:61)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
    at com.infiniteskills.data.Application.main(Application.java:9)

这是我的主要课程

package com.infiniteskills.data;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.infiniteskills.data.entities.User;

public class HibernateUtil {
  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
      Configuration configuration = new Configuration();
      // configuration.addAnnotatedClass(User.class);
      return configuration.buildSessionFactory(
          new StandardServiceRegistryBuilder()
              .applySettings(configuration.getProperties())
              .build());
    } catch (Exception e){
      e.printStackTrace();
      throw new RuntimeException("There was an error building the factory");
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

配置文件

package com.infiniteskills.data;

import org.hibernate.Session;

public class Application {
  public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    session.close();
  }
}

实体类

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration SYSTEM 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
       <session-factory>

       <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

       <!-- Assume test is the database name -->
       <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ifinances</property>
       <property name="hibernate.connection.username">infinite</property>
       <property name="hibernate.connection.password">skills</property>


       <property name="show_sql">true</property>


       <mapping class="com.infiniteskills.data.entities.User"/>

    </session-factory>
    </hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

将您的Hibernate Util更改为以下,这应该解决您的检测配置文件的问题。

var behavior = new ListViewPageBindingBehavior();
behavior.SetBinding(ListViewPageBindingBehavior.CommandProperty, "LoadMoreLeadOfTheDateCommand");
behavior.Converter = new ItemVisibilityConverter();

myListView.Behaviors.Add (behavior);

并获取SessionFactory&amp;会话对象使用以下代码:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
 private static SessionFactory sessionAnnotationFactory;

 public static SessionFactory getSessionAnnotationFactory() {
    if (sessionAnnotationFactory == null) {
        sessionAnnotationFactory = buildAnnotationSessionFactory();
    }
    return sessionAnnotationFactory;
 }

 private static SessionFactory buildAnnotationSessionFactory() {
    SessionFactory sessionFactory = null;
    try {

        sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return sessionFactory;
 }
}