我是hibernate的新手。我想要做的是使用@CollectionId
为我的Address类生成一个标识符。我已经使用了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
我正在使用最新的hibernate。我该怎么办?
答案 0 :(得分:31)
Hilo不再受支持,这应该可行
@GenericGenerator(name="sequence-gen",strategy="sequence")
答案 1 :(得分:5)
如果我们使用的是mysql,则最好使用递增策略的 @GenericGenerator 。
增量-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中使用顺序策略时,我遇到了一个问题,其中我的 ADDRESS_ID 没有正确地递增。
答案 2 :(得分:1)
已删除对'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;
}
}
答案 3 :(得分:0)
您必须从Hi / Lo战略中选择一个:
为了尽可能接近您的教程,我只需在代码中将“hilo”更改为“seqhilo”。
答案 4 :(得分:0)
我建议您尝试以下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"))*
让我知道它是否对您有帮助。
答案 5 :(得分:0)
答案 6 :(得分:0)
这对PostgreSQL上的应用程序有效。尽管我尚未对其进行测试,但它应适用于所有DB。请注意,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>();