Results 1 to 3 of 3

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

  1. #1
    Join Date
    Oct 2011
    Posts
    17

    Unhappy 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •