Primary key attributes of Hibernate
You can directly query the api documentation when these attributes are used. However, api documentation may be difficult for beginners who have no experience.
This section describes how to generate the primary key of the ing file. It is important because Hibernate is used.
Entity class
package test.hibernate.hbmProperty;public class User { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
Test class
Package test. hibernate. hbmProperty; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. test; public class App {private static SessionFactory sessionFactory = new Configuration ()//. configure ()//. addClass (User. class) // Add the Hibernate object class (load the corresponding ing file ). buildSessionFactory (); @ Test public void testSave () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // build Object User = new user (); // User. setId (UUID. randomUUID (). toString (); // id is set to String type user. setName (James); // Save the session. save (user); // session. save (new User (); // ------------------------------------------------ session. getTransaction (). commit (); session. close ();}}
Pay attention to some template code, such as session factory, opening session, and closing of transaction...
File Configuration
The unique primary key ID of a database table can be incremental by Integer, or uudi, which is generated by the database with a unique ID of more than 30 characters.
Main configuration file hibernate. cfg. xml
Org. hibernate. dialect. MySQLDialect
Com. mysql. jdbc. Driver
Jdbc: mysql: // localhost: 3306/hibernate_2015
Root
686175
True
Update