Reputation: 503
I am trying to learn JPA Here is my entity file. Passenger.java
@Entity
public class Passenger implements Serializable{
public Passenger() {
super();
}
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;
}
@Id
@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">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/airlines</jta-data-source>
<class>com.airline.model.Passenger</class>
</persistence-unit>
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();
p.setFirstName("ABC");
p.setLastName("XYZ");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA_POJO");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
}
}
When I run this code I get the following error.
org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'airline.passenger' doesn't exist Error Code: 1146 Call: INSERT INTO PASSENGER (ID, FIRSTNAME, LASTNAME) VALUES (?, ?, ?)
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?
Upvotes: 0
Views: 706
Reputation: 44965
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">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/airlines</jta-data-source>
<class>com.airline.model.Passenger</class>
<properties>
<property name="javax.persistence.jtaDataSource" value="java:/airlines"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
Upvotes: 2