Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

postgresql Java Hibernate:无法创建请求的服务[org.Hibernate.engine.jdbc.env.spi.JdbcEnvironment]

对不起,我的英语不好

现在我在MySQL上测试Hibernate还可以,但在PostgreSQl上我遇到了问题(因为我连接了Heroku DB)。 我试图连接到DB,但它会引发异常(我已经检查了hibernate.cfg.xml中的所有属性):

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:postgres://username:password@host:5432/database]
java.lang.NullPointerException: Cannot invoke "org.hibernate.SessionFactory.openSession()" because the return value of "ua.sillmarry.ArtToolsBot.util.HibernateUtil.getSessionFactory()" is null

这是我的冬眠。cfg。xml:

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

<hibernate-configuration>

<session-factory>

    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgres://username:password@host:5432/database</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>
    <property name="connection.pool_size">1</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
    <property name="show_sql">true</property>
    <property name="current_session_context_class">thread</property>
    <property name="hbm2ddl.auto">validate</property>
    <property name="hibernate.dbcp.initialSize">5</property>
    <property name="hibernate.dbcp.maxTotal">20</property>
    <property name="hibernate.dbcp.maxIdle">10</property>
    <property name="hibernate.dbcp.minIdle">5</property>
    <property name="hibernate.dbcp.maxWaitMillis">-1</property>

    <mapping class="ua.sillmarry.ArtToolsBot.entity.Brush" />

</session-factory>

</hibernate-configuration>

类HibernateSessionFactoryUtil:

package ua.sillmarry.ArtToolsBot.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class HibernateSessionFactoryUtil {

    private static StandardServiceRegistry registry;
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {

                registry = new StandardServiceRegistryBuilder().configure().build();
                MetadataSources sources = new MetadataSources(registry);
                Metadata metadata = sources.getMetadataBuilder().build();
                sessionFactory = metadata.getSessionFactoryBuilder().build();
            } catch (Exception e) {

                e.printStackTrace();
                if (registry != null) {

                    StandardServiceRegistryBuilder.destroy(registry);
                }
            }
        }
        return sessionFactory;
    }

    public static void shutdown() {
        if (registry != null) {
            StandardServiceRegistryBuilder.destroy(registry);
        }

    }
}

类笔刷(映射):

package ua.sillmarry.ArtToolsBot.entity;

import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
@Table(name = "brushes")
public class Brush implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "brushes")
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "category")
    private String category;

    @Column(name = "gd_brush")
    private String gd_brush;

    @Column(name = "gd_img")
    private String gd_img;

    public String toString() {

        return "Brush{\n" +
            "id=" + id + ";\n" +
            "name='" + name + "';\n" +
            "category='" + category + "';\n" +
            "gd_brush='" + gd_brush + "';\n" +
            "gd_image='" + gd_img + "'.";
    }
}

共 (0) 个答案