1

I have been trying to learn how to connect to the embedded database Apache Derby that comes out of the box with glassfish4. What do I have to set in the src/META-INF/persistence.xml in my project? Is it somehow preconfigured? If not, how can I change its settings - through glassfish console?

Thanks in advance for your help.

Mateusz Chrzaszcz
  • 1,240
  • 14
  • 32

1 Answers1

2

You need to edit the persistence.xml to add the persistence-provider, the classes you want to manage, and some configuration for your database, in case you do not use JTA, in your IDE, and package it with the app. You can generally enter the following in the persistence.xml:

<persistence-unit name="call_it_as_you_want" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>YourClass</class>
    <properties>
      <property name="javax.persistence.jdbc.driver"   value="org.apache.derby.jdbc.ClientDriver" /> 
      <property name="javax.persistence.jdbc.url"      value="jdbc:derby://127.0.0.1:1527/yourDatabase;create=true" />
      <property name="javax.persistence.jdbc.user"     value="your_database_login" />
      <property name="javax.persistence.jdbc.password" value="your_database_password" />
      <property name="eclipselink.ddl-generation"      value="drop-and-create-tables" />
    </properties>
  </persistence-unit>

Before that, check if derby runs on port 1527 - i think it should. If you are going to use JPA with EJBs you can use JTA configuration - in this case persistence.xml will only need to declare the pool you will create from the admin console of Glassfish.

Artem Moskalev
  • 5,748
  • 11
  • 36
  • 55