こんにちは、私は JPA の初心者で、oneToMany 関係の永続化でパフォーマンスの問題が発生しています。エンティティとコントローラは、netbeans によって生成されています。以下のコードを見てください。人を固執するとき、
profileId.getPersonSet().add(person);
私のデータベースには約 16000 人が含まれているため、約 45 秒かかります。それが多すぎます !どうすればこれを改善できますか?
<pre><code>
public class Profile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "profile_id", nullable = false)
private Integer profileId;
@Basic(optional = false)
@Column(nullable = false, length = 60)
private String label;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "profileId", fetch = FetchType.EAGER)
private Set<Person> personSet;
public Profile() {
}
.............
</code></pre>
<pre><code>
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "person_id", nullable = false)
private Long personId;
@Basic(optional = false)
@Column(name = "last_name", nullable = false, length = 60)
private String lastName;
@Basic(optional = false)
@Column(name = "first_name", nullable = false, length = 60)
private String firstName;
@Basic(optional = false)
@Column(nullable = false)
@Temporal(TemporalType.DATE)
private Date birthday;
@Column(length = 60)
private String email;
@Column(length = 60)
private String phone;
@Column(name = "mobile_phone", length = 60)
private String mobilePhone;
@Column(length = 60)
private String company;
@JoinColumn(name = "profile_id", referencedColumnName = "profile_id", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Profile profileId;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "personId", fetch = FetchType.LAZY)
..............
</code></pre>
<pre><code>
public class PersonJpaController implements Serializable {
public PersonJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Person person) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Profile profileId = person.getProfileId();
if (profileId != null) {
profileId = em.getReference(profileId.getClass(), profileId.getProfileId());
person.setProfileId(profileId);
}
..........................
em.persist(person);
if (profileId != null) {
profileId.getPersonSet().add(person);
profileId = em.merge(profileId);
}
....................
</code></pre>