The following articles mainly introduce the ing of MySQL auto-incrementing fields in hibernate and examples of its actual application code. The following articles describe the actual operations, I hope it will help you learn the ing of MySQL auto-increment fields in hibernate.
- create table `anietest`.`user`(
- `id` int not null auto_increment,
- `username` varchar(50),
- primary key (`id`)
- );
- create unique index `PRIMARY` on `anietest`.`user`(`id`);
The above MySQL table ing file is
-
- <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>
-
In this way, when inserting data in the ACTION, you do not need to set the value of the model ID, however, if you change <generator class = "native"/> to <generator class = "assigned"/>, you must set the ID value to 0 in the ACTION, in this way, the MySQL field will still be the MySQL auto-incrementing Field
ACTION Code:
- UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
- UserDAO userDao=new UserDAO();
- User user=new User();
- //user.setId(0); class="assigned"
You can set the value to 0 or a greater value.
- user.setUsername(userForm.getUsername());
- userDao.save(user);
The above content is an introduction to the ing of MySQL auto-increment fields in hibernate. I hope you will have some gains.