org.hibernate.exception.JDBCConnectionException:无法打开连接,hibernate,h2

时间:2015-04-24 12:47:31

标签: java hibernate jpa h2

我正在使用hibernate和h2数据库制作示例应用程序。我有一个实体类(实际上超过1,但让它开始使它适用于其中一个),看起来像这样:

package ~path.examples.testjpa.domain;

import javax.persistence.*;
import java.util.*;

/**
 * Created by Me on 2015-04-20.
 */
@Entity
@Table(name= "Persons")
public class Person {
    @Id
    @Column(nullable= false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected int persId;
    @Column(nullable= false, length = 50)
    private String firstName;
    @Column(nullable= false, length = 50)
    private String surname;
    @Column(nullable= true, length = 50)
    private String emailAddress;
    @OneToOne
    private Account account;
    @OneToMany(targetEntity = PhoneNumber.class)
    List phoneNumberList;
    public Person() {
        // empty
    }
    public Person(int persId, String firstName, String surname, String emailAddress) {
        this.persId = persId;
        this.firstName = firstName;
        this.surname = surname;
        this.emailAddress = emailAddress;
    }
    public List getPhoneNumberList() {
        return phoneNumberList;
    }
    public void setPhoneNumberList(List<PhoneNumber> phoneNumberList) {
        this.phoneNumberList= phoneNumberList;
    }
    public int getPersId() {
        return persId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
    @Override
    public String toString() {
        return "Person [persId=" + persId + ", firstName=" + firstName + ", surname=" + surname + ", emailAddress=" + emailAddress + "]";
    }
}

这是我的persistance.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

        <persistence-unit name="jpaTest" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:~/test" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.connection.password" value="" />
            <property name="schemaUpdate" value="true" />


        </properties>
    </persistence-unit>
</persistence>

和jpaTest类,应该建立连接

package ~path.examples.service;

import ~path.examples.testjpa.domain.Person;

import javax.persistence.*;

/**
 * Created by Me on 2015-04-22.
 */
public class JpaTest {
    public static void main(String args[]) {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpaTest");

        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction userTransaction = em.getTransaction();
        userTransaction.begin();
        Person person = new Person();
        person.setFirstName("Charles");
        person.setSurname("Dickens");
        em.persist(person);
        userTransaction.commit();
        em.close();
        entityManagerFactory.close();
    }
}

当我尝试运行它时,我得到以下内容:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1371)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
    at eu.rivetgroup.examples.service.JpaTest.main(JpaTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: org.hibernate.exception.JDBCConnectionException: Could not open connection
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1263)
    at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)
    ... 6 more
Caused by: java.sql.SQLException: No suitable driver found for jdbc:h2:~/test
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
    ... 11 more

我的猜测是,我错过了一些明显的东西,因为我是一个没有经验的用户。 但是,我看了所有可以找到的地方,到目前为止似乎没有任何答案可以帮助我 谢谢你的帮助!

0 个答案:

没有答案