AhmedRana AhmedRana - 7 months ago 33
Java Question

JPA connecting to wrong table

I am trying to learn JPA
Here is my entity file.


public class Passenger implements Serializable{
public Passenger() {

public int getId() {
return id;

public void setId(int id) {
this.id = id;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String firstName;
private String lastName;


My persistence.xml is below.

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="JPA_POJO">

My servlet is like this.

@WebServlet(name = "addPassenger")
public class addPassenger extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Passenger p = new Passenger();
// airline p = new airline();


EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA_POJO");
EntityManager em = emf.createEntityManager();



When I run this code I get the following error.

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'airline.passenger' doesn't exist
Error Code: 1146

As far as I can understand it is trying to connect to PASSENGER table instead of AIRLINE table.
I have assigned the data source that points to airline table. In fact I wrote a class file airline.java basically same as Passenger.java, when I used airline.java object(commented out in code) in my servlet it worked.
What am I doing wrong?


What it does seem to be totally expected, you did not provide a name to the table of your entity Passenger so it uses the name of the entity as default table name so it tries to insert into the table Passenger of the schema airline which is what is expected.

For me your problem is only that the table has not been created so far, if you want let eclipselink create your table for you, you should add the property eclipselink.ddl-generation to your file persistence.xml and set it to create-or-extend-tables as described here.

In your case it will be something like:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
    <persistence-unit name="JPA_POJO">
            <property name="javax.persistence.jtaDataSource" value="java:/airlines"/>
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>