使用 Hibernate 在 Java 中实现序列

发布于 2024-09-29 07:38:24 字数 130 浏览 0 评论 0原文

我使用 Java 和 Hibernate 作为 ORM 工具。有什么方法可以使用 Hibernate 在 Java 中实现序列吗?

目前我正在使用 Oracle 序列来实现此目的,但随着与数据库交互的增加,这是一种非常昂贵的方法。

I am using Java and Hibernate as ORM tool. Is there any way i can implement sequence in Java using Hibernate?

Currently I am using Oracle sequences to achieve this, but that is a very costly way as interaction with database increases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-10-06 07:38:24

这实际上是一件非常简单的事情:

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 是您必须实现的 hibernate 接口。上面的例子只是生成一个随机id。

为了使用它,你必须将 generator 设置为 mypackage.RandomIdentifierGenerator

显然,这个实现缺乏任何不生成相同 id 两次的保证,这可能很重要,也可能不重要对于您正在编写的应用程序。

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. The above example just generates a random id.

In order to use this you have to set the generator to 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.

星軌x 2024-10-06 07:38:24

目前我正在使用 Oracle 序列来实现此目的,但随着与数据库交互的增加,这是一种非常昂贵的方法。

您确定使用 Oracle 序列的成本很高吗?正如其他评论者所提到的,这不太可能。话虽如此,您应该尝试使用增量超过 1 的序列。将此类序列与 hi-lopooled 一起使用 优化器,可能效果很好。如果 N 是序列的增量大小,这可确保 Hibernate SessionN 插入中仅访问数据库一次。缺点是您必须使用单独的 create_time 列来标识行的插入顺序。

Currently I am using Oracle sequences to achieve this, but that is a very costly way as interaction with database increases.

Are you sure that using Oracle sequences is costly? 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. This ensures that a Hibernate Session will hit the database only once in N inserts, if N is the increment size of the sequence. The downside is that you will have to use a separate create_time column to identify the order of insertion of rows.

十年不长 2024-10-06 07:38:24

我用这个来生成主键:java.util.UUID.randomUUID().toString().replaceAll("-", "");

它将生成32位主键,如下所示:1b694a70285f49c8a25a5de598042e7b 。

使用这种方法几乎没有机会获得重复的主键。

最好将其与 hibernateGenerateValue 一起使用。
示例代码如下:

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

uuid 设置策略是由 Hibernate 提供的,您可以通过使用自己的类的限定名设置策略来使用自己的 Java 类。

I use this to generate primary key :java.util.UUID.randomUUID().toString().replaceAll("-", "");

It will generate 32-digit primary key like this:1b694a70285f49c8a25a5de598042e7b .

It has little chance to get duplicate primary key by using this approach.

It would be nice to use this with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文