Hibernate里让程序产生主键就出错了请帮我看看

05-03-28 zhangbo198294

但我想用外部程序控制 <generator class="Assigned"/>

程序产生主键:Cat cat=new Cat();

//cat.setName("tom");

cat.setSex("z");

TestCat.insertCat(cat);

运行就出错了

net.sf.hibernate.MappingException: could not instantiate id generator

at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:82)

at net.sf.hibernate.mapping.Simplevalue.createIdentifierGenerator(Simplevalue.java:80)

at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:631)

at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:715)

at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)

at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)

at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:750)

at Test.TestCat.insertCat(TestCat.java:33)

at Test.TestCat.main(TestCat.java:157)

Caused by: java.lang.ClassNotFoundException: Assigned

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:164)

at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:86)

at net.sf.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:76)

... 8 more

那这是什么原因呢?

zhangbo198294
2005-03-28 09:40

映射文件:generator class="Assigned"

banq
2005-03-28 10:24

Assigned是由你自己分配ID,不是由Hibernate自动产生的,使用native等试验一下。

zhangbo198294
2005-03-28 14:44

我就是想由自己的程序产生主键该怎么解决呢?

SportsBaby1980
2005-03-28 19:51

假定

你的cat类的id字段为:long id;

Cat cat=new Cat();

//cat.setName("tom");

cat.setSex("z");

cat.setId(1);

TestCat.insertCat(cat);

就行了

2Go 1 2 下一页