Data Persistence is inevitable during project development. There are one-to-one, one-to-multiple, multiple-to-one, and multiple-to-multiple centralized models in the relational model. The following describes how these models are implemented in Hibernate and makes a simple record:
The first is a one-to-one model,
In this model, each student has a library card and a library card can only correspond to one student. The library card cannot exist independently. Then student's PK is ID, library_card's PK, and fk id is dependent on student's PK.
The following is a class diagram of the two classes and how to configure their ing files.
Student ing file:
<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <property name="name"></property>
<One-to-one name = "librarycard"> </one-to-one> </class>
Librarycard ing file:
<? XML version = "1.0"?> <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <one-to-one name="student" constrained="true"></one-to-one>
Constrained is used to add a foreign key constraint to the table structure. If it is not added, it will not be affected.