简体   繁体   English

使用Hibernate在Java中实现序列

[英]Implementing sequence in Java using Hibernate

I am using Java and Hibernate as ORM tool. 我正在使用Java和Hibernate作为ORM工具。 Is there any way i can implement sequence in Java using Hibernate? 有什么办法可以使用Hibernate在Java中实现序列?

Currently I am using Oracle sequences to achieve this, but that is a very costly way as interaction with database increases. 目前,我正在使用Oracle序列来实现这一目标,但是随着与数据库交互的增加,这是一种非常昂贵的方法。

It is actually a very easy thing to do: 实际上,这是一件非常容易的事情:

package mypackage;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.HibernateException;

import java.io.Serializable;
import java.security.SecureRandom;
import java.util.UUID;

public class RandomIdentifierGenerator implements IdentifierGenerator {

  private final static SecureRandom sr = new SecureRandom();

  public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException {
    long val = sr.nextLong();
    return Long.toString(Math.abs(val), Character.MAX_RADIX);
  }
}

IdentitfierGenerator is the hibernate interface you have to implement. IdentitfierGenerator是您必须实现的休眠接口。 The above example just generates a random id. 上面的示例仅生成一个随机ID。

In order to use this you have to set the generator to mypackage.RandomIdentifierGenerator 为了使用此功能,您必须将generator设置为mypackage.RandomIdentifierGenerator

Obviously this implementation lacks any guarantee of not generating the same id twice, this may or may not be important for the application you are writing. 显然,此实现不能保证不会两次生成相同的id,这对于您正在编写的应用程序可能重要,也可能不重要。

I use this to generate primary key :java.util.UUID.randomUUID().toString().replaceAll("-", ""); 我用它来生成主键:java.util.UUID.randomUUID()。toString()。replaceAll(“-”,“”);

It will generate 32-digit primary key like this:1b694a70285f49c8a25a5de598042e7b . 它将生成这样的32位主键:1b694a70285f49c8a25a5de598042e7b。

It has little chance to get duplicate primary key by using this approach. 使用这种方法获得重复主键的机会很小。

It would be nice to use this with hibernate GeneratedValue. 最好将它与hibernate GeneratedValue一起使用。 sample code would like be : 示例代码如下:

@GeneratedValue(generator="UUIdentifier")
@GenericGenerator(name="UUIdentifier", strategy = "uuid")
private String resourceId;

uuid set by to strategy is provided by Hibernate, you can feel like to use your own Java class by set strategy using qualified name of your own class. 由Hibernate提供的uuid by by策略设置是由Hibernate提供的,您可以使用自己的类的限定名称通过set strategy来使用自己的Java类。

Currently I am using Oracle sequences to achieve this, but that is a very costly way as interaction with database increases. 目前,我正在使用Oracle序列来实现这一目标,但是随着与数据库交互的增加,这是一种非常昂贵的方法。

Are you sure that using Oracle sequences is costly? 您确定使用Oracle序列成本很高吗? As other commenters have mentioned, this is unlikely. 正如其他评论者提到的那样,这不太可能。 Having said that, you should try to use sequences that increment by more than 1. Using such sequences along with hi-lo or pooled optimizers , is likely to work well. 话虽如此,您应该尝试使用递增大于1的序列。将此类序列与hi-lopooled 优化器一起使用可能会很好地工作。 This ensures that a Hibernate Session will hit the database only once in N inserts, if N is the increment size of the sequence. 如果N是序列的增量大小,这可以确保Hibernate SessionN插入中仅命中一次数据库。 The downside is that you will have to use a separate create_time column to identify the order of insertion of rows. 缺点是您将必须使用单独的create_time列来标识行的插入顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM