when designing a database, we consider the relationship between tables and tables, such as the one to one, one-to-many, many-to-many relationships we have mentioned in front of us, and in our database we implement these relationships through foreign keys, third tables, and so on. The Hibernate time entity class and the tables in the database are mapped, how do these relationships map? Here's a brief summary:
A, one-to-many, many-to-one relationship mapping, where students and classes are briefly demonstrated:
1, the class of the student and the corresponding mapping file are written:
private int sid; Private String sname; Private Classes Classes; , the introduction of Class objects, many-to-one
<class name= "com.ljh.hibernate.pojo.Student" table= "T_student" lazy= "false" > <id name= "Sid" Column= "Sid" ; <generator class= "native"/> </id> <property name= "sname" column= "sname" type= "java.lang.String" l Ength= "Not-null=" "true"/> <!--represents the relationship of an object: a many-to-one name represents the Relationship Object column table of the current class The Database foreign key field (also describes the data relationship) class represents the type of the Name property value cascade the Actions (Insert,update,delete) of the active party, and the passive side also follows the same Operation. Value: Save-update, delete, all save-update: Cascade to save or update the associated object when you save or update the current object Delete: Cascade delete associated objects when deleting the current object All: Contains save,update,delete three operations. For many-to-one scenarios, cascading cannot use delete and all, otherwise it violates the integrity of the data relationship. Lazy: Lazy Loading delays initializing object information, and then querying the database when objects are used. False: Disable lazy loading proxy: Use lazy load (default) to complete the deferred load extension function with Cglib agent. No-proxy: Do not use proxies to complete lazy loading. You can use the third-party bytecode enhancement tool. Fetch: Data fetching strategy: according to the active side, query the passive side of the query method used. Fetch= "SelECT "Default value, will be found in multiple statements, often delay loading data fetch=" join "will default to the left connection query data, not delay loading data. Not-null if the value is true, then the framework uses an inner join to query the data. --<many-to-one name= "classes" column= "CID" cascade= "Save-update" lazy= "No-proxy" fetch= "join" not-null= "true" class= "Com.ljh.hibernate.pojo.Classes" ></many-to-one> </class>
2, Class entity classes and corresponding mapping files:
private int cid; Private String CNAME; Private set<student> Studentset = new hashset<student> (); Introduce a collection of student classes, one-to-many
<class name= "com.ljh.hibernate.pojo.Classes" table= "t_classes" > <id name= "cid" column= "CID" > &L T;generator class= "native"/> </id> <property name= "CNAME" column= "CNAME" type= "java.lang.String" Len Gth= "not-null=" true "/> <!--declaring a one-to-many mapping lazy: lazy Loading false: Lazy loading disabled true: Lazy Loading (default) Extra: Supports lazy loading. (recommended) When you get the collection's own information, you can send efficient query statements. For example, if you only want to get the length of the collection without needing to get information about the data in the collection, the framework calculates the collection length through the function execution query. Fetch: The query method that the data fetching strategy uses when querying the data of the associated object. Join: Query immediately with a join statement. (Lazy loading does not work) Select: Query with multiple query statements. Subselect: Queries are made immediately through subquery statements. (Not recommended) (By default, and select takes the same result) not-null= "true" for a one-to-many query, even if the Not-null statement is set, the left connection query is still used. Inverse= "true", which indicates control inversion, is managed by the other party, the student side, to manage the foreign key. Because foreign keys are in students--<set name= "Studentset" cascade= "All" inverse= "true" fetch= "Subselect" > <key column= "CID" not-null= "true";</key> <one-to-many class= "com.bjpowernode.hibernate.pojo.Student"/> </set> </CLASS&G T
Two, self-correlating: It is in their own class to associate themselves, such as the parent menu and sub-menu relationship
,
corresponding entity class, and mapping file
private int mid; private String name; Private set<menu> MenuSet = new hashset<menu> (); The relationship between the parent menu and the submenu is: one-to-many private menu pmenu;//submenus and parent menus have a
<class name= "Com.ljh.hibernate.pojo.Menu" table= "T_menu" > <id name= "mid" column= "mid" > < Generator class= "native"/> </id> <property name= "name" column= "name" type= "Java.lang.String" Length= "not-null=" true "/> <many-to-one name=" Pmenu "column=" m_id "cascade=" Save-update "></ Many-to-one> <!-- design of self-correlating tables: Foreign key fields cannot be non-empty. -- <set name= "MenuSet" cascade= "All" inverse= "true" > <key column= "m_id" ></key> <one-to-many class= "Com.ljh.hibernate.pojo.Menu"/> </set> </class>
three, one-to-one relationship mapping:
1, if the primary key is used for mapping: that is, the passive side of the primary key is from the active side of the primary key, you can also call it a foreign key:
classes to add to each other's applications.
Map file in the active side, add one-to-one label:
<one-to-one name= "userinfo" cascade= "All" class= "Com.ljh.hibernate.pojo.UserInfo" ></one-to-one> Passive side, primary key is foreign key, also add one-to-one mapping tag: <id name= "UID" column= "UID" > <generator class= "foreign" > < Param name= "Property" >user</param> </generator> </id> <!--describe a one-to-one relationship mapping constrained= "True": indicates that a one-to-one use of a foreign key association is mandatory. Add foreign KEY constraints. -- <one-to-one name= "user" constrained= "true" class= "Com.ljh.hibernate.pojo.User" ></ One-to-one>
2 , the use of FOREIGN key constraints, is actually a lot of a special case, such as students on the class,
class to add applications to each other.
Map File Middle School student end active side, add foreign key to constrain, add many-to-one label,
<!--unique : Unique constraint If the foreign key contains a unique constraint, then the relationship between the active and the passive side is one-to-one. for a-to-one mapping, you can set a cascade relationship for delete and all based on the passive side of the active query to correlate data, which is supported for lazy loading. -- <many-to-one name= "Classes" column= "CID" unique= "true" cascade= "All" class= " Com.ljh.hibernate.pojo.Classes "></many-to-one> class-Side mapping file: Add one-to-one tags: <one-to-one name=" Student "Cascade=" All "property-ref=" classes "class=" Com.ljh.hibernate.pojo.Student "></one-to-one>
Four, many-to-many relationships, such as student-to-curriculum correspondence, generate a third table for maintenance in the database:
Introduce the set set of each other in the respective class , representing many-to-many.
Student side:
<set name= "Courseset" table= "T_student_course" cascade= "save-update" > <key column= "Sid" ></key > <many-to-many class= "Com.ljh.hibernate.pojo.Course" column= "CID" ></many-to-many> /set >
Course End:
<set name= "Courseset" table= "T_student_course" cascade= "save-update" > <key column= "Sid" ></key > <many-to-many class= "Com.ljh.hibernate.pojo.Course" column= "CID" ></many-to-many> /set >
Five, the joint primary key mapping, this situation is very rare, see how it is,
When this federated key is present, we need a class for which we define a federated primary key (implementing the Serializable Interface), which declares the properties of multiple fields in the class, representing the Federated primary key field.
When you configure a mapping file, the configuration of the primary key takes advantage of the label of the Federated primary Key:
<composite-id name= "id" > <key-property name= "title" ></key-property> <key-property name= " Author "></key-property> </composite-id>
in summary, for Hibernate in the database to be associated with a few simple examples of mapping, mainly because we are based on the actual situation, the use of several corresponding tags, there is the use of the property inside. Like lazy,Cascade,Fetch and so on. Watch out for the differences. In this way, we can set up the association of the data in the database, and make it more reasonable to build the database according to the class.
Hibernate (iii)--relationship mapping in the framework