1

I want to Persist an entity Book which has ManyToOne relationship with other entity Author .

  @Entity
    @Table(name = "BOOK")
    public class Book {
        private int id;
        private Author author;

    @ManyToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "AUTHOR_ID")
     public Author getAuthor() {
        return author;
    }
}

So, When I want to save a Book entity, I need to FIND the Author entity using

Author aut = entityManager.find(Author.class, Id); 
book.setAuthor(aut); 
em.merge(book)

So, for finding of a Author I need to explicitly issue an SELECT query which I don't want to do. Is there any other way that, I can avoid explicit looking for foreign key but can persist the entity with Foreign key value?

Thanks

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
pijushcse
  • 510
  • 9
  • 31
  • possible duplicate of [hibernate - how to save parent with detached child](http://stackoverflow.com/questions/32130703/hibernate-how-to-save-parent-with-detached-child) – Dragan Bozanovic Sep 24 '15 at 10:51

1 Answers1

0

Is it option for you to use JPQL? You can just use simple query

UPDATE Book b SET b.author=(SELECT a FROM Author a WHERE a.id=:author_id) WHERE b.id=:book_id

Another way to do this is following:

Author reference = entityManager.getReference(Author.class,authorId);
book.setAuthor(reference);
entityManager.merge(book);

A bit more info about getReference method

This way select won't be issued.

Community
  • 1
  • 1
asm0dey
  • 2,841
  • 2
  • 20
  • 33
  • entityManager.getReference(Author.class,authorId); I tried with that approach and debug the code. It seems, when execute this line, a SELECT query issued to the Database. But, thats what I don't want. I want the SELECT for the Foreign key column would be implicit – pijushcse Sep 25 '15 at 19:04
  • @pijushcse isn't it what I wrote? – asm0dey Sep 25 '15 at 19:06
  • That serve the purpose. For saving the Book entity, a SELECT query need to execute for the Foreign key column. And, getReference() method issued a SELECT query. – pijushcse Sep 25 '15 at 19:08
  • It's very strange. Are you sure that it – asm0dey Sep 25 '15 at 19:10
  • Sorry, posted accidentally. It's very strange. Are you sure that it was called n this statement? – asm0dey Sep 25 '15 at 19:23
  • Yes, sir. I'm quite sure and spending a long time on that. – pijushcse Sep 25 '15 at 19:43
  • Is it possible that your class is final for example? Also may it be somehow linked to fact that you're using primitive int as id? – asm0dey Sep 25 '15 at 19:47
  • Class is not final. but Enitity Id field is a primitive (int) type. Is that a problem? – pijushcse Sep 25 '15 at 19:49
  • I'm not sure, to be honest. But it may be — hibernate works better with non-primitive types. – asm0dey Sep 25 '15 at 20:16
  • How do you check that the `select` is executed in the `getReference` method? Is it possible that you have a variable inspection window during debugging that triggers proxy initialization? Could you try logging something before and after the `getReference` method call without stopping on any break points to make sure that the `select` is executed in between? – Dragan Bozanovic Sep 25 '15 at 22:37
  • it's easy: turn on hibernate logging in `persistence.xml`, place breakpoint on this statement, run all the program until hitting this breakpoint, clear console output and make one step in debugger. If then select is in your console - than it's actually executed on this statement. Another way is to make something like `============getting reference============` before and after statement and to see if hibernate makes db call between them. – asm0dey Sep 26 '15 at 04:42
  • @asm0dey Sure, I was asking the OP. :) – Dragan Bozanovic Sep 26 '15 at 05:39
  • @DraganBozanovic sorry, thought it was OP, not other person. – asm0dey Sep 26 '15 at 09:03
  • I suppose that select is not issued when getReference() is called, but when the retrieved author is assigned to the book. Hibernate probably wants to check that the entity referenced by the foreign ley really exists in DB. This is not prescribed in JPA standard. It is possible that hibernate provides an option to turn this behavior off, but I'm not so into hibernate tweaking. – OndroMih Sep 26 '15 at 19:31
  • @OndrejM agreed, it's possible (a little strange though). Then OP has only option to use appropriate JPQL query. – asm0dey Sep 26 '15 at 19:37
  • No, Hibernate does not check whether the entity exists. – Dragan Bozanovic Sep 26 '15 at 20:14