简体   繁体   中英

SQLite, NetBeans, EclipseLink and JPA - NetBeans keeps starting Apache Derby instead of SQLite DB

I'm struggling with NetBeans and failing to make a project (Vaadin -> Maven -based) that connects to an SQLite database. For some reason every time I run my project:

  • NetBeans starts an Apache Derby database
  • JPA doesn't generate tables in my SQLite table (probably since I never manage to connect to it)

I have done the following:

  1. "Install" the SQLite JDBC driver - downloaded the jar, went to Services -> Database and created a new connection pointing at the jar. I've named it SQLite 3.8.11.2 . It has been successfully added and listed under Databases -> Drivers .
  2. Adding a connection to the database - the database is created using the command line tool and is located inside the root directory of the NetBeans Maven project. Currently it is empty since I want to use JPA to generate my tables from the respective entity classes. I added the connection and it points to the cookbook.sqlite file. In order to check if things are working at this stage I created a small table using NetBeans' user interface, added a bunch of stuff and then using the terminal checked if the database file has changed and what it contains. No problems here.
  3. Resolve Maven dependecies - to the <dependencies/> inside the pom.xml of my project I added:

     <dependencies> <!-- various other dependencies mostly related to Vaadin --> ... <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId> <version>2.5.2</version> <scope>provided</scope> </dependency> </dependencies> 
  4. Created a persistence unit - I have 3 entity classes - User , Ingredient and Recipe . The generated persistence.xml contains:

     <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="CookbookPU" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <non-jta-data-source>cookbook_db</non-jta-data-source> <class>com.ava.cookbook.models.User</class> <class>com.ava.cookbook.models.Ingredient</class> <class>com.ava.cookbook.models.Recipe</class> <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/> <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/PATH_TO_PROJECT_ROOT/cookbook_db.sqlite"/> <property name="javax.persistence.jdbc.user" value="cookbook"/> <property name="javax.persistence.jdbc.password" value="cookbook"/> <property name="eclipselink.logging.level" value="FINEST"/> </properties> </persistence-unit> </persistence> 

Note that the schema generation is set to drop-and-create .

Basically these are my settings. When I try to run the project in the output window four new tabs appear:

  • Run (Cookbook) - this one tells me information about the state of the running project (since I haven't started actually accessing the database (which I cannot connect to :3) it runs perfectly fine)
  • GlassFish Server 4.1 - information about the GlassFish server that the project is running on
  • Browser Log - I'm using the NetBeans connector and while running the Vaadin project inside Google Chrome this tab displays a bunch of Vaadin-related events
  • Java DB Database Process - this one is the bummer in all this. In detail the messages here are:

     Fri Oct 09 15:02:47 CEST 2015 : Security manager installed using the Basic server security policy. Fri Oct 09 15:02:48 CEST 2015 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1527 

Before I saw that I was thinking that I've screwed things up with the whole JPA-Entity-thing but after I noticed the second message in particular it became obvious that the project not connecting to the SQLite database but running it's own Derby DB.

I have barely any experience with Maven, EclipseLink, JPA and NetBeans so it is quite possible I'm overlooking something. Some advice or even a solution would be much appreciated.

If the connection is already setup incorrectly.

  1. Use the Netbeans Menu : Windows -> Services to show the services tab.
  2. Expand Servers
  3. Expand GlassFish Server
  4. Expand Resources
  5. Expand JDBC
  6. Expand JDBC Resources
  7. Select your resource name.
  8. Right-click for properties. Note the Pool Name
  9. Expand Connection Pools
  10. Select the Pool you just noted.
  11. Right-click for properties.
  12. Edit properties to use sqlite instead of derby. URL should be jdbc:sqlite:<path to cookbook.sqlite>

If you haven't setup the resource.

  1. Select your project.
  2. Right-click for New -> Other
  3. In the Dialog select Category = Glassfish, File Type = JDBC Resource
  4. Select Create New JDBC Connection Pool.
  5. Change the name to your resources name
  6. Click Next Twice
  7. Set the Connection Pool name to something that you can remember.
  8. Set the Data Source CLass Name to anything you want.
  9. Set the Resource Type javax.sql.ConnectiPoolDataSource
  10. Click Next
  11. Check the settings on the last screen of the dialog and then click Finish.

To create classes mapped to tables:

  1. Select your project.
  2. Right-click for New -> Other
  3. In the Dialog select Category = Persistance, File Type = Entity Class
  4. Change Class name and Package Name

There are some tutorials that would probably be worth while to quickly work through even though they also cover some irrelevant material they are not that long.

Getting Started with Java EE Applications
Creating a Simple Web Application Using a MySQL Database

and/or

Take a look at the sample, at File > New Project > Samples > Java EE > Web JPA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM