create table `anietest`.`user`(
`id` int not null auto_increment,
`username` varchar(50),
primary key (`id`)
);
create unique index `PRIMARY` on `anietest`.`user`(`id`);
上面这个MYSQL的表的映射文件为
<hibernate-mapping>
<class name="org.nieweiguo.User" table="user" catalog="anietest">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="50" />
</property>
</class>
</hibernate-mapping>
这样在ACTION里面插入数据的时候就不用设置model的ID的值,但是如果把<generator class="native" />改成<generator class="assigned" /> 就得在ACTION里设置ID值为0 ,这样MYSQL中字段还是会自增
ACTION的代码:
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
UserDAO userDao=new UserDAO();
User user=new User();
//user.setId(0); class="assigned" 的时候要设置值,可以设置为0或者更大的值
user.setUsername(userForm.getUsername());
userDao.save(user);