将对象保存在数据库中(如果它尚不存在)(Hibernate& Spring) [英] Save object in database if it does not already exist (Hibernate & Spring)

查看:156
本文介绍了将对象保存在数据库中(如果它尚不存在)(Hibernate& Spring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Hibernate / Spring应用程序来管理一些电影。



类影片与类风格有许多关系。
这两个类都使用GeneratedValue注释生成了id。



使用@Cascade(CascadeType.SAVE_UPDATE)$ b通过电影对象保存流派$ b我已经为类型的类型属性设置了一个唯一的约束(例如它的名字;例如Fantasy)。

现在我想要做的是Hibernate检查是否已经有类型为Fantasy的流派,如果存在,则使用该流派的ID而不是尝试插入新记录。
(后者显然会引发错误)

最后,我需要的是像select-before-update之类的东西,但更像是select-before-save。 / p>

在Hibernate中有这样一个函数吗?



一些代码:



电影课

  @Entity 
公共类电影{

@ Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

私人字符串名称;

@Lob
私有字符串描述;

@Temporal(TemporalType.TIME)
private发布日期;

@ManyToMany
@Cascade(CascadeType.SAVE_UPDATE)
private Set< Genre> genres = new HashSet< Genre>();

.... //其他方法

风格类

  @Entity 
public class Genre {

@Column(unique = true)
私人字符串类型;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id

.... //其他方法


解决方案

您可能会想到这一点。任何select-before-update / save-before-save选项将导致2个数据库往返行程,第一个用于选择,第二个用于插入(如有必要)。



如果你知道从一开始你就不会有很多流派,那么在大多数情况下,你在1 RT中有几个选择:


  1. Hibernate第二级缓存可以容纳很多(如果不是全部)流派,那么在简单散列表查找时(假设单个节点)您可以检查是否存在。

  2. 您可以假设您的所有流派已经存在,请使用session.load(),并处理新插入作为行未找到异常的结果当您引用尚未存在的流派时抛出。

实际上,除非您谈论大量交易,保存/更新前的一个简单的预查询不会影响您的表现。


I'm working on a Hibernate/Spring application to manage some movies.

The class movie has a many to many relationship with the class genre. Both of these classes have generated id's using the GeneratedValue annotation.

The genre is saved through the movie object by using @Cascade(CascadeType.SAVE_UPDATE) I have placed a unique constraint on the genre's type attribute (which is it's name; "Fantasy" for example).

What I would like to do now is have Hibernate check if there is already a genre with type "Fantasy" and if there is, use that genre's id instead of trying to insert a new record. (The latter would obviously throw an error)

Finally what I need is something like select-before-update but more like select-before-save.

Is there such a function in Hibernate?

Some code:

Movie class

@Entity
public class Movie {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

private String name;

@Lob
private String description;

@Temporal(TemporalType.TIME)
private Date releaseDate;

@ManyToMany
@Cascade(CascadeType.SAVE_UPDATE)
private Set<Genre> genres = new HashSet<Genre>();

.... //other methods

Genre class

@Entity
public class Genre {

@Column(unique=true)
private String type;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id

....//other methods

解决方案

You may be over-thinking this. Any select-before-update/select-before-save option is going to result in 2 DB round trips, the first for the select, and the second for the insert if necessary.

If you know you won't have a lot of genres from the outset, you do have a couple of options for doing this in 1 RT most of the time:

  1. The Hibernate second-level cache can hold many if not all of your Genres, resulting in a simple hashtable lookup (assuming a single node) when you check for existence.
  2. You can assume all of your genres are already existing, use session.load(), and handle the new insert as a result of the row not found exception that gets thrown when you reference a genre that doesn't already exist.

Realistically, though, unless you're talking about a LOT of transactions, a simple pre-query before save/update is not going to kill your performance.

这篇关于将对象保存在数据库中(如果它尚不存在)(Hibernate&amp; Spring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆