【问题标题】:HiLo generator strategy not workingHiLo 生成器策略不起作用
【发布时间】:2015-10-13 12:58:29
【问题描述】:

我是休眠新手。我想做的是使用@CollectionId为我的地址类生成一个标识符。我为此使用了Collection 接口。但是,当我使用 @GenericGenerator 并将策略设置为 hilo 时,它会引发异常。 这是我的代码:

@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userId;
    private String userName;

    @ElementCollection
    @JoinTable(name="USER_ADDRESS",
        joinColumns=@JoinColumn(name="USER_ID")
    )

    @GenericGenerator(name = "hilo-gen", strategy = "hilo")
    @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "hilo-gen", type = @Type(type="long"))
    private Collection<Address> address = new ArrayList<Address>();

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Collection<Address> getAddress() {
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }
}

我得到以下异常:

Exception in thread "main" org.hibernate.MappingException: Could not instantiate id generator [entity-name=null]
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:121)
    at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:259)
    at org.hibernate.persister.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:429)
    at org.hibernate.persister.collection.BasicCollectionPersister.<init>(BasicCollectionPersister.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createCollectionPersister(PersisterFactoryImpl.java:152)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createCollectionPersister(PersisterFactoryImpl.java:140)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:408)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at com.hbt.HibernateTest.main(HibernateTest.java:35)
Caused by: java.lang.UnsupportedOperationException: Support for 'hilo' generator has been removed
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:132)
    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:112)
    ... 14 more

我正在使用最新的休眠模式。我该怎么办?

【问题讨论】:

  • 我也使用 MySql 作为我的数据库

标签: java hibernate collections hibernate-annotations


【解决方案1】:

不再支持 Hilo,这应该可以工作

@GenericGenerator(name="sequence-gen",strategy="sequence")

【讨论】:

  • 你能解释一下删除halo的原因吗
【解决方案2】:

如果我们使用mysql,最好使用increment策略的@GenericGenerator。

  1. sequence - Oracle、Postgresql 支持这种策略。
  2. 增量 - MySql 支持这种策略。

    @ElementCollection
    @JoinTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID"))
    @GenericGenerator(name = "increment-gen", strategy = "increment")
    @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "increment-gen", type = @Type(type="long"))
    private Collection<Address> listOfAddress = new ArrayList<>();
    

当我在 MySql 中使用 sequence 策略时,我遇到了一个问题,我的 ADDRESS_ID 没有正确增加。

【讨论】:

  • 感谢您提供此信息,它很有帮助,我也收到了错误,但没有出现上述错误,我的代码拒绝编译并出现以下错误-org.hibernate.tool.schema.spi.CommandAcceptanceException:通过 JDBC 语句执行 DDL “drop table if exists sequence-gen”时出错。但是修改策略以增加解决了这个问题。
【解决方案3】:

已删除对 'hilo' 生成器的支持。有关其他信息,此链接为您提供了已弃用的列表。

要克服这个问题,您可以简单地使用序列生成器。这将解决您的问题。

@Entity
@Table(name = "USER_DETAILS")
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userId;
    private String userName;

    @ElementCollection
    @JoinTable(name="USER_ADDRESS",
        joinColumns=@JoinColumn(name="USER_ID")
    )

    @GenericGenerator(name = "sequence-gen", strategy = "sequence")
    @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "sequence-gen", type = @Type(type="long"))
    private Collection<Address> address = new ArrayList<Address>();

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Collection<Address> getAddress() {
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }
}

【讨论】:

    【解决方案4】:

    仅供参考,MYSQL 支持序列策略,但表名不应包含连字符“-”(这是在 @GenericGenerator(name) 中提供的名称) 在 mysql 中创建序列表时可能导致 DDL 异常。

    【讨论】:

      【解决方案5】:

      这适用于 PostgreSQL 上的应用程序。虽然我没有测试过它,但它应该适用于所有数据库。请注意,increment-gen 使用的不是序列。

      @ElementCollection
          @JoinTable( name = "user_address", joinColumns = @JoinColumn( name = "user_id"))
          @GenericGenerator(name="increment-gen",strategy="increment")
          @CollectionId( columns = { @Column( name ="address_id") }, generator ="increment-gen", type =@Type( type ="long"))
          private Collection<Address> listOfAddresses = new ArrayList<Address>();
      

      【讨论】:

        【解决方案6】:

        您必须从 Hi/Lo 策略中选择一种:

        为了尽可能接近您的教程,我只需在您的代码中将“hilo”更改为“seqhilo”。

        【讨论】:

        • 最新的hibernate有变化吗?因为我正在关注的教程使用休眠 3.0。我正在使用休眠 5.0。最新版本不支持hilo吗?
        • 我不知道他们何时做出改变,但据我所知,您应该使用上述策略之一。为了尽可能接近您的教程,我只需将“hilo”更改为“seqhilo”
        • @AdityaSawant 已删除对“hilo”生成器的支持
        • @kommradHomer 删除的原因是什么?
        • @KasunSiyambalapitiya 我不知道。但据我记得,他们只是替换了它的实现或使用方式。因此,如果您正确调整代码,您仍然可以拥有该功能
        【解决方案7】:

        我建议您尝试以下 2 种解决方案中的任何一种,它会解决您的问题。它符合 Hibernate 5.2.X.

        中提供的规范

        信息来源-https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

        解决方案 1 -

         @GenericGenerator(name = "product_generator",strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator")
         @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "product_generator", type = @Type(type="long"))*
        

        解决方案 2 -

        *@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "product_generator")
         @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "product_generator", type = @Type(type="long"))*
        

        如果对你有帮助,请告诉我。

        【讨论】:

        • 你能解释一下问题的根源是什么以及你的三行是如何解决的吗?
        • 问题的根源是 Aditya Sawant 试图在 hibernate 中使用 Hilo 策略。在较新版本的休眠中已弃用。我的 2 解决方案建议如何使用 SEQUENCE 类型的通用生成器来实现相同的目标。
        猜你喜欢
        • 1970-01-01
        • 2017-02-16
        • 2013-03-30
        • 2023-04-07
        • 1970-01-01
        • 2021-06-24
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多