在Netbeans上使用GlassFish和Derby设置Jakarta EE时遇到问题。

huangapple go评论60阅读模式
英文:

Having problems setting up Jakarta EE on Netbeans using GlassFish and Derby

问题

我知道这是相当过时的技术,但我发现自己需要使用GlassFish上的Jakarta EE来完成一个项目。我已经知道答案,只是想记录一下我解决这些技术组合的问题的方法,因为修复可能出现的所有问题的信息都散布在许多不同的帖子和网站中。

首先,我将列出问题,然后我将提供解决方案:

  1. 我遇到的第一个问题是,IntelliJ要求您支付订阅费才能使用Jakarta EE功能,而Eclipse IDE已将其GlassFish插件存档,因此我最终使用了Netbeans,因为它似乎是唯一仍然支持GlassFish的IDE。

  2. 然后,在下载Apache Derby(10.16.1.1)以设置数据库服务器之后,我收到了以下异常:

在创建数据库时发生错误:
java.lang.ClassNotFoundException:org.apache.derby.jdbc.Clientdriver
  1. 在解决了缺少类的问题后,我尝试将任何内容提交到数据库时都会被设置为回滚,无法将任何内容持久化到数据库中。这是我遇到的异常:
javax.persistence.RollbackException:事务“已回滚”,因为事务已设置为RollbackOnly。
  1. 最后,在事务最终没有回滚时,我遇到了一个错误,因为显然我尝试写入的表在数据库中并不存在。尽管我可以在Derby服务器中清楚地看到它,并且persistence.xml文件正确指向它:
java.sql.SQLSyntaxErrorException:表/视图'CUSTOMER'不存在。
英文:

I know it's pretty outdated technology but I found myself in the need of doing a project using Jakarta EE on GlassFish. I already know the answer, I just want to document my workaround for making this combination of technologies work together, since the information to fix all the problems that may arise is scattered through many different posts and websites.

First I will list the issues and then I will reply with the solutions:

  1. First problem I encountered is that IntelliJ asks you to pay a subscription in order to use Jakarta EE functionality and Eclipse IDE has archived their plugins for GlassFish, so I ended up using Netbeans because it's the only IDE that still supports GlassFish apparently.

  2. Then, after downloading Apache Derby (10.16.1.1) for setting up the database server I was receiving this exception:

An error occurred while creating the database:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.Clientdriver
  1. After solving the missing class issue, every commit I tried to do to the database was being set to rollback, being unable to persist anything into the database. This is the exception I was getting:
javax.persistence.RollbackException: Transaction "rolled back" because transaction was set to RollbackOnly.
  1. Finally, when the transaction was finally not getting rolled back, I was getting an error because apparently the table I was trying to write into didn't exist. Even though I could clearly see it in the Derby server and the persistence.xml file pointed to it correctly:
java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.

答案1

得分: 1

这是我用来解决所有这些问题的解决方法:###

> 2. 接着,在下载了Apache Derby(10.16.1.1)用于设置数据库服务器后,我遇到了这个异常:
> > 在创建数据库时发生错误: > java.lang.ClassNotFoundException: org.apache.derby.jdbc.Clientdriver >

基本上是库中的一个类放在了错误的包中。我只需复制db-derby-10.16.1.1-bin\lib\derbytools.jar\org\apache\derby中的jdbc文件夹,并将其粘贴到db-derby-10.16.1.1-bin\lib\derbyclient.jar\org\apache\derby中。


> 3. 在解决了缺少类的问题后,我尝试对数据库进行的每次提交都被设置为回滚,无法将任何内容持久化到数据库中。这是我得到的异常:
> > javax.persistence.RollbackException: Transaction "rolled back" because transaction was set to RollbackOnly. >

这里的问题是在创建persistence.xml文件时,与其使用Jakarta库来设置持久化单元属性,不如使用旧的javax库,造成了不兼容性。将所有的javax改为jakarta。此外,您可能需要将transaction-type设置为JTA

<persistence-unit name="customersDatabase" transaction-type="JTA"> 
  <exclude-unlisted-classes>false</exclude-unlisted-classes> 
  <properties> 
    <property name="jakarta.persistence.jdbc.url" value="jdbc:derby://localhost:1527/customers"/> 
    <property name="jakarta.persistence.jdbc.user" value="root"/> 
    <property name="jakarta.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/> 
    <property name="jakarta.persistence.jdbc.password" value="root"/> 
    <property name="jakarta.persistence.schema-generation.database.action" value="create"/> 
  </properties> 
</persistence-unit> 

> 4. 最终,在事务最终不再被回滚时,我遇到了一个错误,因为显然我试图写入的表不存在。尽管我可以在Derby服务器中清楚地看到它,而且persistence.xml文件也正确地指向它:

> > java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist. >

这是花费我更多时间的问题,我简直要疯了。在persistence.xml文件中,我们声明了要连接到哪个数据库。然而,在GlassFish服务器域的domain-name\config文件夹中还有一个名为domain.xml的文件,它也设置了数据库连接并优先于持久化文件。它建立了对三个不同数据库的连接,我们必须更改名为DerbyPool的那一个:

<jdbc-connection-pool is-isolation-level-guaranteed="false" datasource-classname="org.apache.derby.jdbc.ClientDataSource" name="DerbyPool" res-type="javax.sql.DataSource"> 
  <property name="PortNumber" value="1527"></property> 
  <property name="Password" value="root"></property> 
  <property name="User" value="root"></property> 
  <property name="serverName" value="localhost"></property> 
  <property name="DatabaseName" value="customers"></property> 
  <property name="connectionAttributes" value=";create=true"></property> 
</jdbc-connection-pool> 

就是这样!如果您正确安装了所有内容并解决了这些问题,您的GlassFish + Derby服务器应该正在运行,并与Netbeans同步,所有对数据库的查询都应该正常工作。

英文:

Here's the workaround that I used to solve all these issues:

> 2. Then, after downloading Apache Derby (10.16.1.1) for setting up the database server I was receiving this exception:
>
> An error occurred while creating the database:
> java.lang.ClassNotFoundException: org.apache.derby.jdbc.Clientdriver
>

Basically the library had a class in the wrong package. I just copied the jdbc folder located in db-derby-10.16.1.1-bin\lib\derbytools.jar\org\apache\derby and pasted it in db-derby-10.16.1.1-bin\lib\derbyclient.jar\org\apache\derby.


> 3. After solving the missing class issue, every commit I tried to do to the database was being set to rollback, being unable to persist anything into the database. This is the exception I was getting:
>
> javax.persistence.RollbackException: Transaction "rolled back" because transaction was set to RollbackOnly.
>

Here the problem was that when the persistence.xml file was created, instead of using the jakarta library for the persistence-unit properties, it was using the old javax library, creating an incompatibility. Change all the javax for jakarta. Also, you may need to set the transaction-type to JTA.

<persistence-unit name="customersDatabase" transaction-type="JTA">
  <exclude-unlisted-classes>false</exclude-unlisted-classes>
  <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/customers"/>
    <property name="javax.persistence.jdbc.user" value="root"/>
    <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="javax.persistence.jdbc.password" value="root"/>
    <property name="javax.persistence.schema-generation.database.action" value="create"/>
  </properties>
</persistence-unit>

> 4. Finally, when the transaction was finally not getting rolled back, I was getting an error because apparently the table I was trying to write into didn't exist. Even though I could clearly see it in the Derby server and the persistence.xml file pointed to it correctly:
>
> java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.
>

This is the one that took me more time, I was just going crazy. In the persistence.xml file we stated which database we want to connect to. However, there's a file called domain.xml inside the GlassFish server domain's domain-name\config folder which also sets up a database connection and dominates over the persistence file. It stablishes a connection to three different databases, we have to change the one named DerbyPool:

<jdbc-connection-pool is-isolation-level-guaranteed="false" datasource-classname="org.apache.derby.jdbc.ClientDataSource" name="DerbyPool" res-type="javax.sql.DataSource">
  <property name="PortNumber" value="1527"></property>
  <property name="Password" value="root"></property>
  <property name="User" value="root"></property>
  <property name="serverName" value="localhost"></property>
  <property name="DatabaseName" value="customers"></property>
  <property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>

And that's it! If you installed everything correctly and fixed these issues your GlassFish + Derby server should be running and synchronized with Netbeans and all queries to the database should be working properly.

huangapple
  • 本文由 发表于 2023年7月10日 19:25:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定