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:
为了建立数据库connectioin,我创建了一个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
…并且包含驱动程序.jar: db2jcc4.jar
When running the app I get the following error:
运行app时,出现以下错误:
java.sql.SQLSyntaxErrorException: Schema 'DB2ADMIN' does not exist.
So did I miss something essential?
我错过了什么重要的东西吗?
Thanks in advance!
提前谢谢!
1 个解决方案
#1
2
Short Answer: you need to specify default DB schema name in persistence.xml.
简短的回答:您需要在persistence.xml中指定默认的DB模式名。
…
<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.
说明:很可能您正在使用DB2ADMIN用户连接到DB。DB2中的数据库模式名(如果没有显式指定的话)等于用户名,即DB2ADMIN。这样的模式并不存在于数据库中,因此您已经得到了错误。
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.
您需要在JPA配置中指定适当的数据库模式名,即表所在的模式名。这里没有要存档的JPA属性,但是您正在使用hibernate,所以您可以使用hibernate特定的属性来实现这一点。
#1
2
Short Answer: you need to specify default DB schema name in persistence.xml.
简短的回答:您需要在persistence.xml中指定默认的DB模式名。
…
<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.
说明:很可能您正在使用DB2ADMIN用户连接到DB。DB2中的数据库模式名(如果没有显式指定的话)等于用户名,即DB2ADMIN。这样的模式并不存在于数据库中,因此您已经得到了错误。
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.
您需要在JPA配置中指定适当的数据库模式名,即表所在的模式名。这里没有要存档的JPA属性,但是您正在使用hibernate,所以您可以使用hibernate特定的属性来实现这一点。