DB2, Hibernate, JPA: Schema does not exist

2.3k views Asked by At

I'm quite new to the subject and I'd like to know what is wrong with what I've done so far.

So to establish the database connectioin I created a persistence.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="Primary">
        <class>xxx.model.Lecture</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="javax.persistence.jdbc.url"    value="jdbc:db2://localhost:50000/xxx" />
            <property name="javax.persistence.jdbc.user" value="xxx" />
            <property name="javax.persistence.jdbc.password" value="xxx" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
            <property name="show_sql" value="true"/>
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

... and included the driver .jar: db2jcc4.jar

When running the app I get the following error:

java.sql.SQLSyntaxErrorException: Schema 'DB2ADMIN' does not exist.

So did I miss something essential?

Thanks in advance!

1

There are 1 answers

0
Alexander Pranko On

Short Answer: you need to specify default DB schema name in persistence.xml.

…
<property name="hibernate.default_schema" value="xxx"/>
…

Explanation: Most probably you are using DB2ADMIN user to connect to the DB. The database schema name in DB2 (if not explicitly specified) is equal to user name, i.e. DB2ADMIN. Such schema doesn't exist in your DB therefore you've got the error.

You need to specify proper database schema name in JPA config, i.e. schema name where the tables are located. There is no JPA property to archive this but you are using hibernate anyway, so you can use hibernate specific property to achieve this.