Many-to-many relationship in hibernate
About many-to-many relationships
Database: when using the many-to-many relationship, we can use a composite primary key. You can also disable it by Directly introducing a foreign key.
An intermediate table is required when many-to-many relationships are used in the database.
The database structure in the many-to-many relationship is as follows:
Table: Orders
Field: orderid (Primary Key ),....
Table: Users
Field: usersid (Primary Key ),....
Intermediate table: cy_order_user
Field: cy_order_user primary key id
Foreign key: cy_orderid (introduced to the Orders table)
Foreign key: cy_usersid (Users table introduced)
Note: The Foreign keys of the intermediate table must be applied in the configuration file corresponding to the object class. Therefore, to distinguish between them, orderid is not used for naming Foreign keys, but cy_orderid is used.
When you use Myeclipse to reverse generate the corresponding entity class, the intermediate entity class is generated. When configuring many-to-many relationships in Myeclipse, the information of the intermediate table is not required.
Therefore, delete the object class and configuration file of the intermediate table. You also need to delete the corresponding path in hibernate. cfg. xml. Then, you need to modify the variable names of the Set in the Orders class and Users class.
Because the ing of the object classes generated by inversion is for the intermediate table, you need to modify the Set variable in the object class.
For example, in the Orders class, change the variable name of the Set to user
Then, you need to set the relevant attributes in the configuration file corresponding to the Orders class as follows:
The following attributes need to be modified:
Note that the properties configured in the po ing file of each corresponding object po class are not required. For example, a table in the database may have 50 fields,
It does not have to configure 50 attributes in the ing file that generates the object class in reverse order. You can configure it as needed. That is to say, 50 fields in a table
The corresponding ing file may only have five attributes. You can manually add or delete the attribute as needed, but this attribute does not exist. When the database is added
It is null. If the query result is null. Therefore, you need to manually add or delete configurations as needed.
When we define a default field in the database, we need to configure dynamic-insert = "true" in the corresponding configuration file as follows:
Differences between createSQLQuery and createQuery:
The differences between createQuery and createSQLQuery in hibernate are:
The former uses the hql statement for query, and the latter can use the SQL statement for query.
The former uses the Bean generated by hibernate as the object to load the list and return
The latter is stored as an object array.
Therefore, it is not convenient to use createSQLQuery to load the list and return the object with the Bean generated by hibernate.
Suddenly, we found that createSQLQuery has such a method to directly convert objects.
Query query = session. createSQLQuery (SQL). addEntity (XXXXXXX. class );
XXXXXXX indicates the object of the Bean generated by hibernate, that is, the Bean mapped from the data table.
The paging statement in Hibernate can be written in this way.
Session = HibernateUtils. getSession ();
Session. beginTransaction ();
Query query = session. createQuery ("from User ");
Query. setFirstResult (0); // starts from the first record
Query. setMaxResults (4); // retrieve four records
List userList = query. list ();