Finally i've foud a solution.. maybe will help someone.
The think was that i've a OneToMany-ManyToOne relation (bidirectional), an activity has multiple AssignedActivities and a AssignedActivity is relationated width an Activity.
Here are my Classes :
Code:
@Entity
@Table (name="PROJECT_ACTIVITIES")
@SequenceGenerator(name = "SEQ_PACT_ID", sequenceName = "SEQ_PACT_ID", allocationSize = 1)
public class Activity implements IsSerializable{
@Id
@Column(name="PACT_ID", nullable=false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PACT_ID")
private Integer id = null;
@Column(name="PACT_DESC")
private String desc = null;
@Column (name ="STATE")
private Integer state = null;
@OneToMany (mappedBy="pk.activity", fetch = FetchType.LAZY)
@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN, CascadeType.SAVE_UPDATE})
private Set<AssignedActivity> assignedTo = new HashSet<AssignedActivity>();
...}
@Entity
@Table(name = "ASSIGNED_ACTIVITIES")
public class AssignedActivity implements IsSerializable {
@EmbeddedId
private AssignedActivityPk pk = new AssignedActivityPk();
...}
@Embeddable
public class AssignedActivityPk implements Serializable {
@Column (name = "CONS_ID", nullable=false)
private Integer consId;
@ManyToOne
@JoinColumn (name = "PACT_ID", nullable=false)
@ForeignKey(name="PACT_ID")
private Activity activity = new Activity();
...}
and then to save and update my records i'm doing :
Code:
if ( a.getId() != null) {
Activity act = (Activity) session.load(Activity.class, a.getId()) ;
act.setDesc(a.getDesc());
act.setDueDate(a.getDueDate());
act.setPersonInCharge(a.getPersonInCharge());
act.setState(a.getState());
act.clearAssignedTo();
session.saveOrUpdate(act);
session.getTransaction().commit();
session.getTransaction().begin();
Iterator it = a.getAllAssignedTo().iterator();
while (it.hasNext()) {
AssignedActivity aa = (AssignedActivity)it.next();
aa.setActivity(act);
act.addAssignedTo(aa);
}
session.saveOrUpdate(act);
} else {
session.persist(a);
}
session.getTransaction().commit();
To update the records i delete first the old records and then create the new ones. After long time, this is the solution i've found which is working... if somebody has a better solution please let me know.
Regards,