1

私は(うまくいけば)私はすべて正しいことをしていると思います。何度もグーグルで検索しましたが、何が悪いのかわかりませんでした。

Profile複数Appointmentの を持つことができます。それぞれAppointmentに がありDepartmentます。

  • デパートメントはもちろんセットで装填・収納。
  • 予定が作成され、上記のセットから部門が取得されます。
  • 予定はプロフィールに追加されます。
  • プロファイルは永続化されます。
  • 永続化するために渡された切り離されたエンティティは、Department に対してスローされます

プロフィール:

@Entity
@Table(name = "PROFILE")
public class Profile {
/**
 * 
 */
private Set<Appointment> appointments = new HashSet<Appointment>();

/**
 * @return the appointments
 */

@OneToMany(mappedBy = "profile", cascade = { CascadeType.PERSIST,
        CascadeType.MERGE })
public Set<Appointment> getAppointments() {
    return appointments;
}

/**
 * @param appointments
 *            the appointments to set
 */
public void setAppointments(Set<Appointment> appointments) {
    this.appointments = appointments;
}

/**
 * 
 * @param apt
 */
public void addAppointment(Appointment apt) {
    apt.setProfile(this);
    appointments.add(apt);
}
}

予定:

@Entity
@Table(name = "APPOINTMENT")
public class Appointment {
/**
 * 
 */
private Department department;
private Profile profile;


/**
 * @return the department
 */
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "DEPARTMENT_ID")
@Index(name = "IDX_APPOINTMENT_DEPARTMENT")
@ForeignKey(name = "FK_APPOINTMENT_DEPARTMENT")
public Department getDepartment() {
    return department;
}

/**
 * @param department
 *            the department to set
 */
public void setDepartment(Department department) {
    this.department = department;
}

/**
 * @return the profile
 */
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "PROFILE_ID")
@Index(name = "IDX_APPOINTMENT_PROFILE")
@ForeignKey(name = "FK_APPOINTMENT_PROFILE")
public Profile getProfile() {
    return profile;
}

/**
 * @param profile
 *            the profile to set
 */
public void setProfile(Profile profile) {
    this.profile = profile;
}
}

デパートメント:

@Entity
@Table(name = "DEPARTMENT")
public class Department {
/**
 *
 */
private Set<Appointment> appointments = new HashSet<Appointment>();
private Set<Department> children = new HashSet<Department>();
private Department parent;


/**
 * @return the appointments
 */
@OneToMany(mappedBy = "department")
public Set<Appointment> getAppointments() {
    return appointments;
}

/**
 * @param appointments
 *            the appointments to set
 */
public void setAppointments(Set<Appointment> appointments) {
    this.appointments = appointments;
}

/**
 * @return the children
 */
@OneToMany(mappedBy = "parent", cascade = { CascadeType.PERSIST,
        CascadeType.MERGE })
public Set<Department> getChildren() {
    return children;
}

/**
 * @param children
 *            the children to set
 */
public void setChildren(Set<Department> children) {
    this.children = children;
}

/**
 * @return the parent
 */
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENTDEPARTMENT_ID")
@Index(name = "IDX_DEPARTMENT_CHILDREN")
@ForeignKey(name = "FK_DEPARTMENT_CHILDREN")
public Department getParent() {
    return parent;
}

/**
 * @param parent
 *            the parent to set
 */
public void setParent(Department parent) {
    this.parent = parent;
}
}

誤った@Serviceコード:

@Transactional(propagation = Propagation.REQUIRED)
private Profile addNewProfile(ProfileJxb profileJxb) {
    logger.entry();

    Profile profile = new Profile(profileJxb);
    for (AppointmentJxb aptJxb : profileJxb.getAppointments()
            .getAppointmentJxb()) {
        Appointment apt = new Appointment(aptJxb);
        Department d = getDepartmentByName(aptJxb.getDepartment()); // previously fetched
        apt.setDepartment(d);
        // d.getAppointments().add(apt); // another failed attempt, this results in LazyInitException

        profile.addAppointment(apt);
    }

    profile = profileDao.makePersistent(profile); // Exception thrown here!

    logger.exit();
    return profile;
}

Departmentこの例外を回避するために永続的な状態でフェッチして設定しているため、何かが欠けていることを願っています。

ありがとうございました。

4

1 に答える 1