首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JPA为我没有访问的相关实体运行数百个选择

JPA为我没有访问的相关实体运行数百个选择
EN

Stack Overflow用户
提问于 2013-10-11 14:43:43
回答 2查看 99关注 0票数 0

我正在学习JPA,并试图编写一个小应用程序来显示带有约会的表。

现在,我只是使用h:dataTable (JSF)显示了对约会的描述,但出于某种原因,这会导致JPA为每个约会生成一个选择,以检索相关的Creator实体。因此,例如,如果我有100个约会,这将生成101个对数据库的查询(一个用于检索约会,另一个用于每个创建者),即使我没有以任何方式访问这个Creator实体。

这是我的Appointment实体。它通过一个不是主键的代码引用Creator实体,但我不能更改表的结构。

代码语言:javascript
运行
复制
@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实体

代码语言:javascript
运行
复制
@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
}

这是我的刀

代码语言:javascript
运行
复制
@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;
    }
}

这是我的日志摘录

代码语言:javascript
运行
复制
[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]



..........
..........
..........
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-15 07:44:03

好吧,我解决了。这是因为我使用的是Tomcat,而EclipseLink无法使用织入来更改类,而没有这种情况,EclipseLink只能使用急切的加载。

我刚从Tomcat转移到一个真正的应用服务器。

票数 0
EN

Stack Overflow用户

发布于 2013-10-11 14:51:29

fetchType关系的默认ManyToOneEAGER。将其设置为LAZY,这将导致在访问之前不会查询Creator实体。

代码语言:javascript
运行
复制
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "APPOINTMENT_CREATOR", referencedColumnName = "USER_CODE")
private Creator creator;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19320994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档