sqlserver中已经设置id为自增主键hbm文件中关于id字段的定义:
<id name="id" type="integer">
<column name="id" />
<generator class="assigned"/>
</id>如果我这样写:
VoteDAO dao=new VoteDAO();
Vote vote=new Vote();
String txtvote="不能说";
vote.setItem(txtvote); System.out.println(vote.getItem());
dao.getSession().beginTransaction().begin();
dao.getSession().saveOrUpdate(vote);
dao.getSession().beginTransaction().commit();
报如下异常:
ids for this class must be manually assigned before calling save():
com.zjw.model.Vote如果我这样写:
VoteDAO dao=new VoteDAO();
Vote vote=new Vote();
String txtvote="不能说"; vote.setId(19);
vote.setItem(txtvote); System.out.println(vote.getItem());
dao.getSession().beginTransaction().begin();
dao.getSession().saveOrUpdate(vote);
dao.getSession().beginTransaction().commit();
报如下异常:
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'vote' 中的标识列插入显式值。
设置id不行,不设置也不行,两难了!
不是说assigned是指外部程序来负责吗?
<id name="id" type="integer">
<column name="id" />
<generator class="assigned"/>
</id>如果我这样写:
VoteDAO dao=new VoteDAO();
Vote vote=new Vote();
String txtvote="不能说";
vote.setItem(txtvote); System.out.println(vote.getItem());
dao.getSession().beginTransaction().begin();
dao.getSession().saveOrUpdate(vote);
dao.getSession().beginTransaction().commit();
报如下异常:
ids for this class must be manually assigned before calling save():
com.zjw.model.Vote如果我这样写:
VoteDAO dao=new VoteDAO();
Vote vote=new Vote();
String txtvote="不能说"; vote.setId(19);
vote.setItem(txtvote); System.out.println(vote.getItem());
dao.getSession().beginTransaction().begin();
dao.getSession().saveOrUpdate(vote);
dao.getSession().beginTransaction().commit();
报如下异常:
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'vote' 中的标识列插入显式值。
设置id不行,不设置也不行,两难了!
不是说assigned是指外部程序来负责吗?
如果是使用序列自增
<generator class="assigned"/>
改为<generator class="sequence">
<param name="sequence">序列名称</param>
</generatror>
%%%% Error Creating SessionFactory %%%%
到底怎么样才能解决这个问题呢
<column name="id" />
<generator class="sequence"/>
<param name="sequence">id</param>
</id>
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity"/>
</id>"java.lang.Integer"对应的自增类型是“identity”
"assigned"是对应varchar类型
id为自增主键就只能由数据库维护.
非常感谢 用技术创造艺术 再问个问题写Integer就会报错 为什么必须得写成java.lang.Integer 呢
<id name="id" type="java.lang.Integer">
<generator class="uuid.hex"/>
不用SETID了
<column name= "id " />
<generator class= "identity "/>
</id>
这些代码呀,怎么看呀,请指教一下