-->
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.  [ 5 posts ] 
Author Message
 Post subject: Does persist always hit the database?
PostPosted: Tue Mar 27, 2007 7:22 pm 
Newbie

Joined: Fri Jun 23, 2006 12:52 pm
Posts: 11
I'm wondering whether or not the JPA EntityManager.persist method is 100% guaranteed to always call the database? Here's a code snippet:

Code:
        EntityManager em = emf.createEntityManager();
        Product p = new Product();
        p.setAmount(1.0);
        p.setName("FooBar");
        EntityTransaction et = em.getTransaction();
        et.begin();
        em.persist(p);

        // Test with JDBC
        Connection c = ((EntityManagerImpl) em).getSession().connection();
        System.out.println("AC = " + c.getAutoCommit());

        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery("select * from products where name = 'FooBar'");
        assertTrue(rs.next());
        assertEquals("FooBar", rs.getString("name"));


So, this works, but my question is will it always work no matter what? Will em.persist always execute the SQL even if there are no GeneratedValues or it is an update?


Top
 Profile  
 
 Post subject: The answer is no
PostPosted: Wed Mar 28, 2007 12:31 pm 
Newbie

Joined: Fri Jun 23, 2006 12:52 pm
Posts: 11
Okay, so I read closely all the documentation and the answer is a definitive NO. I'll be posting a blog entry about this and I'll be evaluating the other JPA providers to determine which is best. The problem I have with this solution is that Hiberante is trying to be a database instead of a simple ORM. Quite annoying actually.


Top
 Profile  
 
 Post subject: A thought....
PostPosted: Wed Mar 28, 2007 12:45 pm 
Newbie

Joined: Fri Jun 23, 2006 12:52 pm
Posts: 11
If I have a MySQL database and the object I'm persisting has a @GeneratedValue annotation anywhere inside it, it HAS to hit the database when I call persist right? Isn't it 100% guaranteed to always have the id set after a call to persist?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no this is not guarantied, use save for that

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Save method?
PostPosted: Thu Mar 29, 2007 9:48 am 
Newbie

Joined: Fri Jun 23, 2006 12:52 pm
Posts: 11
emmanuel wrote:
no this is not guarantied, use save for that


There is no save method via JPA. Were you thinking of something else?

I think I've figured out most of what is happening under the hoods. There are a few conditions that cause this to always write to the database. The easiest of which is to always include a GeneratedValue and always commit within a transaction. This causes the code to assume a flush during every write operation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.