gpt4 book ai didi

java - HiLo 生成器策略不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 19:48:59 24 4
gpt4 key购买 nike

我是 hibernate 新手。我想做的是使用 @CollectionId 为我的 Address 类生成一个标识符。为此,我使用了Collection 接口(interface)。但是,当我使用 @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

我正在使用最新的 hibernate 。我该怎么办?

最佳答案

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

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

关于java - HiLo 生成器策略不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33103355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com