HiLo生成器策略不起作用 [英] HiLo generator strategy not working

查看:130
本文介绍了HiLo生成器策略不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的冬眠者。我正在尝试使用 @CollectionId 生成我的Address类的标识符。我为此使用了 Collection 接口。但是,当我使用 @GenericGenerator 并将策略设置为hilo时,它将引发异常。
这是我的代码:

I am new to hibernate. What I am trying to do is use @CollectionIdto generate an identifier for my Address class. I have used Collection interface for this. However when I use @GenericGenerator and set strategy to hilo, it throws an Exception. Here's my code:

@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

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

I am using the latest hibernate. What should I do?

推荐答案

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

Hilo is not supported anymore, this should work

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

这篇关于HiLo生成器策略不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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