我在尝试将xml文件解析为mysql数据库时遇到以下错误。数据库已创建。 我不明白为什么它无法创建用户
错误代码 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();
}
}
任何帮助都会被我看到我的持久性课程,这对我来说似乎很好。
答案 0 :(得分:0)
查看错误:
Table dt354rel.user doesn't exist
检查此表是否缺失,或者您是否无权访问该表。
顺便说一下。您不必在所有方法中创建新的EntityManager。您可以在重复使用它后创建它。