我正在学习 JPA,并且正在尝试编写一个小应用程序来显示带有约会的表格。
现在,我只使用h:dataTable
(JSF) 显示约会的描述,但由于某种原因,这会导致 JPA 为每个约会生成一个选择以检索相关Creator
实体。因此,例如,如果我有 100 个约会,这会生成 101 个对数据库的查询(一个用于检索约会,一个用于每个创建者),即使我没有Creator
以任何方式访问此实体。
这是我的Appointment
实体。它通过不是主键的代码引用Creator
实体,但我无法更改表的结构。
@Entity
public class Appointment implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigDecimal id;
private String description;
private Timestamp date;
@ManyToOne
@JoinColumn(name = "APPOINTMENT_CREATOR", referencedColumnName = "USER_CODE")
private Creator creator;
// Getters and setters
}
这是Creator
实体
@Entity
public class Creator implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigDecimal id;
private String name;
private String surname;
@Column(name="USER_CODE")
private int userCode;
@OneToMany(mappedBy = "creator")
private List<Cita> appointments;
public Appointment addAppointment(Appointment appointment) {
getAppointments().add(appointment);
appointment.setCreator(this);
return appointment;
}
public Appointment removeAppointment(Appointment appointment) {
getAppointments().remove(appointment);
appointment.setCreator(null);
return appointment;
}
// Getters and setters
}
这是我的 DAO
@Repository
public class AppointmentDAO {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
public List<Appointment> findAll() {
List<Appointment> result = null;
try {
result = this.entityManager.createQuery("SELECT a from Appointment a").getResultList();
} catch (Exception ex) {
}
return result;
}
}
这是我的日志摘录
[EL Finest]: connection: 2013-10-11 16:22:35.406--ServerSession(33459456)--Connection(11062282)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection released to connection pool [read].
[EL Finest]: query: 2013-10-11 16:22:35.421--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Execute query ReadObjectQuery(name="creator" referenceClass=Creator sql="SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)")
[EL Finest]: connection: 2013-10-11 16:22:35.421--ServerSession(33459456)--Connection(20248250)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection acquired from connection pool [read].
[EL Finest]: connection: 2013-10-11 16:22:35.421--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main]) --reconnecting to external connection pool
[EL Fine]: sql: 2013-10-11 16:22:35.453--ServerSession(33459456)--Connection(3304058)--Thread(Thread["http-bio-8080"-exec-3,5,main])--SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)
bind => [7054382]
[EL Finest]: connection: 2013-10-11 16:22:35.515--ServerSession(33459456)--Connection(20248250)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection released to connection pool [read].
[EL Finest]: transaction: 2013-10-11 16:22:35.531--UnitOfWork(12558692)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Register the existing object net.turbo.model.entities.Creator@b2e368
[EL Finest]: query: 2013-10-11 16:22:35.531--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Execute query ReadObjectQuery(name="creator" referenceClass=Creator sql="SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)")
[EL Finest]: connection: 2013-10-11 16:22:35.531--ServerSession(33459456)--Connection(32578948)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection acquired from connection pool [read].
[EL Finest]: connection: 2013-10-11 16:22:35.531--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main])--reconnecting to external connection pool
[EL Fine]: sql: 2013-10-11 16:22:35.562--ServerSession(33459456)--Connection(13173146)--Thread(Thread["http-bio-8080"-exec-3,5,main])--SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)
bind => [7042199]
[EL Finest]: connection: 2013-10-11 16:22:35.64--ServerSession(33459456)--Connection(32578948)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection released to connection pool [read].
[EL Finest]: transaction: 2013-10-11 16:22:35.64--UnitOfWork(12558692)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Register the existing object net.turbo.model.entities.Creator@1b0c903
[EL Finest]: query: 2013-10-11 16:22:35.64--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Execute query ReadObjectQuery(name="creator" referenceClass=Creator sql="SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)")
[EL Finest]: connection: 2013-10-11 16:22:35.64--ServerSession(33459456)--Connection(7112313)--Thread(Thread["http-bio-8080"-exec-3,5,main])--Connection acquired from connection pool [read].
[EL Finest]: connection: 2013-10-11 16:22:35.64--ServerSession(33459456)--Thread(Thread["http-bio-8080"-exec-3,5,main])--reconnecting to external connection pool
[EL Fine]: sql: 2013-10-11 16:22:35.671--ServerSession(33459456)--Connection(15764427)--Thread(Thread["http-bio-8080"-exec-3,5,main])--SELECT ID, SURNAME, USER_CODE, NAME FROM CREATOR WHERE (USER_CODE = ?)
bind => [7076961]
..........
..........
..........