Announcement Announcement Module
Collapse
No announcement yet.
Issues with Many TO Many Update - getHibernateTemplate().SaveOrUpdate()..Urgent pleas Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issues with Many TO Many Update - getHibernateTemplate().SaveOrUpdate()..Urgent pleas

    Hi,
    I have a many to Many Mapping between, the three tables.

    User, Role and user_role(mapping table). Role table has the following records. The tables have GenerateKey set to Auto, so the ID needs not be supplied.

    ---Role
    --------------------
    Role ID Role Name
    --------------------
    1 User
    2 Admin

    I am using the following code to create a new User in the database. My Purpose is to create a new user and use the existing role (2) to update user_role table. But when I use the code below, it creates one new record in each tables. User, Role and user_role all gets new record. But I only want to create a new record in User and user_role tables and not in Role. And I am setting the user property to "User" which maps to 2 in the role table.

    Can you please tell what is wrong here. Or is there any other way of achiving the same result.
    I would really appreciate any help on this.

    #code ----
    Role role = new Role();
    role.setUser("User");
    User user = new User();
    user.setRole(role);
    getHibernateTemplate().saveOrUpdate(user)

  • #2
    And why shouldn't it create 2 objects. You are creating 2 new objects yourself, for hibernate those are perfectly crisp, shiny new objects (no database id) and thus you get 2 records. Not so strange... If you want to simply attach an existing role to the user then do exactly that, retrieve the role from database, attach it to user, and save...

    You seem to be missing some basic knowledge on hibernate I suggest a areas on hibernate.

    Another point don't use HibernateTemplate/HibernateDaoSupport it isn't recommended anymore since hibernate 3.0.1. Simply use the SessionFactory directly.

    Comment


    • #3
      And why shouldn't it create 2 objects. You are creating 2 new objects yourself, for hibernate those are perfectly crisp, shiny new objects (no database id) and thus you get 2 records. Not so strange... If you want to simply attach an existing role to the user then do exactly that, retrieve the role from database, attach it to user, and save...

      You seem to be missing some basic knowledge on hibernate I suggest a areas on hibernate.

      Another point don't use HibernateTemplate/HibernateDaoSupport it isn't recommended anymore since hibernate 3.0.1. Simply use the SessionFactory directly.

      Comment

      Working...
      X