java—使用带有hibernate的属性文件连接到mysql数据库

2hh7jdfx  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(300)

我看了各种类似的问题,但没有找到一个直接击中什么需要寻求帮助。
因此,以前我在hibernate xml配置文件中保存了所有数据库连接的详细信息,如下所示(此时的所有内容都按预期工作,因此我只包含更改的行)

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">URL</property>
    <property name="connection.username">USERNAME</property>
    <property name="connection.password">PASSWORD</property>

我把这些台词改成

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

我获取了这些属性,并将它们放在属性文件dbconnection.properties中

connection.driver_class=com.mysql.jdbc.Driver
connection.username=USERNAME
connection.password=PASSWORD
connection.url=URL

我的应用程序中有一个方法,它构建并返回sessionfactory

private SessionFactory getSessionFactory(){
         Properties dbConnectionProperties = new Properties();

         try {
            dbConnectionProperties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("dbConnection.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }

         SessionFactory factory = new Configuration()
                    .mergeProperties(dbConnectionProperties)
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(EmailMessage.class)
                    .buildSessionFactory();

        return factory;

以及使用会话工厂的方法

public void PostUnpostedMessageToFacebook(){

        SessionFactory factory = getSessionFacto

ry();
    System.out.println(factory.getProperties());
    // create a session 
    Session session = factory.getCurrentSession();
    session.beginTransaction();
  ****some more code let me know if you want to see it but I didn't think it was relevant as this is where the error is thrown****

在我做出这些改变之前,一切都按预期进行。但是,现在我收到下面的错误。
你能帮助我理解为什么现在抛出这个错误吗?因为我不太明白如何或者为什么不再返回jdbc连接。
非常感谢

Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
    at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44)
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:115)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:145)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:263)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:271)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)
    at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:56)
    at org.hibernate.internal.AbstractSharedSessionContract.beginTransaction(AbstractSharedSessionContract.java:409)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
    at com.sun.proxy.$Proxy23.beginTransaction(Unknown Source)
    at com.post2facebook.postToFacebookApp.GmailToFacebook.PostUnpostedMessageToFacebook(GmailToFacebook.java:80)
    at com.post2facebook.postToFacebookApp.MainApp.gmailToFacebookOnlyDemo(MainApp.java:16)
    at com.post2facebook.postToFacebookApp.MainApp.main(MainApp.java:11)
yzuktlbb

yzuktlbb1#

谢谢你的帮助,作为对我自己和其他用户的一个提醒,不要认为以前有用的东西仍然有效。我的问题是用于数据库连接的url。
当这个url在xml文件中时,它如下所示 jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC 但是由于某种原因,当从属性文件获取这个时,处理amp;和;所以我把它们删除了

hibernate.connection.url=jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC

谢谢

相关问题