Description of MySQL ing of MySQL auto-increment fields in hibernate

Source: Internet
Author: User

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.

 
 
  1. create table `anietest`.`user`(  
  2. `id` int not null auto_increment,  
  3. `username` varchar(50),  
  4. primary key (`id`)  
  5. );  
  6. create unique index `PRIMARY` on `anietest`.`user`(`id`);  

The above MySQL table ing file is

 
 
  1. <class name="org.nieweiguo.User" table="user" catalog="anietest"> 
  2. <id name="id" type="java.lang.Integer"> 
  3. <column name="id" /> 
  4. <generator class="native" /> 
  5. </id> 
  6. <property name="username" type="java.lang.String"> 
  7. <column name="username" length="50" /> 
  8. </property> 
  9. </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:

 
 
  1. UserForm userForm = (UserForm) form;// TODO Auto-generated method stub  
  2. UserDAO userDao=new UserDAO();  
  3. User user=new User();  
  4. //user.setId(0); class="assigned"  

You can set the value to 0 or a greater value.

 
 
  1. user.setUsername(userForm.getUsername());  
  2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.