-->
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: Mapping almost ok, one value dont presists.
PostPosted: Fri Jan 26, 2007 2:41 pm 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
I have mapped two entities with OneToMany through an HashMap. It seems to work rather good, except one thing. When Im adding a new HashMap to my Author and then try to update the values will be presisted except for the joincolumn in the booktable. Its always null. No exception is thrown.

Author
Code:
@Entity
public class Author {
   private Long id;
   private String name;
   private Map <String, Book> bookList = new HashMap <String, Book> ();   

   [...]

   @OneToMany(mappedBy="author",targetEntity=Book.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
   @MapKey(name="id")
   public Map<String,Book> getBookList() {
      return bookList;
   }

}




Book
Code:
public class Book {
   
   private String id;
   private String name;
   private Long authorId;
   private author;
   
   [...]
   
   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name="author_id",referencedColumnName="id")
   public Author getAuthor() {
      return author;
   }   
         

}



jsp
Code:
   [...]
   bookList.put(new Book("Foo","Bar"));
   Author author = authorService.getAuthorById(1L);
   author.setBookList(bookList);
   authorService.updateAuthor(author);


Q: Why is the author_id in book table never set.

Q: The old posts remains untouched i the table. Replacing an collection with objects should replace all post in the table(some thing like "delete from Book where author_id = ..." )


Enviroment:
Java 1.6 / 6
Mysql 5.x
jboss-4.0.5.GA



Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 11:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
because it's a bidirectional relationship and thus both sides have to be updated.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 6:41 pm 
Newbie

Joined: Thu Jan 11, 2007 11:27 am
Posts: 11
Thanks for your answer. I still dont got it to work. Even if update author and related books. Its that what you mean by both sides? If add an collection to entity I shouldnt need to set the keycolumn by my self, Hibernate will do that for me... right?

Code:
Book book = new Book("foo","bar");
book.setAuthorId(author.getId()); // this line is a waste, right? Hibernate will do it for me as soon as I add the book to the Author collection and presist?



Thanks for your time, I will be dealing out points as soon as I mange to solve this...

---- EDIT ----

Sollutions: Hopes this helps someone.

What emmanuel means is this.
Code:
Book book = new Book("Foo","Bar");
Author author = authorService.getAuthorById(1L);
book.setAuthor(author); //One side
bookList.put(new Book("Foo","Bar"));
author.setBookList(bookList); // Second side
authorService.updateAuthor(author); //Presist


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.