difference hibernate save vs persist vs saveOrUpdate

Difference between save and saveOrUpdate in Hibernate Main difference between save and saveOrUpdate method is that save() generates a new identifier and INSERT record into database while saveOrUpdate can either INSERT or UPDATE based upon existence of record. Clearly saveOrUpdate is more flexible in terms of use but it involves an extra processing to find…

“detached entity passed to persist error” with JPA

I got the answer, I was using: em.persist(user); I used merge in place of persist: em.merge(user); Here .persist() only will insert the record.If we use .merge() it will check is there any record exist with the current ID,If it exist it will update otherwise it will insert new record.    The error you described occurs…

jpa merge vs persist

Either way will add an entity to a PersistenceContext, the difference is in what you do with the entity afterwards. Persist takes an entity instance, adds it to the context and makes that instance managed (ie future updates to the entity will be tracked) Merge creates a new instance of your entity, copies the state…

获取刚persist的bean的id

被这个问题困扰好多天了,今天奋战到两点多终于解决了,其实是一个很简单的解决方法。 前面用到contact和employee,需要在jsp里面创建contact后加入到employee的列表,貌似直接persist contact后,立即用这个contact的话会id 为null,相当苦恼!不知道是oracle的原因还是因为是在客户端里调用的原因。 今天在为了完成作业的任务之一–3个entity bean,强行加了一个一对一的phoneNumber to contact,以为会很简单,其实坐起来还是用了两个多小时,也好,更清楚的理解了一下orm! 当表格中得到phoneNumber后,就要立即instantiate一个phoneNumber bean,然后persist,问题跟以前一样,如果直接用这个bean来创建新contact的话,id会为空,然后当然就nullpointer来了! 解决方法是改造一下persist方法,以为在dao里面的话,persist之后那个obj是会有id的,于是在dao里面见一个persist的wrapper方法,让它返回刚persist的obj,这样就有id了! //其实这个就是merge方法,不用增加了,今天才发现。。。(11月2日) 如果是一对多的话,以为可以直接用那个list的add方法,不知道EJB会不会任,明天来试试!如果可以的话就不用手动去维护中间那张表了,也可以把那个sql语句给去掉!//确实可行,不过add之后要增加一个persist或者merge的操作,它们不会自动进数据库。。。(11月2日)希望能行,momo!