0

私はJPAを学んでおり、予定のあるテーブルを表示する小さなアプリケーションを作成しようとしています.

今のところ、h:dataTable(JSF) を使用して予定の説明を表示するだけですが、何らかの理由で、JPA が予定ごとに 1 つの選択を生成して、関連するCreatorエンティティを取得します。したがって、たとえば、100 件の予定がある場合、このCreatorエンティティへのアクセスを使用していない場合でも、データベースに対して 101 件のクエリが生成されます (1 件は予定を取得するため、1 件は作成者ごとに 1 件)。

これが私の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]



..........
..........
..........
4

2 に答える 2