hibernate string型为主键问题

数据库为mysql,主键设为string型的fileName,然后在hbm.xml中设置
到session.save()时出现下列异常:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.demo.model.UserFiles
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
(下略)
基本上就是存储时主键fileName这个字段没有默认值,无法保存;看来是fileName没有被识别出来,但是程序中我都明明.SetFileName().
还有hibernate操作int型主键时就没问题,对string型主键不知应该如何处理?
[b]问题补充:[/b]
to ag_sherry
那是不是数据表还必须设置一个id字段?hibernate会自动为你生成一个32位的无重复的id,不理解,感觉string还不是主键,反倒这个无重复的id是主键了


有了这个设置后,hibernate不会自动为为你生成id

在save前,你必须手动给id赋值。

主键设置为string,你可以考虑uuid,hibernate会自动为你生成一个32位的无重复的id,这样设置就可以了:

看异常抛错是说"在保存前必须设计主键",你可以在save()方法的前面用system.out的打印出对象的filename看是不是空的.
[quote]还有hibernate操作int型主键时就没问题,对string型主键不知应该如何处理? [/quote]

这个没有什么区别!

同意楼上的回答 uuid自动生成字符型主键