How to avoid creating a new row if similar row exists?

梦想的初衷 提交于 2019-12-03 02:26:53

although the row exists it creates a new one

That's not quite right. It's not just hibernate automatically deciding to create a new user. Hibernate is doing what your code tells it to:

  • In addMyUsers(), you create new Name() and new User(), and you give neither of these a pre-existing ID. You have made these objects look like new ones, rather than pre-existing ones to be updated.
  • In addMyUser(), you call session.merge(user). Hibernate sees the objects have no ID - so it merges them and assigns them the NEW status. When the transaction is flushed & committed, hibernate generates SQL to create new IDs and store the objects as new records.

If you want to determine whether an object is pre-existing, and operate on the previous record where possible:

  1. Obtain the fields that you want to use (e.g. from a web form) - in your case, this includes "det" field.
  2. See if the record already exists in the database. Retrieve the object using hibernate via session.find() or session.get(), in your case using the "det" field.
  3. If the object cannot be found, then create a new object.
  4. For retrieved objects, you can optionally detach the object from the session before modification, (e.g. via session.clear()). Newly created objects are already in a detached state.
  5. Modify the object (set it's fields).
  6. If the object is detached, then attach via session.merge(). Merge works for both pre-existing detached objects (obtained via retrieval) and new detached objects (via new <Object>()). Alternatively, for pre-existing detached objects you can call session.update() and for new detached objects you can call session.save()/persist().
  7. flush/commit the transaction.

You're missing (2).

Its primary key that plays important role here. you should use name as primary key in this case but i will not suggest you to do that, take id as primary key.

Update:

Also in case of update id should be same with the record you want to update, but in case of insertion, id should be null and you will get it inserted in db.

Answer to your comments :

you need to fetch the primary key and track it this way:

   session.update(recordEntity);// or save 
   int id=recordEntity.getId();

you must get the Name first, not new Name().

so use menu like "search user"/dropdown or something for user to choose the Name they want, then you can add that Name to new User.name

or if you dont want to search for Name first(from the thin air#lol), you should not use User.name with the type of Name in the first place, use string then. but it's more risky. and the relationship cant be build that way(you must query again if you want to get Name from the User that have name x).

Have you configured this way. Please share ur code if its not this way.

@Entity
@Table(name = "example", catalog = "example_catalog")
@org.hibernate.annotations.Entity(
        dynamicUpdate = true
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!