java - Understanding Hibernate saveOrUpdate and the Persistence Life Cycle -


Let's look at a simple example of a dog and a cat who is friends is not a rare incident. Apart from this, there is also the advantage of being more interesting than the case of my business.

We need a function called "SaveFriends" which takes the name of the dog and the name of a cat. We will save the dog and then cat to work for this example, the cat will have to get references to the dog back. I think this is not a perfect example, but it is lovely and works for our purposes.

FriendService.java

  public int saveFriends (string dog name, string quatname) {dog fido = new dog (); Cat felix = new cat (); Fido.name = dogName; Fido = Pashto Sweet Dog (FIOO); Felix.name = catName; [East. A] Felix. Friends = FIOS; [Ex.b] felix.friend = animalDao.getDogByName (dogName); AnimalDao.saveCat (Felix); Public Dog SaveDog (dog dog)  

AnimalDao.java (HibernateDaoSupport extended)

 {GetHibernateTemplate ( ). SaveOrUpdate (dog); Return dog} Public Cat Save Cat (cat cat) {getHibernateTemplate (). SaveOrUpdate (cat); Return cat; } Public dog getDogByName (name of string) {return (dog) getHibernateTemplate (). ("Where from name =?", Name) search dog .get (0); }  

Now, for a minute, suppose I would like to use Example A or Example B to save my friend. What is better than the other to use?

In addition to this, for example, B. will not give you the notorious "not-empty property reference to a null or transient value" error? I'm just guessing here but I think this is because the dog is still in session.

I will understand that none of those examples work, but please explain why.

Now, for a minute, suppose I used to save my friend. Or would like to use Example B to use what's better than the other?

Both examples will work (in example B, hibernate will insert the dog before selecting it), but I do not see the point of doing an additional selection (example B) I have just gone for example, for example the dog has already passed.

Also, for example, would you not mention the notorious "zero-zero property to a null or transient value" error?

No (see above). In Example B., the dog is no longer transient. In the example A, the insert will be done in the correct order.

I'm just guessing here but I think this is because the dog is still in session.

First level cache (session) ID is used for search by, which is not the case here.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -