-->
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.  [ 2 posts ] 
Author Message
 Post subject: One-to-Many unidirectional not saving Join table
PostPosted: Sun Jun 14, 2009 11:54 am 
Newbie

Joined: Sun Jun 14, 2009 11:39 am
Posts: 2
Hi,

I have a one-to-many unidirectional association using a Join table. What I would think to be a pretty vanilla case. What I am seeing is that on save (saveOrUpdate) the entity tables are getting updated great, but the Join Table I have specified is not. I then go to read the parent entity and none of the associated entities are returned because of the missing Join table data. I have tested by changing the name of the Join Table specified in my code to a table not in my DB and on read, I indeed get a table not found error (Good) but on save I get no such error. It's like the association (and the Join table update) is being completely ignored on save. Kinda driving me nuts any help is appreciated. Here's the relevant code:

Code:

// PARENT or ONE SIDE
@Entity
@Table(name = "User")
public class User {
   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "userId")
    private Integer userId;
   
    @OneToMany(fetch=FetchType.EAGER)
    @Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinTable(
            name="userSheets",
            joinColumns = @JoinColumn( name="sheetId"),
            inverseJoinColumns = @JoinColumn( name="userId")
    )
    private Set<Sheet> sheets;

}

// CHILDREN or MANY SIDE
@Entity
@Table(name = "Sheet")
public class Sheet {
    // Unidirectional so no mapping here per hibernate doc.
   // http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections

}

// SAVE CODE
public class UserDAO extends HibernateDaoSupport {
   @Transactional(readOnly=false)
   public void save(User newUser) {
      Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
      session.setFlushMode( FlushMode.COMMIT );

                // User table gets updated. Sheet table gets updated. userSheets does not. On read however, it is expected.
      getHibernateTemplate().saveOrUpdate(newUser);
   }
}


Top
 Profile  
 
 Post subject: Re: One-to-Many unidirectional not saving Join table
PostPosted: Sun Jun 14, 2009 4:13 pm 
Newbie

Joined: Sun Jun 14, 2009 11:39 am
Posts: 2
Update.
I fixed this by simply making the mapping a bidirectional mapping and adding the userId to the sheets table. It also required that I remember to set the user on every sheet before save.

For anyone interested, here is the code that is now working:

Code:
@Entity
@Table(name = "User")
public class User {   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "userId")
    private Integer userId;

    @OneToMany(mappedBy="user")
    @Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    private Set<Sheet> sheets;
}

@Entity
@Table(name = "Sheet")
public class Sheet {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "sheetId")
    private int sheetId;

    @ManyToOne
    @JoinColumn(name="userId")
    private User user;
}

DB UPDATE:
Removed JoinTable userSheets
Added userId to Sheets (created FK to user)

CODE UPDATE:
When instantiating a new sheet, need to make sure you call setUser() on Sheet to create the relationship.


I hate to be 'that guy' but I don't think the unidirectional with a Join table works, or to be more PC, works as described in the doc(http://docs.jboss.org/hibernate/stable/ ... ollections). I don't have time to debug through the hibernate src, but I assume there is an undocumented setting/step that is required to establish the relationship and have the JoinTable specified updated on save.

I am still hoping I am just wrong and someone can point out my error, especially considering the dogmatic tone the hibernate docs take : "A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section)."

Like I said, hopefully I can be proven wrong. But in the meantime, I have updated my code and schema and am getting onto my next deliverable.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.