我該如何提高性能?當從stackoverflow或gituhb rest api請求數據時,需要幾毫秒才能得到響應,但對於我的小應用程序,每個請求需要3-5秒。從休眠數據庫獲取數據需要很長的時間
例子:
@XmlRootElement
@Entity
@Table(name = "customers")
public class Customer extends Model {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name = "address_id")
private Address address;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private List<Booking> bookings;
/**
* Constructor.
*
*/
public Customer() {}
/**
* Constructor.
*
* @param email Email of the customer.
* @param address Address of the customer.
* @param name Name of the customer.
*/
public Customer(String email, Address address, String name) {
this.email = email;
this.address = address;
this.name = name;
}
/**
* Set the id of the customer.
*
*/
public void setId(int id) {
this. id = id;
}
/**
* Get the id of the customer.
*
* @return Id of the customer.
*/
public int getId() {
return id;
}
/**
* Get the name of the customer.
*
* @return Name of the customer.
*/
public String getName() {
return name;
}
/**
* Set the name of the customer.
*
* @param name New name of the customer.
*/
public void setName(String name) {
this.name = name;
}
/**
* Get the email of the customer
*
* @return Email of the customer.
*/
public String getEmail() {
return email;
}
/**
* Set the email of the customer
*
* @param email New email of the customer.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Get the address of the customer
*
* @return Address of the customer.
*/
public Address getAddress() {
return address;
}
/**
* Set the address of the customer
*
* @param address New address of the customer.
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* Get the bookings of the customer.
*
* @return List of bookings.
*/
@XmlTransient
public List<Booking> getBookings() {
return bookings;
}
/**
* Set the bookings of the customer.
*
* @param bookings List of bookings.
*/
public void setBookings(List<Booking> bookings) {
this.bookings = bookings;
}
/**
* Get all customers from the database.
*
* @return List of customers.
*/
public static List<Customer> all() {
return (List<Customer>) Database.all(Customer.class);
}
/**
* Find a customer by the id.
*
* @param id Id of the customer.
* @return The customer object. Returns null if no customers were found.
*/
public static Customer find(int id) {
return (Customer) Database.find(Customer.class, id);
}
/**
* Check if a given customer id already exists.
*
* @param id Id of the customer.
* @return Boolean.
*/
public static boolean exists(int id) {
return Database.exists(Customer.class, id);
}
/**
* Find customers by a specific column value.
*
* @param column Column of the database table.
* @param value Value of the column.
* @return List of customers.
*/
public static List<Customer> where(String column, String value) {
return (List<Customer>) Database.where(Customer.class, column, value);
}
}
數據庫類:
public class Database {
/**
* Get all objects of a model from the database.
*
* @param model Class type of the model.
* @return List of all model objects.
*/
public static List<? extends Model> all(Class<? extends Model> model) {
EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
List<Model> list = (List<Model>) entityManager.createQuery("from " + model.getName(), model).getResultList();
entityManager.close();
return list;
}
/**
* Find a model object by the id.
*
* @param model Class type of the model.
* @param id Id of the model object.
* @return The model object. Returns null if no objects were found.
*/
public static Model find(Class<? extends Model> model, int id) {
EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
Model m = entityManager.find(model, id);
entityManager.close();
return m;
}
/**
* Check if a given model object id already exists.
*
* @param model Class type of the model.
* @param id Id of the model object.
* @return Boolean.
*/
public static boolean exists(Class<? extends Model> model, int id) {
return Database.find(model, id) != null;
}
/**
* Find model objects by a specific column value.
*
* @param model Class type of the model.
* @param column Column of the database table.
* @param value Value of the column.
* @return List of model objects.
*/
public static List<? extends Model> where(Class<? extends Model> model, String column, String value) {
EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
Query query = entityManager.createQuery("from " + model.getName() + " where " + column + "=:value");
return (List<? extends Model>) query.setParameter("value", value).getResultList();
}
}
HibernateUtil類:
public class HibernateUtil {
private static EntityManagerFactory entityManagerFactory;
static {
entityManagerFactory = Persistence.createEntityManagerFactory("com.travelagency.jpa");
}
/**
* Get the EntityManager Factory object.
*
* @return EntityManagerFactory object.
*/
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
}
謝謝
您問「什麼需要它這麼久」,並且不打擾在您的JPA提供程序的日誌中查找?在SQL調用?在執行其他操作?哦,創建一個EntityManager不需要很長時間(這是EMF需要很長時間,而你只有其中的一個) –