2015-10-13 127 views
10

我是新来的休眠。我正在尝试使用@CollectionId为我的Address类生成一个标识符。我已经使用Collection接口。但是,当我使用@GenericGenerator并将策略设置为hilo时,它会抛出异常。 这里是我的代码: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", 
     [email protected](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 

我使用的是最新的休眠。我该怎么办?

+0

另外我使用MySql作为我的数据库 –

回答

24

希洛不再被支持,这应该工作

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

你能解释一下删除halo的原因吗? –

0

必须从高/低策略中选择一种:

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

+0

最近的hibernate有没有任何改变?因为我下面的教程使用hibernate 3.0。我正在使用休眠5.0。最新版本不支持hilo吗? –

+0

我不知道他们什么时候做出改变,但从我知道你应该使用上述策略之一。要尽可能接近你的教程,我只需将“hilo”更改为“seqhilo” –

+0

@AdityaSawant对'hilo'生成器的支持已被删除 – kommradHomer

0

的支持“希洛”发电机已被删除。有关更多信息,该链接为您提供了不赞成使用的列表。

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

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

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

    @ElementCollection 
    @JoinTable(name="USER_ADDRESS", 
     [email protected](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; 
    } 
}