Home em.getTransaction().commit(); causing an error in jpa
Reply: 1

em.getTransaction().commit(); causing an error in jpa

college1
1#
college1 Published in 2017-12-06 18:46:20Z

ive been getting the following errors when trying parser a xml file to mysql database.The database is created. I dont under stand why it cannot create the user

error code Console errors

Driver.java

package DAO;

import parser.XMLParser;

import java.util.ArrayList;
import java.util.List;

import entities.*;

public class NewDriver {

public static void main(String args[]) throws Exception{
    UserDAO userDAO = new UserDAO();

    ProductDAO productDAO= new ProductDAO();
    ProductOnHandDAO productOnHandDAO= new ProductOnHandDAO();
    XMLParser x = new XMLParser();

    User user1 = new User("luke", "luke");
    userDAO.createUser(user1);

    Store store = new Store();
    StoreDAO storeDAO= new StoreDAO();
    storeDAO.createStore(store);


    List<Product> products = x.getProduct("‪test.xml");
    for(Product p : products){
        p.setStore(store);
        productDAO.createProduct(p);
    }

    List<Product> productList = ProductDAO.findByStore(store);
    List<ProductOnHand>productOnHands;

    productOnHands = x.getAllProductOnHand(productList,"‪test.xml");
    for (ProductOnHand p1 : productOnHands){
        p1.setStore(store);
        ProductOnHandDAO.createProductOnHand(p1);

    }


}
} 

UserDAO.java

public class UserDAO {

 public void createUser(User user){
        PersistenceUtil.persist(user);
    }

    public List<User> findAllUsers(){
        EntityManager em = PersistenceUtil.createEM();
        List<User> users = (List<User>)
                em.createNamedQuery("User.findAllUsers").getResultList();
        em.close();
        return users;
    }

     public User findUserByUsernameAndPassword(String username, String 
      password){
        EntityManager em = PersistenceUtil.createEM();
        List<User> users = (List<User>)






   em.createNamedQuery("User.findUserByUsernameAndPassword").
   setParameter("username", username).setParameter("password", 
   password).getResultList();
        em.close();
        return users.get(0);
    }
 }

User.java

@Entity
public class User {

@Id
@Column
private int id;

private String username;
private String password;


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public User(String username, String password) {
    this.username = username;
    this.password = password;
}


public User() {
}
}

Persistence.util

public class PersistenceUtil implements Serializable {

private static final long serialVersionUID = 1L;


protected static EntityManagerFactory emf =
        Persistence.createEntityManagerFactory("dt354rel");


public static void persist(Object entity) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(entity);
    em.getTransaction().commit();
    em.close();
}

public static void remove(Object entity) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Object mergedEntity = em.merge(entity);
    em.remove(mergedEntity);
    em.getTransaction().commit();
    em.close();
}

public static Object merge(Object entity) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    entity = em.merge(entity);
    em.getTransaction().commit();
    em.close();
    return entity;
}

public static EntityManager createEM() {
    return emf.createEntityManager();
}

}  

Any help would be appricated i looked at my persistence class and it all seems fine to me.

Simon Martinelli
2#
Simon Martinelli Reply to 2017-12-06 19:03:41Z

Look at the error:

Table dt354rel.user doesn't exist

Check if this table is missing or if you don't have access to that table.

Btw. you don't have to create a new EntityManager in all that methods. You could create it once an reuse it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.365027 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO