0

Hibernate entities used. There are Address and Room entities and apropriate tables in DB.
Address can have multiple Rooms.

Both tables have address_Id and customerEmailAddress
These columns are FK in Room table,that references to Address table.

There is already Address record in DB.
It's needed to add several Rooms to this Address.

How to organize Hibernate relation with annotations between Address and Room, so Room table to be updated with appropriate attributes from Address:
address_Id and customerEmailAddress

Java part looks like:

    Room room = new Room();
    Address addr  = someService.getAddressFromSession();
    room.add(addr);
    entityManager.persist(room);
    Room room2 = new Room();
    room2.add(addr);
    entityManager.persist(room2);

DB outcome (Room table) should be following:
id||addressId|| customerEmailAddress
1 || 3               || mail3@a.com
2 || 3               || mail3@a.com

tshepang
  • 12,111
  • 21
  • 91
  • 136
sergionni
  • 13,290
  • 42
  • 132
  • 189

1 Answers1

1

You haven't mentioned about primary keys of both the tables. Assuming both the tables have their own primary keys, I think you can have standard mapping done between Address and Room objects using component model as below:

  1. Map address_id and customerEmailAddress as a component. Refer the documentation for component mapping here-- Hibernate Component Mapping.

  2. Add rooms in Address and One-To-Many relationship using component defined above and set cascade to All.

  3. Add address in Room as Many-To-One using component defined above.

  4. Define one addRoom method in Address as below(conceptually):

      public void addRoom(Room room){
           if(this.rooms == null){
               this.rooms = new ArrayList<Room>();
           }
           room.setAddress(this);
           this.rooms.add(room);
      } 
    
  5. Now you can simply work at Address level. To save the room, add the room to address and save the address.

Hope this helps!

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • hi Yogendra,thank you for hint.I will feedback soon how it works. – sergionni Oct 14 '12 at 08:24
  • got `...MySQLIntegrityConstraintViolationException: Duplicate entry...`,while persisting `Address`,as far it's aready exists in DB. Perhaps ,I should use other `CascadeType`? – sergionni Oct 14 '12 at 12:48
  • Is there are other constraint in your room table? Also please make sure, all the room objects are populated with `Id` (primary key) except the new room object. All old room objects should have been the originally loaded objects ONLY. Hibernate uses primary key to determine, whether it is insert or update. Also **use `merge` in place of `persist`** to avoid duplicate constraint exception. – Yogendra Singh Oct 14 '12 at 15:02
  • it looks like the problem was in that `customerEmailAddress` and `addressId` were defined as `unique key` on mysql create script.that means that they should be unique as far they represent `tuple`. – sergionni Oct 16 '12 at 15:32
  • Good to know your problem is resolved. If you think the answer was helpful, please don't forget to accept the answer. – Yogendra Singh Oct 16 '12 at 15:34