java - JPA connecting to wrong table -


i trying learn jpa here 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 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 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 run code 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 passenger (id, firstname, lastname) values (?, ?, ?)

as far can understand trying connect passenger table instead of airline table. have assigned data source points airline table. in fact wrote class file airline.java same passenger.java, when used airline.java object(commented out in code) in servlet worked. doing wrong?

what seem totally expected, did not provide name table of entity passenger uses name of entity default table name tries insert table passenger of schema airline expected.

for me problem table has not been created far, if want let eclipselink create table you, should add property eclipselink.ddl-generation file persistence.xml , set create-or-extend-tables described here.

in case 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> 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -