-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: join table as a Java entity question
PostPosted: Sat Feb 17, 2007 11:03 am 
Newbie

Joined: Fri Jan 26, 2007 3:51 pm
Posts: 16
Hi,

I have a class named ScheduledCourse, it contains a collection of Enroll objects. The Enroll class represents a Join Table in the database but is also a Java entity class. I used the example as presented in the book Java Persistence with Hibernate Chapter 7 page 304, regarding "Mapping the join table to an intermediate entity".

I'm trying to test the deletion of an instance of ScheduledCourse which also includes this collection of Enroll objects, like so:

Code:
@Entity
@Table(name = "SCHEDULED_COURSES")
public class ScheduledCourse implements Serializable
{
  ...
  @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy="scheduled_course")
   //@org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   @org.hibernate.annotations.CollectionOfElements
   @JoinTable(name = "ENROLL", joinColumns = @JoinColumn(name = "ENROLL_ID"))
   private Collection<Enroll> ENROLLED = new ArrayList<Enroll>();
  ...
}



The Student class has the same at its end:

Code:
@Entity
@Table(name = "STUDENTS")

public class Student extends User implements Serializable
{
  ...
  @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy="student")
  @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @org.hibernate.annotations.CollectionOfElements
  private Collection<Enroll> ENROLLED = new ArrayList<Enroll>();
  ...
}



The Enroll class looks like so:
Code:
@Entity
@Table(name = "ENROLL")
public class Enroll
{
  @Embeddable
  public static class Id implements Serializable
  {
    @Column(name = "USER_ID")
    private Long userID;

    @Column(name = "SCHEDULED_COURSE_ID")
    private Long scheduled_course_id;

    public Id(){}

    public Id(Long userid, Long scheduled_course_id)
    {
      this.userID = userid;
      this.scheduled_course_id = scheduled_course_id;
    }

    public boolean equals(Object o)
    {
       if(o != null && o instanceof Id)
       {
         Id that = (Id)o;
         return this.userID.equals(that.userID) && this.scheduled_course_id.equals(that.scheduled_course_id);
       }
       else
       {
         return false;
       }
    }

    public int hashCode()
    {
      return userID.hashCode() + scheduled_course_id.hashCode();
    }
  }//end of inner class Id

  @EmbeddedId
  private Id id = new Id();

  ...

  @ManyToOne
  @JoinColumn(name = "USER_ID", insertable = false, updatable = false)
  private Student student; //This represents the student enrolling in a scheduled course

  @ManyToOne
  @JoinColumn(name = "SCHEDULED_COURSE", insertable = false, updatable = false)
  private ScheduledCourse scheduled_course;

  //Constructors
  public Enroll(){}

  public Enroll(Company company, Administrator admin, Student student, ScheduledCourse sc)
  {
    this.company = company;
    this.admin = admin;
    this.student = student;
    this.scheduled_course = sc;

    //Set identifier values
    this.id.scheduled_course_id = sc.getId();
    this.id.userID = student.getId();

    //Guarantee referential integrity
    sc.getEnrolled().add(this);
    student.getEnrolled().add(this);
  }   
  ...   
}



So, imagine if you will, that I've enrolled three students into the ScheduledCourse instance. This means the Enroll table has these three records in it tying in the students with the ScheduledCourse.

When I call this in my test app:

EntityManager em = ...;
ScheduledCourse sc = em.find(ScheduledCourse.class, new Long(1));
em.remove(sc);

Naturally, one would expect that those records in the Enroll table to disappear along with the one record kept in the SCHEDULED_COURSES table. But they don't. I tried eliminating the cascading options altogether, but anything I do has no affect.

Can someone explain what needs to be done in this scenario to make it work?

Alan


Top
 Profile  
 
 Post subject: join table as a Java entity question
PostPosted: Sun Feb 18, 2007 11:03 am 
Newbie

Joined: Mon Nov 27, 2006 7:38 am
Posts: 7
Hi Alan,

For the cascading remove operation to work you need a clear parent-child relationship and you need to remove the parent. You are trying to remove the child (ScheduledCourse) and then have the parent (Enroll) removed as well. Also the Enroll entity is participating in a relationship with both the ScheduledCourse entity and the Student entity.

I'm not sure there is an easy solution to your problem. Usually relationship maintenance is the responsibility of the application.

Claus


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 12:48 pm 
Newbie

Joined: Fri Jan 26, 2007 3:51 pm
Posts: 16
I've noticed that hibernate handles intermediate tables ok when they aren't java objects also. But in this case, the Enroll class is a java object labelled as an @Entity. Now this changes hibernates behaviour, as mentioned.

So, I guess I'm just going to have to deal with it manually with application code like you said.

Thanks anyway.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.