1

I have a database using HSQLDB, and my persistence.xml file is not creating the tables as I would expect. Here is my persistence.xml code:

<?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="DanPfeiffer-SE452-ProjectPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>java.user.jpa.User</class>
    <class>java.score.jpa.Score</class>
    <class>java.teeBox.jpa.TeeBox</class>
    <class>java.course.jpa.Course</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>


    <properties>
      <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbc.JDBCDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:hsql://localhost/mydb"/>
      <property name="javax.persistence.jdbc.user" value="SA"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create-tables"/>
      <property name="eclipselink.deploy-on-startup" value="true" />    
    </properties>
  </persistence-unit>

</persistence>

I have a few Java Beans written with @Entity annotation. Here's the simplest of those beans.

package main.java.user.jpa;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import main.java.score.IScore;
import main.java.score.jpa.Score;
import main.java.user.IUser;

@Entity
@NamedQuery(name = "findAllUsers", query = "select u from User u")
public class User implements Serializable, IUser    {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userId;

    @NotNull
    private String firstName;

    @NotNull
    private String lastName;

    @NotNull
    private double handicap;

    @OneToMany
    private Collection<Score> scores;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getFirstName() {
        return this.firstName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getLastName() {
        return this.lastName;
    }

    public void setId(int id) {
        this.userId = id;
    }
    public int getId() {
        return userId;
    }
    public void setHandicap(double hcp) {
        this.handicap = hcp;
    }
    public double getHandicap() {
        return handicap;
    }
    @Override
    public void setScores(Collection<Score> scores) {
        if (this.scores != null){
            for (Score temp : scores){
                this.scores.add(temp);
            }
        }
        else{
            this.scores = scores;
        }
    }
    @Override
    public Collection<Score> getScores() {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean equals(Object object){
        boolean result = false;
        if (object instanceof User){
            User temp = (User)object;
            if (temp.getFirstName().equals(this.firstName)
                    && temp.getLastName().equals(this.lastName)
                    && temp.getHandicap() == this.handicap){
                result = true;
            }
        }
        return result;
    }

    public String toString(){
        String result = ("User: \nID: " + this.userId + "\nName:" + this.firstName + " " + this.lastName + " \nHandicap:" + this.handicap);
        return result;
    }

}

From my understanding, the persistence.xml file should drop and create all of the tables each time the hsqldb.jar file is run.

Folder Structure in Eclipse:

I haven't found an issue with my connection (it works when I connect, just no data), my persistence file or location. I originally populated the database this way.

Community
  • 1
  • 1
  • As you originally did it successfully, it is likely there is a mismatch between the URLs used in the program and outside it. Run the Server with `silent=false` and check the output when you connect. – fredt Nov 06 '17 at 17:02

0 Answers0